Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experiment] async_hooks: optimize fast-path promise hook for ALS #34512

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions benchmark/async_hooks/promises.js
Expand Up @@ -19,6 +19,11 @@ const tests = {
promiseResolve() {},
destroy() {}
}).enable();
},
enabledWithInitOnly() {
hook = createHook({
init() {}
}).enable();
}
};

Expand All @@ -27,6 +32,7 @@ const bench = common.createBenchmark(main, {
asyncHooks: [
'enabled',
'enabledWithDestroy',
'enabledWithInitOnly',
'disabled',
]
});
Expand Down
66 changes: 59 additions & 7 deletions src/async_wrap.cc
Expand Up @@ -231,19 +231,18 @@ PromiseWrap* PromiseWrap::New(Environment* env,

// Skip for init events
if (silent) {
Local<Value> maybeAsyncId = promise
Local<Value> maybe_async_id = promise
->Get(context, env->async_id_symbol())
.ToLocalChecked();

Local<Value> maybeTriggerAsyncId = promise
Local<Value> maybe_trigger_async_id = promise
->Get(context, env->trigger_async_id_symbol())
.ToLocalChecked();

if (maybeAsyncId->IsNumber() && maybeTriggerAsyncId->IsNumber()) {
double asyncId = maybeAsyncId->NumberValue(context).ToChecked();
double triggerAsyncId = maybeTriggerAsyncId->NumberValue(context)
.ToChecked();
return new PromiseWrap(env, obj, asyncId, triggerAsyncId);
if (maybe_async_id->IsNumber() && maybe_trigger_async_id->IsNumber()) {
double async_id = maybe_async_id.As<Number>()->Value();
double trigger_async_id = maybe_trigger_async_id.As<Number>()->Value();
return new PromiseWrap(env, obj, async_id, trigger_async_id);
}
}

Expand Down Expand Up @@ -320,6 +319,59 @@ static void FastPromiseHook(PromiseHookType type, Local<Promise> promise,
Environment* env = Environment::GetCurrent(context);
if (env == nullptr) return;

if (type == PromiseHookType::kBefore &&
env->async_hooks()->fields()[AsyncHooks::kBefore] == 0) {
Local<Value> maybe_async_id;
if (!promise->Get(context, env->async_id_symbol())
.ToLocal(&maybe_async_id)) {
return;
}

Local<Value> maybe_trigger_async_id;
if (!promise->Get(context, env->trigger_async_id_symbol())
.ToLocal(&maybe_trigger_async_id)) {
return;
}

if (maybe_async_id->IsNumber() && maybe_trigger_async_id->IsNumber()) {
double async_id = maybe_async_id.As<Number>()->Value();
double trigger_async_id = maybe_trigger_async_id.As<Number>()->Value();
env->async_hooks()->push_async_context(
async_id, trigger_async_id, promise);
}

return;
}

if (type == PromiseHookType::kAfter &&
env->async_hooks()->fields()[AsyncHooks::kAfter] == 0) {
Local<Value> maybe_async_id;
if (!promise->Get(context, env->async_id_symbol())
.ToLocal(&maybe_async_id)) {
return;
}

if (maybe_async_id->IsNumber()) {
double async_id = maybe_async_id.As<Number>()->Value();
if (env->execution_async_id() == async_id) {
// This condition might not be true if async_hooks was enabled during
// the promise callback execution.
env->async_hooks()->pop_async_context(async_id);
}
}

return;
}

if (type == PromiseHookType::kResolve &&
env->async_hooks()->fields()[AsyncHooks::kPromiseResolve] == 0) {
return;
}

// Getting up to this point means either init type or
// that there are active hooks of another type.
// In both cases fast-path JS hook should be called.

Local<Value> argv[] = {
Integer::New(env->isolate(), ToAsyncHooksType(type)),
promise,
Expand Down