Skip to content

Commit 8b3d338

Browse files
committed
feat: propagate available module growth during code splitting
1 parent d2abe4d commit 8b3d338

62 files changed

Lines changed: 831 additions & 47 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Remove modules an async chunk inherits from a named parent chunk grown later.

‎lib/graph/buildChunkGraph.js‎

Lines changed: 119 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ const getAsyncDependencyToInitialChunkError = memoize(() =>
4343
* @typedef {object} ChunkGroupInfo
4444
* @property {ChunkGroup} chunkGroup the chunk group
4545
* @property {RuntimeSpec} runtime the runtimes
46-
* @property {boolean} initialized is this chunk group initialized
4746
* @property {bigint | undefined} minAvailableModules current minimal set of modules available at this point
4847
* @property {bigint[]} availableModulesToBeMerged enqueued updates to the minimal set of available modules
49-
* @property {Set<Module>=} skippedItems modules that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules is shrinking)
48+
* @property {Set<Module>=} skippedItems modules that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules changes)
5049
* @property {Set<[Module, ModuleGraphConnection[]]>=} skippedModuleConnections referenced modules that where skipped because they were not active in this runtime
5150
* @property {bigint | undefined} resultingAvailableModules set of modules available including modules from this chunk group
52-
* @property {Set<ChunkGroupInfo> | undefined} children set of children chunk groups, that will be revisited when availableModules shrink
51+
* @property {Set<ChunkGroupInfo> | undefined} children set of children chunk groups, that will be revisited when resultingAvailableModules changes
52+
* @property {Set<ChunkGroupInfo> | undefined} parents set of parent chunk groups, whose resultingAvailableModules intersect to minAvailableModules
5353
* @property {Set<ChunkGroupInfo> | undefined} availableSources set of chunk groups that are the source for minAvailableModules
5454
* @property {Set<ChunkGroupInfo> | undefined} availableChildren set of chunk groups which depend on the this chunk group as availableSource
5555
* @property {number} preOrderIndex next pre order index
@@ -80,6 +80,9 @@ const getAsyncDependencyToInitialChunkError = memoize(() =>
8080
const ZERO_BIGINT = BigInt(0);
8181
const ONE_BIGINT = BigInt(1);
8282

83+
// Connect entry for a known child whose parent is outdated: no block to walk
84+
const OUTDATED_RECONNECT = Symbol("outdated reconnect");
85+
8386
/**
8487
* Checks whether this object is ordinal set in mask.
8588
* @param {bigint} mask The mask to test
@@ -432,7 +435,7 @@ const visitModules = (
432435
const blockChunkGroups = new Map();
433436

434437
/** @type {Map<ChunkGroupInfo, Set<DependenciesBlock>>} */
435-
const blocksByChunkGroups = new Map();
438+
const blocksByChunkGroupInfo = new Map();
436439

437440
/** @typedef {Map<string, ChunkGroupInfo>} NamedChunkGroup */
438441

@@ -458,11 +461,13 @@ const visitModules = (
458461
/** @type {QueueItem[]} */
459462
let queue = [];
460463

461-
/** @typedef {Set<[ChunkGroupInfo, QueueItem | null]>} ConnectList */
464+
/** @typedef {Set<[ChunkGroupInfo, QueueItem | typeof OUTDATED_RECONNECT]>} ConnectList */
462465
/** @type {Map<ChunkGroupInfo, ConnectList>} */
463466
const queueConnect = new Map();
464467
/** @type {Set<ChunkGroupInfo>} */
465468
const chunkGroupsForCombining = new Set();
469+
/** @type {Set<ChunkGroupInfo>} */
470+
const chunkGroupsForGrowing = new Set();
466471

467472
// Fill queue with entrypoint modules
468473
// Create ChunkGroupInfo for entrypoints
@@ -476,14 +481,14 @@ const visitModules = (
476481
const chunkGroupInfo = {
477482
depModule: null,
478483
circular: false,
479-
initialized: false,
480484
chunkGroup,
481485
runtime,
482486
minAvailableModules: undefined,
483487
availableModulesToBeMerged: [],
484488
skippedItems: undefined,
485489
resultingAvailableModules: undefined,
486490
children: undefined,
491+
parents: undefined,
487492
availableSources: undefined,
488493
availableChildren: undefined,
489494
preOrderIndex: 0,
@@ -547,7 +552,7 @@ const visitModules = (
547552

548553
/** @type {Set<ChunkGroupInfo>} */
549554
const outdatedChunkGroupInfo = new Set();
550-
/** @type {Set<[ChunkGroupInfo, QueueItem | null]>} */
555+
/** @type {Set<[ChunkGroupInfo, QueueItem | typeof OUTDATED_RECONNECT]>} */
551556
const chunkGroupsForMerging = new Set();
552557
/** @type {QueueItem[]} */
553558
let queueDelayed = [];
@@ -608,7 +613,6 @@ const visitModules = (
608613
depModule,
609614
circular: b.circular,
610615
chunkGroup: entrypoint,
611-
initialized: false,
612616
runtime:
613617
entrypoint.options.runtime ||
614618
/** @type {string | undefined} */ (entrypoint.name),
@@ -617,6 +621,7 @@ const visitModules = (
617621
skippedItems: undefined,
618622
resultingAvailableModules: undefined,
619623
children: undefined,
624+
parents: undefined,
620625
availableSources: undefined,
621626
availableChildren: undefined,
622627
preOrderIndex: 0,
@@ -711,14 +716,14 @@ const visitModules = (
711716
cgi = {
712717
depModule,
713718
circular: b.circular,
714-
initialized: false,
715719
chunkGroup: c,
716720
runtime: chunkGroupInfo.runtime,
717721
minAvailableModules: undefined,
718722
availableModulesToBeMerged: [],
719723
skippedItems: undefined,
720724
resultingAvailableModules: undefined,
721725
children: undefined,
726+
parents: undefined,
722727
availableSources: undefined,
723728
availableChildren: undefined,
724729
preOrderIndex: 0,
@@ -964,6 +969,12 @@ const visitModules = (
964969
let chunkMask = /** @type {bigint} */ (maskByChunk.get(chunk));
965970
chunkMask |= ONE_BIGINT << BigInt(moduleOrdinal);
966971
maskByChunk.set(chunk, chunkMask);
972+
if (chunkGroupInfo.resultingAvailableModules !== undefined) {
973+
// A later block reached this chunk group after its children read this value,
974+
// we clear it and mark outdated so the children reconnect and drop it
975+
chunkGroupInfo.resultingAvailableModules = undefined;
976+
outdatedChunkGroupInfo.add(chunkGroupInfo);
977+
}
967978
}
968979
// fallthrough
969980
case ENTER_MODULE: {
@@ -1020,6 +1031,19 @@ const visitModules = (
10201031
}
10211032
};
10221033

1034+
/**
1035+
* Get the modules in the chunks of a chunk group.
1036+
* @param {ChunkGroupInfo} chunkGroupInfo The info object for the chunk group
1037+
* @returns {bigint} The mask of modules in the chunks of the chunk group
1038+
*/
1039+
const getChunkGroupMask = (chunkGroupInfo) => {
1040+
let mask = ZERO_BIGINT;
1041+
for (const chunk of chunkGroupInfo.chunkGroup.chunks) {
1042+
mask |= /** @type {bigint} */ (maskByChunk.get(chunk));
1043+
}
1044+
return mask;
1045+
};
1046+
10231047
/**
10241048
* Calculate resulting available modules.
10251049
* @param {ChunkGroupInfo} chunkGroupInfo The info object for the chunk group
@@ -1030,18 +1054,9 @@ const visitModules = (
10301054
return chunkGroupInfo.resultingAvailableModules;
10311055
}
10321056

1033-
let resultingAvailableModules = /** @type {bigint} */ (
1034-
chunkGroupInfo.minAvailableModules
1035-
);
1036-
1037-
// add the modules from the chunk group to the set
1038-
for (const chunk of chunkGroupInfo.chunkGroup.chunks) {
1039-
const mask = /** @type {bigint} */ (maskByChunk.get(chunk));
1040-
resultingAvailableModules |= mask;
1041-
}
1042-
10431057
return (chunkGroupInfo.resultingAvailableModules =
1044-
resultingAvailableModules);
1058+
/** @type {bigint} */ (chunkGroupInfo.minAvailableModules) |
1059+
getChunkGroupMask(chunkGroupInfo));
10451060
};
10461061

10471062
const processConnectQueue = () => {
@@ -1063,9 +1078,20 @@ const visitModules = (
10631078
const runtime = chunkGroupInfo.runtime;
10641079

10651080
// 3. Update chunk group info
1066-
for (const [target, processBlock] of targets) {
1081+
for (const [target, queueItem] of targets) {
1082+
if (target.parents === undefined) {
1083+
target.parents = new Set();
1084+
}
1085+
target.parents.add(chunkGroupInfo);
10671086
target.availableModulesToBeMerged.push(resultingAvailableModules);
1068-
chunkGroupsForMerging.add([target, processBlock]);
1087+
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+
}
10691095
const oldRuntime = target.runtime;
10701096
const newRuntime = mergeRuntime(oldRuntime, runtime);
10711097
if (oldRuntime !== newRuntime) {
@@ -1083,7 +1109,7 @@ const visitModules = (
10831109
statProcessedChunkGroupsForMerging += chunkGroupsForMerging.size;
10841110

10851111
// Execute the merge
1086-
for (const [info, processBlock] of chunkGroupsForMerging) {
1112+
for (const [info, queueItem] of chunkGroupsForMerging) {
10871113
const availableModulesToBeMerged = info.availableModulesToBeMerged;
10881114
const cachedMinAvailableModules = info.minAvailableModules;
10891115
let minAvailableModules = cachedMinAvailableModules;
@@ -1110,25 +1136,17 @@ const visitModules = (
11101136
outdatedChunkGroupInfo.add(info);
11111137
}
11121138

1113-
if (processBlock) {
1114-
let blocks = blocksByChunkGroups.get(info);
1115-
if (!blocks) {
1116-
blocksByChunkGroups.set(info, (blocks = new Set()));
1139+
if (queueItem !== OUTDATED_RECONNECT) {
1140+
let blocks = blocksByChunkGroupInfo.get(info);
1141+
if (blocks === undefined) {
1142+
blocks = new Set();
1143+
blocksByChunkGroupInfo.set(info, blocks);
11171144
}
11181145

1119-
// Whether to walk block depends on minAvailableModules and input block.
1120-
// We can treat creating chunk group as a function with 2 input, entry block and minAvailableModules
1121-
// If input is the same, we can skip re-walk
1122-
let needWalkBlock = !info.initialized || changed;
1123-
if (!blocks.has(processBlock.block)) {
1124-
needWalkBlock = true;
1125-
blocks.add(processBlock.block);
1126-
}
1127-
1128-
if (needWalkBlock) {
1129-
info.initialized = true;
1130-
queueDelayed.push(processBlock);
1131-
}
1146+
// A walk depends only on the block and minAvailableModules: skip it when both are seen
1147+
const newBlock = !blocks.has(queueItem.block);
1148+
if (newBlock) blocks.add(queueItem.block);
1149+
if (newBlock || changed) queueDelayed.push(queueItem);
11321150
}
11331151
}
11341152
chunkGroupsForMerging.clear();
@@ -1163,6 +1181,27 @@ const visitModules = (
11631181
chunkGroupsForCombining.clear();
11641182
};
11651183

1184+
const processChunkGroupsForGrowing = () => {
1185+
for (const info of chunkGroupsForGrowing) {
1186+
/** @type {bigint | undefined} */
1187+
let availableModules;
1188+
for (const parent of /** @type {Set<ChunkGroupInfo>} */ (info.parents)) {
1189+
const resultingAvailableModules =
1190+
calculateResultingAvailableModules(parent);
1191+
availableModules =
1192+
availableModules === undefined
1193+
? resultingAvailableModules
1194+
: availableModules & resultingAvailableModules;
1195+
}
1196+
if (availableModules !== info.minAvailableModules) {
1197+
info.minAvailableModules = availableModules;
1198+
info.resultingAvailableModules = undefined;
1199+
outdatedChunkGroupInfo.add(info);
1200+
}
1201+
}
1202+
chunkGroupsForGrowing.clear();
1203+
};
1204+
11661205
const processOutdatedChunkGroupInfo = () => {
11671206
statChunkGroupInfoUpdated += outdatedChunkGroupInfo.size;
11681207
// Revisit skipped elements
@@ -1188,7 +1227,28 @@ const visitModules = (
11881227
}
11891228
}
11901229

1191-
// 2. Reconsider skipped connections
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
11921252
if (info.skippedModuleConnections !== undefined) {
11931253
const minAvailableModules =
11941254
/** @type {bigint} */
@@ -1220,7 +1280,7 @@ const visitModules = (
12201280
}
12211281
}
12221282

1223-
// 2. Reconsider children chunk groups
1283+
// 4. Reconsider children chunk groups
12241284
if (info.children !== undefined && info.children.size > 0) {
12251285
statChildChunkGroupsReconnected += info.children.size;
12261286
let connectList = queueConnect.get(info);
@@ -1230,11 +1290,11 @@ const visitModules = (
12301290
queueConnect.set(info, connectList);
12311291
}
12321292
for (const cgi of info.children) {
1233-
connectList.add([cgi, null]);
1293+
connectList.add([cgi, OUTDATED_RECONNECT]);
12341294
}
12351295
}
12361296

1237-
// 3. Reconsider chunk groups for combining
1297+
// 5. Reconsider chunk groups for combining
12381298
if (info.availableChildren !== undefined) {
12391299
for (const cgi of info.availableChildren) {
12401300
chunkGroupsForCombining.add(cgi);
@@ -1247,7 +1307,7 @@ const visitModules = (
12471307

12481308
// Iterative traversal of the Module graph
12491309
// Recursive would be simpler to write but could result in Stack Overflows
1250-
while (queue.length || queueConnect.size) {
1310+
while (queue.length || queueConnect.size || outdatedChunkGroupInfo.size) {
12511311
logger.time("visitModules: visiting");
12521312
processQueue();
12531313
logger.timeAggregateEnd("visitModules: prepare");
@@ -1285,12 +1345,25 @@ const visitModules = (
12851345
queue = queueDelayed.reverse();
12861346
queueDelayed = tempQueue;
12871347
}
1348+
1349+
// Grow only after the shrinking phase finishes, so growth
1350+
// reads each parent's final `resultingAvailableModules`
1351+
if (
1352+
queue.length === 0 &&
1353+
queueConnect.size === 0 &&
1354+
chunkGroupsForCombining.size === 0 &&
1355+
chunkGroupsForGrowing.size > 0
1356+
) {
1357+
logger.time("visitModules: growing available modules");
1358+
processChunkGroupsForGrowing();
1359+
logger.timeEnd("visitModules: growing available modules");
1360+
}
12881361
}
12891362

12901363
for (const info of outdatedOrderIndexChunkGroups) {
12911364
const { chunkGroup, runtime } = info;
12921365

1293-
const blocks = blocksByChunkGroups.get(info);
1366+
const blocks = blocksByChunkGroupInfo.get(info);
12941367

12951368
if (!blocks) {
12961369
continue;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { v } from './m3.js';
2+
export const values = [v];
3+
export const load0 = () => import(/* webpackChunkName: 'g2' */ './a3.js');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const load1 = () => import(/* webpackChunkName: 'g3' */ './a2.js');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const load0 = () => import(/* webpackChunkName: 'g2' */ './a1.js');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { v } from './m3.js';
2+
export const values = [v];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const load0 = () => import(/* webpackChunkName: 'g0' */ './a0.js');
2+
export const load1 = () => import(/* webpackChunkName: 'g1' */ './a1.js');
3+
4+
it('settles availability when a late parent closes a cycle', async () => {
5+
const direct = await load0();
6+
expect(direct.values).toEqual([3]);
7+
expect((await direct.load0()).values).toEqual([3]);
8+
const a1 = await load1();
9+
const a2 = await a1.load1();
10+
const again = await a2.load0();
11+
expect(typeof again.load1).toBe('function');
12+
expect(typeof (await again.load1()).load0).toBe('function');
13+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const v = 3;
2+
export const load = () => import(/* webpackChunkName: 'g1' */ './a0.js');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
module.exports = {
4+
evaluateScriptOnAttached: true,
5+
findBundle() {
6+
return ["main.js"];
7+
}
8+
};

0 commit comments

Comments
 (0)