Skip to content
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
Prev Previous commit
Next Next commit
fixup: move the full caching functionality to JS
  • Loading branch information
apapirovski committed Jan 25, 2018
commit 753a08149293ef6764fb317419d40efc7a799e2d
47 changes: 26 additions & 21 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,44 +243,49 @@
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;
{
const bindingObj = Object.create(null);

const getBinding = process.binding;
process.binding = function binding(module) {
if (!module || typeof module !== 'string')
return;
module = String(module);
if (typeof bindingObj[module] === 'object')
return bindingObj[module];
bindingObj[module] = getBinding(module);
return bindingObj[module];
};

const getLinkedBinding = process._linkedBinding;
process._linkedBinding = function _linkedBinding(module) {
module = String(module);
if (typeof bindingObj[module] === 'object')
return bindingObj[module];
return _binding(module);
bindingObj[module] = getLinkedBinding(module);
return bindingObj[module];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor anti-pattern, looking up twice. Do this instead:

let mod = bindingObj[module];
if (typeof mod !== 'object')
  mod = bindingObj[module] = getBinding(module);
return mod;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, don't know what I was thinking :) Updated.

};
}

function setupInternalBinding(bindingObj) {
const _internalBinding = process._internalBinding;
{
const bindingObj = Object.create(null);

const getInternalBinding = 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);
bindingObj[module] = getInternalBinding(module);
return bindingObj[module];
};
}

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

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

function setupGlobalVariables() {
Expand Down
10 changes: 0 additions & 10 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,6 @@ inline Environment::Environment(IsolateData* isolate_data,
v8::Context::Scope context_scope(context);
set_as_external(v8::External::New(isolate(), this));

v8::Local<v8::Primitive> null = v8::Null(isolate());
v8::Local<v8::Object> binding_cache_object = v8::Object::New(isolate());
CHECK(binding_cache_object->SetPrototype(context, null).FromJust());
set_binding_cache_object(binding_cache_object);

v8::Local<v8::Object> internal_binding_cache_object =
v8::Object::New(isolate());
CHECK(internal_binding_cache_object->SetPrototype(context, null).FromJust());
set_internal_binding_cache_object(internal_binding_cache_object);

set_module_load_list_array(v8::Array::New(isolate()));

AssignToContext(context, ContextInfo(""));
Expand Down
2 changes: 0 additions & 2 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ class ModuleWrap;
V(async_hooks_after_function, v8::Function) \
V(async_hooks_promise_resolve_function, v8::Function) \
V(async_hooks_binding, v8::Object) \
V(binding_cache_object, v8::Object) \
V(internal_binding_cache_object, v8::Object) \
V(buffer_prototype_object, v8::Object) \
V(context, v8::Context) \
V(host_import_module_dynamically_callback, v8::Function) \
Expand Down
21 changes: 2 additions & 19 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,6 @@ 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 @@ -2563,7 +2556,6 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());

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

// Append a string to process.moduleLoadList
char buf[1024];
Expand All @@ -2589,7 +2581,6 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
} else {
return ThrowIfNoSuchModule(env, *module_v);
}
cache->Set(module, exports);

args.GetReturnValue().Set(exports);
}
Expand All @@ -2600,7 +2591,6 @@ static void InternalBinding(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());

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

// Append a string to process.moduleLoadList
char buf[1024];
Expand All @@ -2614,22 +2604,16 @@ static void InternalBinding(const FunctionCallbackInfo<Value>& args) {
node_module* mod = get_internal_module(*module_v);
if (mod == nullptr) return ThrowIfNoSuchModule(env, *module_v);
Local<Object> exports = InitModule(env, mod, module);
cache->Set(module, exports);

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

static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());

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

Local<Object> cache = env->binding_cache_object();
Local<Value> exports_v = cache->Get(module_name);
CHECK(args[0]->IsString());

if (exports_v->IsObject())
return args.GetReturnValue().Set(exports_v.As<Object>());
Local<String> module_name = args[0].As<String>();

node::Utf8Value module_name_v(env->isolate(), module_name);
node_module* mod = get_linked_module(*module_name_v);
Expand Down Expand Up @@ -2660,7 +2644,6 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
}

auto effective_exports = module->Get(exports_prop);
cache->Set(module_name, effective_exports);

args.GetReturnValue().Set(effective_exports);
}
Expand Down