Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/napi/make_callback_env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
71 changes: 71 additions & 0 deletions benchmark/napi/make_callback_env/binding.cc
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions benchmark/napi/make_callback_env/binding.gyp
Original file line number Diff line number Diff line change
@@ -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)' ],
}],
],
}
]
}
26 changes: 26 additions & 0 deletions benchmark/napi/make_callback_env/index.js
Original file line number Diff line number Diff line change
@@ -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));
}
11 changes: 11 additions & 0 deletions src/api/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading