From 43e6622ae68e5ef54cffda2aa67010f83f4e6e8e Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 21 Nov 2026 07:38:30 +0100 Subject: [PATCH] esm: avoid dictionary-mode objects in import() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 Reviewed-By: Antoine du Hamel Reviewed-By: Chengzhong Wu Reviewed-By: James M Snell --- lib/internal/modules/esm/loader.js | 12 +++++++----- src/module_wrap.cc | 21 +++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 80d6f875944..ff99764566a 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -520,6 +520,12 @@ class ModuleLoader { * @returns {Promise} */ 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); } /** diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 5c073ce7ded..f95313efb58 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -571,9 +571,13 @@ static Local createImportAttributesContainer( Local 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 names(isolate, num_attributes); - LocalVector values(isolate, num_attributes); + Local 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 attributes = Object::New(isolate); + attributes->SetPrototype(context, Null(isolate)).Check(); for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) { Local key = raw_attributes->Get(i); @@ -581,15 +585,12 @@ static Local createImportAttributesContainer( DCHECK(DataIsString(key)); DCHECK(DataIsString(value)); - int idx = i / elements_per_attribute; - names[idx] = key.As(); - values[idx] = value.As(); + attributes + ->CreateDataProperty(context, key.As(), value.As()) + .Check(); } - Local 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; }