summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/async_hooks.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-07-06 13:05:10 +0200
committerGitHub <noreply@github.com>2023-07-06 13:05:10 +0200
commitde630b9b78ad09d3092e6fff4629d8503693bdff (patch)
tree26340adbac51e4b02b88a50da15342a31ff2ec64 /ext/node/polyfills/async_hooks.ts
parent57fae55d822f5aae52ea93d0e55155bc9c12672f (diff)
perf(node/async_hooks): optimize AsyncLocalStorage (#19729)
This makes the implementation of "AsyncLocalStorage" from "node:async_hooks" 3.5x faster than before for noop benchmark (measuring baseline overhead). It's still 3.5x slower than not using `AsyncLocalStorage` and 1.64x slower than using noop promise hooks.
Diffstat (limited to 'ext/node/polyfills/async_hooks.ts')
-rw-r--r--ext/node/polyfills/async_hooks.ts20
1 files changed, 9 insertions, 11 deletions
diff --git a/ext/node/polyfills/async_hooks.ts b/ext/node/polyfills/async_hooks.ts
index df42847a2..c97e81ef5 100644
--- a/ext/node/polyfills/async_hooks.ts
+++ b/ext/node/polyfills/async_hooks.ts
@@ -10,6 +10,7 @@
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
const { core } = globalThis.__bootstrap;
+const { ops } = core;
function assert(cond: boolean) {
if (!cond) throw new Error("Assertion failed");
@@ -29,10 +30,6 @@ let rootAsyncFrame: AsyncContextFrame | undefined = undefined;
let promiseHooksSet = false;
const asyncContext = Symbol("asyncContext");
-function isRejected(promise: Promise<unknown>) {
- const [state] = core.getPromiseDetails(promise);
- return state == 2;
-}
function setPromiseHooks() {
if (promiseHooksSet) {
@@ -43,12 +40,14 @@ function setPromiseHooks() {
const init = (promise: Promise<unknown>) => {
const currentFrame = AsyncContextFrame.current();
if (!currentFrame.isRoot()) {
- assert(AsyncContextFrame.tryGetContext(promise) == null);
+ if (typeof promise[asyncContext] !== "undefined") {
+ throw new Error("Promise already has async context");
+ }
AsyncContextFrame.attachContext(promise);
}
};
const before = (promise: Promise<unknown>) => {
- const maybeFrame = AsyncContextFrame.tryGetContext(promise);
+ const maybeFrame = promise[asyncContext];
if (maybeFrame) {
pushAsyncFrame(maybeFrame);
} else {
@@ -57,16 +56,16 @@ function setPromiseHooks() {
};
const after = (promise: Promise<unknown>) => {
popAsyncFrame();
- if (!isRejected(promise)) {
+ if (!ops.op_node_is_promise_rejected(promise)) {
// @ts-ignore promise async context
- delete promise[asyncContext];
+ promise[asyncContext] = undefined;
}
};
const resolve = (promise: Promise<unknown>) => {
const currentFrame = AsyncContextFrame.current();
if (
- !currentFrame.isRoot() && isRejected(promise) &&
- AsyncContextFrame.tryGetContext(promise) == null
+ !currentFrame.isRoot() && ops.op_node_is_promise_rejected(promise) &&
+ typeof promise[asyncContext] === "undefined"
) {
AsyncContextFrame.attachContext(promise);
}
@@ -117,7 +116,6 @@ class AsyncContextFrame {
}
static attachContext(promise: Promise<unknown>) {
- assert(!(asyncContext in promise));
// @ts-ignore promise async context
promise[asyncContext] = AsyncContextFrame.current();
}