Skip to content

Commit da595c3

Browse files
committed
module: keep ESM load cache entries in fast mode
Keep per-URL load cache entries in fast-property mode while preserving their null prototype. Assisted-by: Pi Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent e7d8ab5 commit da595c3

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎benchmark/esm/load-cache.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
5+
const bench = common.createBenchmark(main, {
6+
urls: [1, 100],
7+
n: [1e7],
8+
}, {
9+
flags: ['--expose-internals'],
10+
});
11+
12+
function main({ urls, n }) {
13+
const { LoadCache } = require('internal/modules/esm/module_map');
14+
const cache = new LoadCache();
15+
const job = () => {};
16+
const keys = Array.from({ length: urls }, (_, i) => `file:///module-${i}.mjs`);
17+
for (let i = 0; i < keys.length; i++) cache.set(keys[i], undefined, job);
18+
19+
bench.start();
20+
for (let i = 0; i < n; i++) {
21+
if (cache.get(keys[i % keys.length]) !== job) {
22+
throw new Error('Load cache miss');
23+
}
24+
}
25+
bench.end(n);
26+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeSort,
77
JSONStringify,
88
ObjectKeys,
9+
ObjectSetPrototypeOf,
910
SafeMap,
1011
} = primordials;
1112
const { kImplicitTypeAttribute } = require('internal/modules/esm/assert');
@@ -108,7 +109,7 @@ class LoadCache extends SafeMap {
108109
debug(`Storing ${url} (${
109110
type === kImplicitTypeAttribute ? 'implicit type' : type
110111
}) in ModuleLoadMap`);
111-
const cachedJobsForUrl = super.get(url) ?? { __proto__: null };
112+
const cachedJobsForUrl = super.get(url) ?? ObjectSetPrototypeOf({}, null);
112113
cachedJobsForUrl[type] = job;
113114
return super.set(url, cachedJobsForUrl);
114115
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
// Flags: --expose-internals
3+
4+
require('../common');
5+
6+
const assert = require('assert');
7+
const { LoadCache } = require('internal/modules/esm/module_map');
8+
9+
const cache = new LoadCache();
10+
const url = 'file:///module.mjs';
11+
const job = () => {};
12+
13+
assert.strictEqual(cache.get(url, '__proto__'), undefined);
14+
assert.strictEqual(cache.has(url, 'constructor'), false);
15+
cache.set(url, '__proto__', job);
16+
assert.strictEqual(cache.get(url, '__proto__'), job);
17+
assert.strictEqual(cache.has(url, '__proto__'), true);
18+
assert.strictEqual(cache.has(url, 'constructor'), false);
19+
cache.set(url, 'constructor', job);
20+
assert.strictEqual(cache.get(url, 'constructor'), job);
21+
cache.delete(url, '__proto__');
22+
assert.strictEqual(cache.get(url, '__proto__'), undefined);

0 commit comments

Comments
 (0)