Skip to content

Commit 1c0d4f7

Browse files
committed
improve Compilation.processDependenciesBlocksForChunkGroups performance
prepare references for modules
1 parent f2e5c1e commit 1c0d4f7

File tree

1 file changed

+75
-41
lines changed

1 file changed

+75
-41
lines changed

lib/Compilation.js

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,61 @@ class Compilation extends Tapable {
11681168
const chunkDependencies = new Map(); // Map<Chunk, Array<{Module, Chunk}>>
11691169
const allCreatedChunkGroups = new Set();
11701170

1171+
// PREPARE
1172+
const blockInfoMap = new Map();
1173+
1174+
const iteratorDependency = d => {
1175+
// We skip Dependencies without Reference
1176+
const ref = d.getReference();
1177+
if (!ref) {
1178+
return;
1179+
}
1180+
// We skip Dependencies without Module pointer
1181+
const refModule = ref.module;
1182+
if (!refModule) {
1183+
return;
1184+
}
1185+
// We skip weak Dependencies
1186+
if (ref.weak) {
1187+
return;
1188+
}
1189+
1190+
blockInfoModules.add(refModule);
1191+
};
1192+
1193+
const iteratorBlockPrepare = b => {
1194+
blockInfoBlocks.push(b);
1195+
blockQueue.push(b);
1196+
};
1197+
1198+
let block, blockQueue, blockInfoModules, blockInfoBlocks;
1199+
for (const module of this.modules) {
1200+
blockQueue = [module];
1201+
while (blockQueue.length > 0) {
1202+
block = blockQueue.pop();
1203+
blockInfoModules = new Set();
1204+
blockInfoBlocks = [];
1205+
1206+
if (block.variables) {
1207+
iterationBlockVariable(block.variables, iteratorDependency);
1208+
}
1209+
1210+
if (block.dependencies) {
1211+
iterationOfArrayCallback(block.dependencies, iteratorDependency);
1212+
}
1213+
1214+
if (block.blocks) {
1215+
iterationOfArrayCallback(block.blocks, iteratorBlockPrepare);
1216+
}
1217+
1218+
const blockInfo = {
1219+
modules: blockInfoModules,
1220+
blocks: blockInfoBlocks
1221+
};
1222+
blockInfoMap.set(block, blockInfo);
1223+
}
1224+
}
1225+
11711226
// PART ONE
11721227

11731228
const blockChunkGroups = new Map();
@@ -1180,7 +1235,7 @@ class Compilation extends Tapable {
11801235
chunkGroup
11811236
}));
11821237

1183-
let module, block, chunk, chunkGroup;
1238+
let module, chunk, chunkGroup;
11841239

11851240
// For each async Block in graph
11861241
const iteratorBlock = b => {
@@ -1227,36 +1282,6 @@ class Compilation extends Tapable {
12271282
});
12281283
};
12291284

1230-
// For each Dependency in the graph
1231-
const iteratorDependency = d => {
1232-
// We skip Dependencies without Reference
1233-
const ref = d.getReference();
1234-
if (!ref) {
1235-
return;
1236-
}
1237-
// We skip Dependencies without Module pointer
1238-
const refModule = ref.module;
1239-
if (!refModule) {
1240-
return;
1241-
}
1242-
// We skip weak Dependencies
1243-
if (ref.weak) {
1244-
return;
1245-
}
1246-
// We connect Module and Chunk when not already done
1247-
if (chunk.addModule(refModule)) {
1248-
refModule.addChunk(chunk);
1249-
1250-
// And enqueue the Module for traversal
1251-
queue.push({
1252-
block: refModule,
1253-
module: refModule,
1254-
chunk,
1255-
chunkGroup
1256-
});
1257-
}
1258-
};
1259-
12601285
// Iterative traversal of the Module graph
12611286
// Recursive would be simpler to write but could result in Stack Overflows
12621287
while (queue.length) {
@@ -1266,18 +1291,27 @@ class Compilation extends Tapable {
12661291
chunk = queueItem.chunk;
12671292
chunkGroup = queueItem.chunkGroup;
12681293

1269-
// Traverse all variables, Dependencies and Blocks
1270-
if (block.variables) {
1271-
iterationBlockVariable(block.variables, iteratorDependency);
1272-
}
1273-
1274-
if (block.dependencies) {
1275-
iterationOfArrayCallback(block.dependencies, iteratorDependency);
1294+
// get prepared block info
1295+
const blockInfo = blockInfoMap.get(block);
1296+
1297+
// Traverse all referenced modules
1298+
for (const refModule of blockInfo.modules) {
1299+
// We connect Module and Chunk when not already done
1300+
if (chunk.addModule(refModule)) {
1301+
refModule.addChunk(chunk);
1302+
1303+
// And enqueue the Module for traversal
1304+
queue.push({
1305+
block: refModule,
1306+
module: refModule,
1307+
chunk,
1308+
chunkGroup
1309+
});
1310+
}
12761311
}
12771312

1278-
if (block.blocks) {
1279-
iterationOfArrayCallback(block.blocks, iteratorBlock);
1280-
}
1313+
// Traverse all Blocks
1314+
iterationOfArrayCallback(blockInfo.blocks, iteratorBlock);
12811315
}
12821316

12831317
// PART TWO

0 commit comments

Comments
 (0)