Skip to content

Commit 619d978

Browse files
authored
perf(performance): ask each chunk once what is loaded where it runs (#21779)
`getLoadedChunks` depends on its chunk alone, and was walked again for every module in it — on a hub chunk 30 entrypoints reach, 330 walks where 31 answer the question. Memoized per chunk in the hook that builds it, so the map goes with it rather than outliving the seal. Bounded work, not a build-time win: the hook is well under 1% of the build, and where every module holding an `import()` has a chunk of its own the map never pays. It earns its keep only on a shared chunk (4.79ms -> 1.40ms on that fixture, medians of 9 interleaved builds, spreads apart).
1 parent 4b389bc commit 619d978

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎lib/performance/RedundantDynamicImportsPlugin.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class RedundantDynamicImportsPlugin {
6161
* @param {Chunk} chunk the chunk the importer runs in
6262
* @returns {Set<Chunk>} the chunks loaded before it runs
6363
*/
64-
const getLoadedChunks = (chunk) => {
64+
const computeLoadedChunks = (chunk) => {
6565
const chunks = new Set([chunk]);
6666
/** @type {Set<ChunkGroup>} */
6767
const entrypoints = new Set();
@@ -117,6 +117,26 @@ class RedundantDynamicImportsPlugin {
117117
return chunks;
118118
};
119119

120+
// The answer depends on the chunk alone, and every module sharing one
121+
// asks the same question. Dropped with the hook it is built in.
122+
/** @type {Map<Chunk, Set<Chunk>>} */
123+
const loadedChunks = new Map();
124+
125+
/**
126+
* @param {Chunk} chunk the chunk the importer runs in
127+
* @returns {Set<Chunk>} the chunks loaded before it runs
128+
*/
129+
const getLoadedChunks = (chunk) => {
130+
let loaded = loadedChunks.get(chunk);
131+
132+
if (loaded === undefined) {
133+
loaded = computeLoadedChunks(chunk);
134+
loadedChunks.set(chunk, loaded);
135+
}
136+
137+
return loaded;
138+
};
139+
120140
/**
121141
* @param {DependenciesBlock} block the block to walk
122142
* @param {Module} importer the module the block belongs to

0 commit comments

Comments
 (0)