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

process: js fast path for cached bindings #18365

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 36 additions & 8 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

setupProcessObject();

internalBinding = process._internalBinding;
delete process._internalBinding;

// do this good and early, since it handles errors.
setupProcessFatal();

Expand Down Expand Up @@ -246,13 +243,44 @@
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
}

function pushValueToArray() {
for (var i = 0; i < arguments.length; i++)
this.push(arguments[i]);
}

function setupProcessBinding(bindingObj) {
const _binding = process.binding;

process.binding = function binding(module) {
if (!module || typeof module !== 'string')
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s unfortunate, but process.binding is used in the wild too often for us to be able to just return undefined if the requested module is undefined. We should cast it to a string just as the C++ version did.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, when would core code call it with something that is not a string? This guard should probably just go.

if (typeof bindingObj[module] === 'object')
return bindingObj[module];
return _binding(module);
};
}

function setupInternalBinding(bindingObj) {
const _internalBinding = process._internalBinding;
delete process._internalBinding;

internalBinding = function internalBinding(module) {
if (!module || typeof module !== 'string')
return;
if (typeof bindingObj[module] === 'object')
return bindingObj[module];
return _internalBinding(module);
};
}

function setupProcessObject() {
process._setupProcessObject(pushValueToArray);
const [
bindingObj,
internalBindingObj
] = process._setupProcessObject(pushValueToArray);

function pushValueToArray() {
for (var i = 0; i < arguments.length; i++)
this.push(arguments[i]);
}
setupProcessBinding(bindingObj);
setupInternalBinding(internalBindingObj);
}

function setupGlobalVariables() {
Expand Down
37 changes: 11 additions & 26 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,13 @@ void SetupProcessObject(const FunctionCallbackInfo<Value>& args) {
env->process_object()->Delete(
env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "_setupProcessObject")).FromJust();


Local<Array> ret = Array::New(env->isolate(), 2);
ret->Set(env->context(), 0, env->binding_cache_object()).FromJust();
ret->Set(env->context(), 1, env->internal_binding_cache_object()).FromJust();

args.GetReturnValue().Set(ret);
}


Expand Down Expand Up @@ -2526,22 +2533,6 @@ Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
}


static bool PullFromCache(Environment* env,
const FunctionCallbackInfo<Value>& args,
Local<String> module,
Local<Object> cache) {
Local<Context> context = env->context();
Local<Value> exports_v;
Local<Object> exports;
if (cache->Get(context, module).ToLocal(&exports_v) &&
exports_v->IsObject() &&
exports_v->ToObject(context).ToLocal(&exports)) {
args.GetReturnValue().Set(exports);
return true;
}
return false;
}

static Local<Object> InitModule(Environment* env,
node_module* mod,
Local<String> module) {
Expand Down Expand Up @@ -2569,14 +2560,11 @@ static void ThrowIfNoSuchModule(Environment* env, const char* module_v) {
static void Binding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Local<String> module;
if (!args[0]->ToString(env->context()).ToLocal(&module)) return;
CHECK(args[0]->IsString());

Local<String> module = args[0].As<String>();
Local<Object> cache = env->binding_cache_object();

if (PullFromCache(env, args, module, cache))
return;

// Append a string to process.moduleLoadList
char buf[1024];
node::Utf8Value module_v(env->isolate(), module);
Expand Down Expand Up @@ -2609,14 +2597,11 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
static void InternalBinding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Local<String> module;
if (!args[0]->ToString(env->context()).ToLocal(&module)) return;
CHECK(args[0]->IsString());

Local<String> module = args[0].As<String>();
Local<Object> cache = env->internal_binding_cache_object();

if (PullFromCache(env, args, module, cache))
return;

// Append a string to process.moduleLoadList
char buf[1024];
node::Utf8Value module_v(env->isolate(), module);
Expand Down