-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
src: avoid env lookups and a global handle in InternalCallbackScope #66316
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
Open
nigrosimone
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
nigrosimone:callback-scope-cost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #include <node.h> | ||
| #include <uv.h> | ||
| #include <v8.h> | ||
|
|
||
| using v8::Context; | ||
| using v8::Function; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Global; | ||
| using v8::HandleScope; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Object; | ||
| using v8::Value; | ||
|
|
||
| struct State { | ||
| uv_timer_t timer; | ||
| Isolate* isolate; | ||
| int64_t n; | ||
| Global<Function> fn; | ||
| Global<Function> done; | ||
| }; | ||
|
|
||
| static void OnTimer(uv_timer_t* handle) { | ||
| State* state = static_cast<State*>(handle->data); | ||
| Isolate* isolate = state->isolate; | ||
| HandleScope handle_scope(isolate); | ||
| Local<Function> fn = state->fn.Get(isolate); | ||
| Local<Context> context = fn->GetCreationContextChecked(isolate); | ||
| Context::Scope context_scope(context); | ||
| Local<Object> recv = context->Global(); | ||
| for (int64_t i = 0; i < state->n; i++) { | ||
| HandleScope inner_scope(isolate); | ||
| (void)node::MakeCallback(isolate, recv, fn, 0, nullptr, {0, 0}); | ||
| } | ||
| Local<Function> done = state->done.Get(isolate); | ||
| (void)node::MakeCallback(isolate, recv, done, 0, nullptr, {0, 0}); | ||
| uv_close(reinterpret_cast<uv_handle_t*>(&state->timer), | ||
| [](uv_handle_t* h) { delete static_cast<State*>(h->data); }); | ||
| } | ||
|
|
||
| // run(n, fn, done): calls fn n times from a timer, then calls done. | ||
| static void Run(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| State* state = new State; | ||
| state->isolate = isolate; | ||
| state->n = args[0]->IntegerValue(isolate->GetCurrentContext()).FromJust(); | ||
| state->fn.Reset(isolate, args[1].As<Function>()); | ||
| state->done.Reset(isolate, args[2].As<Function>()); | ||
| state->timer.data = state; | ||
| uv_timer_init(node::GetCurrentEventLoop(isolate), &state->timer); | ||
| uv_timer_start(&state->timer, OnTimer, 0, 0); | ||
| } | ||
|
|
||
| static void Initialize(Local<Object> target, Local<Value> module, void* data) { | ||
| NODE_SET_METHOD(target, "run", Run); | ||
| } | ||
|
|
||
| NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| 'targets': [ | ||
| { | ||
| 'target_name': 'binding', | ||
| 'sources': [ 'binding.cc' ] | ||
| } | ||
| ] | ||
| } | ||
|
nigrosimone marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../../common.js'); | ||
|
|
||
| // The addon calls into JS with node::MakeCallback from a libuv timer, so | ||
| // every call opens a top-level callback scope, like an I/O callback does. | ||
|
|
||
| let binding; | ||
| try { | ||
| binding = require(`./build/${common.buildType}/binding`); | ||
| } catch { | ||
| console.error('napi/make_callback/index.js Binding failed to load'); | ||
| process.exit(0); | ||
|
nigrosimone marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1e6, 1e7], | ||
| }); | ||
|
|
||
| function main({ n }) { | ||
| bench.start(); | ||
| binding.run(n, () => {}, () => bench.end(n)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../../common'); | ||
| const assert = require('assert'); | ||
| const { AsyncLocalStorage } = require('async_hooks'); | ||
| const { runInCallbackScope } = require(`./build/${common.buildType}/binding`); | ||
|
|
||
| // A CallbackScope must restore the async context frame that was active | ||
| // before it, when there was none and when there was one. | ||
|
|
||
| const als = new AsyncLocalStorage(); | ||
|
|
||
| runInCallbackScope({}, 0, 0, common.mustCall(() => { | ||
| als.enterWith('inner'); | ||
| })); | ||
| assert.strictEqual(als.getStore(), undefined); | ||
|
|
||
| als.run('outer', common.mustCall(() => { | ||
| runInCallbackScope({}, 0, 0, common.mustCall(() => { | ||
| als.enterWith('inner'); | ||
| })); | ||
| assert.strictEqual(als.getStore(), 'outer'); | ||
| })); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.