Skip to content

Commit 763ee66

Browse files
perf: grow available modules only below chunk groups that grew
Queue children for the growing phase only when the parent's resulting available modules gained bits, stop each intersection once it reaches minAvailableModules, and disconnect covered modules in the growing phase. Read the watch step from currentWatchStep instead of counting builds.
1 parent 8b3d338 commit 763ee66

2 files changed

Lines changed: 50 additions & 44 deletions

File tree

‎lib/graph/buildChunkGraph.js‎

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const getAsyncDependencyToInitialChunkError = memoize(() =>
4848
* @property {Set<Module>=} skippedItems modules that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules changes)
4949
* @property {Set<[Module, ModuleGraphConnection[]]>=} skippedModuleConnections referenced modules that where skipped because they were not active in this runtime
5050
* @property {bigint | undefined} resultingAvailableModules set of modules available including modules from this chunk group
51+
* @property {bigint | undefined} connectedAvailableModules resultingAvailableModules last passed to children, to detect when it grows
5152
* @property {Set<ChunkGroupInfo> | undefined} children set of children chunk groups, that will be revisited when resultingAvailableModules changes
5253
* @property {Set<ChunkGroupInfo> | undefined} parents set of parent chunk groups, whose resultingAvailableModules intersect to minAvailableModules
5354
* @property {Set<ChunkGroupInfo> | undefined} availableSources set of chunk groups that are the source for minAvailableModules
@@ -80,9 +81,6 @@ const getAsyncDependencyToInitialChunkError = memoize(() =>
8081
const ZERO_BIGINT = BigInt(0);
8182
const ONE_BIGINT = BigInt(1);
8283

83-
// Connect entry for a known child whose parent is outdated: no block to walk
84-
const OUTDATED_RECONNECT = Symbol("outdated reconnect");
85-
8684
/**
8785
* Checks whether this object is ordinal set in mask.
8886
* @param {bigint} mask The mask to test
@@ -461,7 +459,7 @@ const visitModules = (
461459
/** @type {QueueItem[]} */
462460
let queue = [];
463461

464-
/** @typedef {Set<[ChunkGroupInfo, QueueItem | typeof OUTDATED_RECONNECT]>} ConnectList */
462+
/** @typedef {Set<[ChunkGroupInfo, QueueItem | null]>} ConnectList */
465463
/** @type {Map<ChunkGroupInfo, ConnectList>} */
466464
const queueConnect = new Map();
467465
/** @type {Set<ChunkGroupInfo>} */
@@ -487,6 +485,7 @@ const visitModules = (
487485
availableModulesToBeMerged: [],
488486
skippedItems: undefined,
489487
resultingAvailableModules: undefined,
488+
connectedAvailableModules: undefined,
490489
children: undefined,
491490
parents: undefined,
492491
availableSources: undefined,
@@ -552,7 +551,7 @@ const visitModules = (
552551

553552
/** @type {Set<ChunkGroupInfo>} */
554553
const outdatedChunkGroupInfo = new Set();
555-
/** @type {Set<[ChunkGroupInfo, QueueItem | typeof OUTDATED_RECONNECT]>} */
554+
/** @type {Set<[ChunkGroupInfo, QueueItem | null]>} */
556555
const chunkGroupsForMerging = new Set();
557556
/** @type {QueueItem[]} */
558557
let queueDelayed = [];
@@ -620,6 +619,7 @@ const visitModules = (
620619
availableModulesToBeMerged: [],
621620
skippedItems: undefined,
622621
resultingAvailableModules: undefined,
622+
connectedAvailableModules: undefined,
623623
children: undefined,
624624
parents: undefined,
625625
availableSources: undefined,
@@ -722,6 +722,7 @@ const visitModules = (
722722
availableModulesToBeMerged: [],
723723
skippedItems: undefined,
724724
resultingAvailableModules: undefined,
725+
connectedAvailableModules: undefined,
725726
children: undefined,
726727
parents: undefined,
727728
availableSources: undefined,
@@ -1074,6 +1075,19 @@ const visitModules = (
10741075
// 2. Calculate resulting available modules
10751076
const resultingAvailableModules =
10761077
calculateResultingAvailableModules(chunkGroupInfo);
1078+
const connectedAvailableModules =
1079+
chunkGroupInfo.connectedAvailableModules;
1080+
// Merging only intersects, so when this grew all children need the growing phase
1081+
if (
1082+
connectedAvailableModules !== undefined &&
1083+
resultingAvailableModules !== connectedAvailableModules &&
1084+
(resultingAvailableModules & ~connectedAvailableModules) !== ZERO_BIGINT
1085+
) {
1086+
for (const child of chunkGroupInfo.children) {
1087+
if (!child.chunkGroup.isInitial()) chunkGroupsForGrowing.add(child);
1088+
}
1089+
}
1090+
chunkGroupInfo.connectedAvailableModules = resultingAvailableModules;
10771091

10781092
const runtime = chunkGroupInfo.runtime;
10791093

@@ -1085,13 +1099,6 @@ const visitModules = (
10851099
target.parents.add(chunkGroupInfo);
10861100
target.availableModulesToBeMerged.push(resultingAvailableModules);
10871101
chunkGroupsForMerging.add([target, queueItem]);
1088-
if (
1089-
queueItem === OUTDATED_RECONNECT &&
1090-
!target.chunkGroup.isInitial()
1091-
) {
1092-
// Merging only intersects, so a grown parent needs the growing phase.
1093-
chunkGroupsForGrowing.add(target);
1094-
}
10951102
const oldRuntime = target.runtime;
10961103
const newRuntime = mergeRuntime(oldRuntime, runtime);
10971104
if (oldRuntime !== newRuntime) {
@@ -1136,7 +1143,7 @@ const visitModules = (
11361143
outdatedChunkGroupInfo.add(info);
11371144
}
11381145

1139-
if (queueItem !== OUTDATED_RECONNECT) {
1146+
if (queueItem !== null) {
11401147
let blocks = blocksByChunkGroupInfo.get(info);
11411148
if (blocks === undefined) {
11421149
blocks = new Set();
@@ -1192,11 +1199,31 @@ const visitModules = (
11921199
availableModules === undefined
11931200
? resultingAvailableModules
11941201
: availableModules & resultingAvailableModules;
1202+
// Each parent covers minAvailableModules, so the intersection cannot drop below it
1203+
if (availableModules === info.minAvailableModules) break;
11951204
}
1196-
if (availableModules !== info.minAvailableModules) {
1197-
info.minAvailableModules = availableModules;
1198-
info.resultingAvailableModules = undefined;
1199-
outdatedChunkGroupInfo.add(info);
1205+
if (availableModules === info.minAvailableModules) continue;
1206+
info.minAvailableModules = availableModules;
1207+
info.resultingAvailableModules = undefined;
1208+
outdatedChunkGroupInfo.add(info);
1209+
// Reverse of revisiting skipped items: move the own modules now available
1210+
// to skippedItems, where they are added back if it shrinks again
1211+
for (const chunk of info.chunkGroup.chunks) {
1212+
let chunkMask = /** @type {bigint} */ (maskByChunk.get(chunk));
1213+
const coveredModules =
1214+
chunkMask & /** @type {bigint} */ (availableModules);
1215+
if (coveredModules === ZERO_BIGINT) continue;
1216+
if (info.skippedItems === undefined) {
1217+
info.skippedItems = new Set();
1218+
}
1219+
for (const module of chunkGraph.getChunkModules(chunk)) {
1220+
const ordinal = getModuleOrdinal(module);
1221+
if (!isOrdinalSetInMask(coveredModules, ordinal)) continue;
1222+
chunkGraph.disconnectChunkAndModule(chunk, module);
1223+
chunkMask &= ~(ONE_BIGINT << BigInt(ordinal));
1224+
info.skippedItems.add(module);
1225+
}
1226+
maskByChunk.set(chunk, chunkMask);
12001227
}
12011228
}
12021229
chunkGroupsForGrowing.clear();
@@ -1227,28 +1254,7 @@ const visitModules = (
12271254
}
12281255
}
12291256

1230-
// 2. Reverse of step 1: minAvailableModules grew, so move the own modules it now covers
1231-
// to skippedItems, where step 1 adds them back if it shrinks again.
1232-
for (const chunk of info.chunkGroup.chunks) {
1233-
let chunkMask = /** @type {bigint} */ (maskByChunk.get(chunk));
1234-
const availables =
1235-
chunkMask & /** @type {bigint} */ (info.minAvailableModules);
1236-
if (availables === ZERO_BIGINT) continue;
1237-
if (info.skippedItems === undefined) {
1238-
info.skippedItems = new Set();
1239-
}
1240-
for (const module of chunkGraph.getChunkModules(chunk)) {
1241-
const ordinal = getModuleOrdinal(module);
1242-
if (!isOrdinalSetInMask(availables, ordinal)) continue;
1243-
// Already available in all parent chunk groups, so disconnect it
1244-
chunkGraph.disconnectChunkAndModule(chunk, module);
1245-
chunkMask &= ~(ONE_BIGINT << BigInt(ordinal));
1246-
info.skippedItems.add(module);
1247-
}
1248-
maskByChunk.set(chunk, chunkMask);
1249-
}
1250-
1251-
// 3. Reconsider skipped connections
1257+
// 2. Reconsider skipped connections
12521258
if (info.skippedModuleConnections !== undefined) {
12531259
const minAvailableModules =
12541260
/** @type {bigint} */
@@ -1280,7 +1286,7 @@ const visitModules = (
12801286
}
12811287
}
12821288

1283-
// 4. Reconsider children chunk groups
1289+
// 3. Reconsider children chunk groups
12841290
if (info.children !== undefined && info.children.size > 0) {
12851291
statChildChunkGroupsReconnected += info.children.size;
12861292
let connectList = queueConnect.get(info);
@@ -1290,11 +1296,11 @@ const visitModules = (
12901296
queueConnect.set(info, connectList);
12911297
}
12921298
for (const cgi of info.children) {
1293-
connectList.add([cgi, OUTDATED_RECONNECT]);
1299+
connectList.add([cgi, null]);
12941300
}
12951301
}
12961302

1297-
// 5. Reconsider chunk groups for combining
1303+
// 4. Reconsider chunk groups for combining
12981304
if (info.availableChildren !== undefined) {
12991305
for (const cgi of info.availableChildren) {
13001306
chunkGroupsForCombining.add(cgi);

‎test/watchCases/chunks/available-modules/webpack.config.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const { WebpackError } = require("../../../../");
4+
const currentWatchStep = require("../../../helpers/currentWatchStep");
45

56
/** @type {import("../../../../").Configuration} */
67
module.exports = {
@@ -14,10 +15,9 @@ module.exports = {
1415
plugins: [
1516
{
1617
apply(compiler) {
17-
let build = 0;
1818
compiler.hooks.thisCompilation.tap("Test", (compilation) => {
1919
// Only step 1 has no late m.js in "parent", so "child" keeps its copy.
20-
const expected = build++ === 1 ? 1 : 0;
20+
const expected = currentWatchStep.step === "1" ? 1 : 0;
2121
compilation.hooks.afterChunks.tap("Test", () => {
2222
const child =
2323
/** @type {import("../../../../").ChunkGroup} */

0 commit comments

Comments
 (0)