From 3872251a168bd9ff62aca29cce46194b4dbbc22c Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Thu, 26 Nov 2026 20:36:48 +0100 Subject: [PATCH] src: add MakeCallback() taking an Environment node::MakeCallback() looks up the Environment from the creation context of the callback on every call. An addon that calls into the same Environment many times, a server calling one handler per request for example, can now take it once with GetCurrentEnvironment() and pass it. The callback must belong to that Environment. Add a benchmark, napi/make_callback_env, comparing the two overloads. Refs: https://github.com/nodejs/performance/issues/24 Signed-off-by: Nigro Simone PR-URL: https://github.com/nodejs/node/pull/66510 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- benchmark/napi/make_callback_env/.gitignore | 1 + benchmark/napi/make_callback_env/binding.cc | 71 ++++++++++++++++++++ benchmark/napi/make_callback_env/binding.gyp | 15 +++++ benchmark/napi/make_callback_env/index.js | 26 +++++++ src/api/callback.cc | 11 +++ src/node.h | 11 +++ 6 files changed, 135 insertions(+) create mode 100644 benchmark/napi/make_callback_env/.gitignore create mode 100644 benchmark/napi/make_callback_env/binding.cc create mode 100644 benchmark/napi/make_callback_env/binding.gyp create mode 100644 benchmark/napi/make_callback_env/index.js diff --git a/benchmark/napi/make_callback_env/.gitignore b/benchmark/napi/make_callback_env/.gitignore new file mode 100644 index 00000000000..567609b1234 --- /dev/null +++ b/benchmark/napi/make_callback_env/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/benchmark/napi/make_callback_env/binding.cc b/benchmark/napi/make_callback_env/binding.cc new file mode 100644 index 00000000000..f61a7597b6d --- /dev/null +++ b/benchmark/napi/make_callback_env/binding.cc @@ -0,0 +1,71 @@ +#include +#include +#include + +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; + +// Same order as the types in index.js. +enum Type { kMakeCallback, kMakeCallbackEnv }; + +struct State { + uv_timer_t timer; + Isolate* isolate; + node::Environment* env; + int64_t n; + Type type; + Global fn; + Global done; +}; + +static void OnTimer(uv_timer_t* handle) { + State* state = static_cast(handle->data); + Isolate* isolate = state->isolate; + HandleScope handle_scope(isolate); + Local fn = state->fn.Get(isolate); + Local context = fn->GetCreationContextChecked(isolate); + Context::Scope context_scope(context); + Local recv = context->Global(); + for (int64_t i = 0; i < state->n; i++) { + HandleScope inner_scope(isolate); + if (state->type == kMakeCallbackEnv) { + (void)node::MakeCallback(state->env, recv, fn, 0, nullptr, {0, 0}); + } else { + (void)node::MakeCallback(isolate, recv, fn, 0, nullptr, {0, 0}); + } + } + Local done = state->done.Get(isolate); + (void)node::MakeCallback(isolate, recv, done, 0, nullptr, {0, 0}); + uv_close(reinterpret_cast(&state->timer), + [](uv_handle_t* h) { delete static_cast(h->data); }); +} + +// run(n, fn, done, type): calls fn n times from a timer, then calls done. +static void Run(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + State* state = new State; + state->isolate = isolate; + // Found once, the point of the MakeCallbackEnv type. + state->env = node::GetCurrentEnvironment(context); + state->n = args[0]->IntegerValue(context).FromJust(); + state->fn.Reset(isolate, args[1].As()); + state->done.Reset(isolate, args[2].As()); + state->type = static_cast(args[3]->Int32Value(context).FromJust()); + state->timer.data = state; + uv_timer_init(node::GetCurrentEventLoop(isolate), &state->timer); + uv_timer_start(&state->timer, OnTimer, 0, 0); +} + +static void Initialize(Local target, Local module, void* data) { + NODE_SET_METHOD(target, "run", Run); +} + +NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) diff --git a/benchmark/napi/make_callback_env/binding.gyp b/benchmark/napi/make_callback_env/binding.gyp new file mode 100644 index 00000000000..f8eda24c7fb --- /dev/null +++ b/benchmark/napi/make_callback_env/binding.gyp @@ -0,0 +1,15 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': [ 'binding.cc' ], + 'conditions': [ + # node-gyp does not pass the include dirs of the Node.js build, and + # the addon headers have uv.h only when libuv is bundled. + [ 'node_shared_libuv=="true"', { + 'include_dirs': [ '<@(node_shared_libuv_include_dirs)' ], + }], + ], + } + ] +} diff --git a/benchmark/napi/make_callback_env/index.js b/benchmark/napi/make_callback_env/index.js new file mode 100644 index 00000000000..cef720ec647 --- /dev/null +++ b/benchmark/napi/make_callback_env/index.js @@ -0,0 +1,26 @@ +'use strict'; + +const common = require('../../common.js'); + +// node::MakeCallback from a libuv timer, the Environment looked up from the +// callback on every call (MakeCallback) or passed in (MakeCallbackEnv). + +let binding; +try { + binding = require(`./build/${common.buildType}/binding`); +} catch { + console.error('napi/make_callback_env/index.js Binding failed to load'); + process.exit(0); +} + +const types = ['MakeCallback', 'MakeCallbackEnv']; + +const bench = common.createBenchmark(main, { + type: types, + n: [1e6, 1e7], +}); + +function main({ type, n }) { + bench.start(); + binding.run(n, () => {}, () => bench.end(n), types.indexOf(type)); +} diff --git a/src/api/callback.cc b/src/api/callback.cc index dde3e82dacc..19144a72b3f 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -393,6 +393,17 @@ MaybeLocal InternalMakeCallback(Isolate* isolate, env, recv, callback, argc, argv, asyncContext, context_frame); } +MaybeLocal MakeCallback(Environment* env, + Local recv, + Local callback, + int argc, + Local argv[], + async_context asyncContext) { + CHECK_NOT_NULL(env); + return MakeCallbackInEnvironment( + env, recv, callback, argc, argv, asyncContext, Undefined(env->isolate())); +} + MaybeLocal MakeCallbackInEnvironment(Environment* env, Local recv, const Local callback, diff --git a/src/node.h b/src/node.h index 0130904b1c8..d440d89e788 100644 --- a/src/node.h +++ b/src/node.h @@ -1674,6 +1674,17 @@ v8::MaybeLocal MakeCallback(v8::Isolate* isolate, int argc, v8::Local* argv, async_context asyncContext); +/* The same, for an addon that calls into the same Environment many times: + * `env` is passed in, for example from GetCurrentEnvironment() once, instead + * of being looked up from the creation context of `callback` on every call. + * `callback` must belong to `env`. */ +NODE_EXTERN +v8::MaybeLocal MakeCallback(Environment* env, + v8::Local recv, + v8::Local callback, + int argc, + v8::Local* argv, + async_context asyncContext); NODE_EXTERN v8::MaybeLocal MakeCallback(v8::Isolate* isolate, v8::Local recv,