Commit 3872251a168 for nodejs

commit 3872251a168bd9ff62aca29cce46194b4dbbc22c
Author: Nigro Simone <nigro.simone@gmail.com>
Date:   Thu Nov 26 20:36:48 2026 +0100

    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 <nigro.simone@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/66510
    Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
    Reviewed-By: James M Snell <jasnell@gmail.com>

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 <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;
+
+// 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<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);
+    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<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, type): calls fn n times from a timer, then calls done.
+static void Run(const FunctionCallbackInfo<Value>& args) {
+  Isolate* isolate = args.GetIsolate();
+  Local<Context> 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<Function>());
+  state->done.Reset(isolate, args[2].As<Function>());
+  state->type = static_cast<Type>(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<Object> target, Local<Value> 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<Value> InternalMakeCallback(Isolate* isolate,
       env, recv, callback, argc, argv, asyncContext, context_frame);
 }

+MaybeLocal<Value> MakeCallback(Environment* env,
+                               Local<Object> recv,
+                               Local<Function> callback,
+                               int argc,
+                               Local<Value> argv[],
+                               async_context asyncContext) {
+  CHECK_NOT_NULL(env);
+  return MakeCallbackInEnvironment(
+      env, recv, callback, argc, argv, asyncContext, Undefined(env->isolate()));
+}
+
 MaybeLocal<Value> MakeCallbackInEnvironment(Environment* env,
                                             Local<Object> recv,
                                             const Local<Function> 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<v8::Value> MakeCallback(v8::Isolate* isolate,
                                        int argc,
                                        v8::Local<v8::Value>* 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<v8::Value> MakeCallback(Environment* env,
+                                       v8::Local<v8::Object> recv,
+                                       v8::Local<v8::Function> callback,
+                                       int argc,
+                                       v8::Local<v8::Value>* argv,
+                                       async_context asyncContext);
 NODE_EXTERN
 v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
                                        v8::Local<v8::Object> recv,