Skip to content

Commit 43e6622

Browse files
mcollinaaduh95
authored andcommitted
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: #67554 Refs: #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>
1 parent a126f00 commit 43e6622

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

‎lib/internal/modules/esm/loader.js‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,12 @@ class ModuleLoader {
520520
* @returns {Promise<ModuleExports>}
521521
*/
522522
async import(specifier, parentURL, importAttributes, phase = kEvaluationPhase, isEntryPoint = false) {
523+
// tracePromise() ignores the context when there are no subscribers, so only
524+
// create it when there are: a `{ __proto__: null }` literal is created in
525+
// dictionary mode, and import() can be called on a hot path.
526+
const traceContext = onImport.hasSubscribers ?
527+
{ __proto__: null, parentURL, url: specifier } :
528+
undefined;
523529
return onImport.tracePromise(async () => {
524530
const request = new ImportRequest(specifier, phase, importAttributes);
525531
const moduleJob = this.getOrCreateModuleJob(parentURL, request);
@@ -532,11 +538,7 @@ class ModuleLoader {
532538
}
533539
const { module } = await moduleJob.run(isEntryPoint);
534540
return module.getNamespace();
535-
}, {
536-
__proto__: null,
537-
parentURL,
538-
url: specifier,
539-
});
541+
}, traceContext);
540542
}
541543

542544
/**

‎src/module_wrap.cc‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -571,25 +571,26 @@ static Local<Object> createImportAttributesContainer(
571571
Local<FixedArray> raw_attributes,
572572
const int elements_per_attribute) {
573573
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
574-
size_t num_attributes = raw_attributes->Length() / elements_per_attribute;
575-
LocalVector<Name> names(isolate, num_attributes);
576-
LocalVector<Value> values(isolate, num_attributes);
574+
Local<Context> context = realm->context();
575+
576+
// v8::Object::New() with a prototype argument always creates the object in
577+
// dictionary mode. Null the prototype of an ordinary object instead, so that
578+
// the attributes, which the loader reads for every import, get a fast map.
579+
Local<Object> attributes = Object::New(isolate);
580+
attributes->SetPrototype(context, Null(isolate)).Check();
577581

578582
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
579583
Local<Data> key = raw_attributes->Get(i);
580584
Local<Data> value = raw_attributes->Get(i + 1);
581585
DCHECK(DataIsString(key));
582586
DCHECK(DataIsString(value));
583587

584-
int idx = i / elements_per_attribute;
585-
names[idx] = key.As<String>();
586-
values[idx] = value.As<String>();
588+
attributes
589+
->CreateDataProperty(context, key.As<String>(), value.As<String>())
590+
.Check();
587591
}
588592

589-
Local<Object> attributes = Object::New(
590-
isolate, Null(isolate), names.data(), values.data(), num_attributes);
591-
attributes->SetIntegrityLevel(realm->context(), v8::IntegrityLevel::kFrozen)
592-
.Check();
593+
attributes->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen).Check();
593594
return attributes;
594595
}
595596

0 commit comments

Comments
 (0)