Skip to content

Commit 0c0966b

Browse files
perf(ssr): reduce the manifest's chunk graph to edges once
getRequiredChunks walked every chunk module's outgoing connections per chunk, so a module shared by N chunks had its connections iterated N times. Collapse the module graph to chunk-to-chunk edges in a single pass and walk those instead: 29.1ms -> 23.4ms on a 201-chunk build whose 800 shared modules each land in 200 chunks.
1 parent fc9b46f commit 0c0966b

1 file changed

Lines changed: 54 additions & 19 deletions

File tree

‎lib/SSRManifestPlugin.js‎

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,67 @@ const areLoadedTogether = (a, b) => {
4545
return false;
4646
};
4747

48+
/**
49+
* Reduces the module graph to edges between chunks fetched together, walking
50+
* each module's connections once rather than once per chunk containing it.
51+
* @param {Iterable<Module>} modules the compilation's modules
52+
* @param {ChunkGraph} chunkGraph the chunk graph
53+
* @param {ModuleGraph} moduleGraph the module graph
54+
* @returns {Map<Chunk, Set<Chunk>>} the chunks each chunk depends on
55+
*/
56+
const getChunkDependencies = (modules, chunkGraph, moduleGraph) => {
57+
/** @type {Map<Chunk, Set<Chunk>>} */
58+
const dependencies = new Map();
59+
/** @type {Set<Chunk>} */
60+
const targets = new Set();
61+
62+
for (const module of modules) {
63+
targets.clear();
64+
for (const connection of moduleGraph.getOutgoingConnections(module)) {
65+
const dependency = connection.module;
66+
if (dependency === null) continue;
67+
for (const target of chunkGraph.getModuleChunksIterable(dependency)) {
68+
targets.add(target);
69+
}
70+
}
71+
if (targets.size === 0) continue;
72+
73+
for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
74+
/** @type {Set<Chunk> | undefined} */
75+
let edges;
76+
for (const target of targets) {
77+
if (target === chunk) continue;
78+
if (!areLoadedTogether(target, chunk)) continue;
79+
if (edges === undefined) {
80+
edges = dependencies.get(chunk);
81+
if (edges === undefined) dependencies.set(chunk, (edges = new Set()));
82+
}
83+
edges.add(target);
84+
}
85+
}
86+
}
87+
88+
return dependencies;
89+
};
90+
4891
/**
4992
* Collects the chunks the browser needs before a chunk's modules can run: the
5093
* split-out chunks it depends on, and any async ancestor it is reached through.
5194
* Without them a consumer preloading a rendered module would still waterfall.
5295
* Only follows dependencies, never importers, so a shared chunk does not drag
5396
* in the routes that happen to use it.
5497
* @param {Chunk} chunk the chunk a rendered module lives in
55-
* @param {ChunkGraph} chunkGraph the chunk graph
56-
* @param {ModuleGraph} moduleGraph the module graph
98+
* @param {Map<Chunk, Set<Chunk>>} dependencies the precomputed chunk edges
5799
* @returns {Set<Chunk>} the chunks to list for that module
58100
*/
59-
const getRequiredChunks = (chunk, chunkGraph, moduleGraph) => {
101+
const getRequiredChunks = (chunk, dependencies) => {
60102
/** @type {Set<Chunk>} */
61103
const chunks = new Set([chunk]);
62104

63105
for (const current of chunks) {
64-
for (const module of chunkGraph.getChunkModulesIterable(current)) {
65-
for (const connection of moduleGraph.getOutgoingConnections(module)) {
66-
const dependency = connection.module;
67-
if (dependency === null) continue;
68-
for (const target of chunkGraph.getModuleChunksIterable(dependency)) {
69-
if (chunks.has(target)) continue;
70-
if (!areLoadedTogether(target, current)) continue;
71-
chunks.add(target);
72-
}
73-
}
74-
}
106+
const edges = dependencies.get(current);
107+
if (edges === undefined) continue;
108+
for (const target of edges) chunks.add(target);
75109
}
76110

77111
/** @type {Set<ChunkGroup>} */
@@ -134,6 +168,11 @@ class SSRManifestPlugin {
134168
// build-time manifest cannot do; emit root-absolute paths instead so
135169
// consumers can inject them from any route (same as `ManifestPlugin`).
136170
const base = publicPath === "auto" ? "/" : publicPath;
171+
const dependencies = getChunkDependencies(
172+
compilation.modules,
173+
chunkGraph,
174+
moduleGraph
175+
);
137176

138177
/** @type {Map<string, Set<string>>} */
139178
const manifest = new Map();
@@ -143,11 +182,7 @@ class SSRManifestPlugin {
143182

144183
/** @type {string[]} */
145184
const files = [];
146-
for (const required of getRequiredChunks(
147-
chunk,
148-
chunkGraph,
149-
moduleGraph
150-
)) {
185+
for (const required of getRequiredChunks(chunk, dependencies)) {
151186
for (const file of required.files) files.push(base + file);
152187
// Source maps are debug-only; keep them out of the preload manifest.
153188
for (const file of required.auxiliaryFiles) {

0 commit comments

Comments
 (0)