esm: avoid dictionary-mode objects in import()

Import attributes were created in C++ with v8::Object::New() and a
null prototype, once per module request and once per import() call.
Every import() call also created a `{ __proto__: null }`
diagnostics_channel context, which was discarded when module.import
had no subscribers. V8 creates all of these in dictionary mode.

Build the attributes from an ordinary object with its prototype set to
null, and only create the trace context when there are subscribers.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/67554
Refs: https://github.com/nodejs/node/pull/67552
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-11-23 12:34:20 +01:00
1 parent a126f00a6b
commit 43e6622ae6
2 files changed
+18 -15

No files matched your search

+7 -5
View File
@@ -520,6 +520,12 @@ class ModuleLoader {
* @returns {Promise<ModuleExports>}
*/
async import(specifier, parentURL, importAttributes, phase = kEvaluationPhase, isEntryPoint = false) {
// tracePromise() ignores the context when there are no subscribers, so only
// create it when there are: a `{ __proto__: null }` literal is created in
// dictionary mode, and import() can be called on a hot path.
const traceContext = onImport.hasSubscribers ?
{ __proto__: null, parentURL, url: specifier } :
undefined;
return onImport.tracePromise(async () => {
const request = new ImportRequest(specifier, phase, importAttributes);
const moduleJob = this.getOrCreateModuleJob(parentURL, request);
@@ -532,11 +538,7 @@ class ModuleLoader {
}
const { module } = await moduleJob.run(isEntryPoint);
return module.getNamespace();
}, {
__proto__: null,
parentURL,
url: specifier,
});
}, traceContext);
}
/**
+11 -10
View File
@@ -571,9 +571,13 @@ static Local<Object> createImportAttributesContainer(
Local<FixedArray> raw_attributes,
const int elements_per_attribute) {
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
size_t num_attributes = raw_attributes->Length() / elements_per_attribute;
LocalVector<Name> names(isolate, num_attributes);
LocalVector<Value> values(isolate, num_attributes);
Local<Context> context = realm->context();
// v8::Object::New() with a prototype argument always creates the object in
// dictionary mode. Null the prototype of an ordinary object instead, so that
// the attributes, which the loader reads for every import, get a fast map.
Local<Object> attributes = Object::New(isolate);
attributes->SetPrototype(context, Null(isolate)).Check();
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
Local<Data> key = raw_attributes->Get(i);
@@ -581,15 +585,12 @@ static Local<Object> createImportAttributesContainer(
DCHECK(DataIsString(key));
DCHECK(DataIsString(value));
int idx = i / elements_per_attribute;
names[idx] = key.As<String>();
values[idx] = value.As<String>();
attributes
->CreateDataProperty(context, key.As<String>(), value.As<String>())
.Check();
}
Local<Object> attributes = Object::New(
isolate, Null(isolate), names.data(), values.data(), num_attributes);
attributes->SetIntegrityLevel(realm->context(), v8::IntegrityLevel::kFrozen)
.Check();
attributes->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen).Check();
return attributes;
}