lib: convert WeakMaps in cjs loader with private symbol properties · nodejs/node@f6145aa · GitHub
Skip to content

Commit f6145aa

Browse files
legendecasaduh95
authored andcommitted
lib: convert WeakMaps in cjs loader with private symbol properties
Symbol properties are typically more GC-efficient than using WeakMaps, since WeakMap requires ephemeron GC. `module[kModuleExportNames]` would be easier to read than `importedCJSCache.get(module).exportNames` as well. PR-URL: #52095 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 44ee04c commit f6145aa

3 files changed

Lines changed: 79 additions & 54 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 64 additions & 43 deletions

lib/internal/modules/esm/translators.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ const {
4343
stripBOM,
4444
} = require('internal/modules/helpers');
4545
const {
46-
cjsExportsCache,
47-
importedCJSCache,
4846
kIsCachedByESMLoader,
4947
Module: CJSModule,
48+
kModuleSource,
49+
kModuleExport,
50+
kModuleExportNames,
5051
} = require('internal/modules/cjs/loader');
5152
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
5253
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
@@ -285,9 +286,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
285286
}
286287

287288
let exports;
288-
if (cjsExportsCache.has(module)) {
289-
exports = cjsExportsCache.get(module);
290-
cjsExportsCache.delete(module);
289+
if (module[kModuleExport] !== undefined) {
290+
exports = module[kModuleExport];
291+
module[kModuleExport] = undefined;
291292
} else {
292293
({ exports } = module);
293294
}
@@ -366,18 +367,16 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
366367
function cjsPreparseModuleExports(filename, source) {
367368
// TODO: Do we want to keep hitting the user mutable CJS loader here?
368369
let module = CJSModule._cache[filename];
369-
if (module) {
370-
const cached = importedCJSCache.get(module);
371-
if (cached) {
372-
return { module, exportNames: cached.exportNames };
373-
}
370+
if (module && module[kModuleExportNames] !== undefined) {
371+
return { module, exportNames: module[kModuleExportNames] };
374372
}
375373
const loaded = Boolean(module);
376374
if (!loaded) {
377375
module = new CJSModule(filename);
378376
module.filename = filename;
379377
module.paths = CJSModule._nodeModulePaths(module.path);
380378
module[kIsCachedByESMLoader] = true;
379+
module[kModuleSource] = source;
381380
CJSModule._cache[filename] = module;
382381
}
383382

@@ -392,7 +391,7 @@ function cjsPreparseModuleExports(filename, source) {
392391
const exportNames = new SafeSet(new SafeArrayIterator(exports));
393392

394393
// Set first for cycles.
395-
importedCJSCache.set(module, { source, exportNames });
394+
module[kModuleExportNames] = exportNames;
396395

397396
if (reexports.length) {
398397
module.filename = filename;

src/env_properties.h

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)