Skip to content

Commit ddc3782

Browse files
perf: optimize assign-depths in compilation
2 parents 4815712 + e83fb23 commit ddc3782

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

lib/Compilation.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,28 +3895,30 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
38953895
assignDepths(modules) {
38963896
const moduleGraph = this.moduleGraph;
38973897

3898-
/** @type {Set<Module | number>} */
3898+
/** @type {Set<Module>} */
38993899
const queue = new Set(modules);
3900-
queue.add(1);
3900+
// Track these in local variables so that queue only has one data type
3901+
let nextDepthAt = queue.size;
39013902
let depth = 0;
39023903

39033904
let i = 0;
39043905
for (const module of queue) {
3905-
i++;
3906-
if (typeof module === "number") {
3907-
depth = module;
3908-
if (queue.size === i) return;
3909-
queue.add(depth + 1);
3910-
} else {
3911-
moduleGraph.setDepth(module, depth);
3912-
for (const { module: refModule } of moduleGraph.getOutgoingConnections(
3913-
module
3914-
)) {
3915-
if (refModule) {
3916-
queue.add(refModule);
3917-
}
3906+
moduleGraph.setDepth(module, depth);
3907+
// Some of these results come from cache, which speeds this up
3908+
const connections = moduleGraph.getOutgoingConnectionsByModule(module);
3909+
// connections will be undefined if there are no outgoing connections
3910+
if (connections) {
3911+
for (const refModule of connections.keys()) {
3912+
if (refModule) queue.add(refModule);
39183913
}
39193914
}
3915+
i++;
3916+
// Since this is a breadth-first search, all modules added to the queue
3917+
// while at depth N will be depth N+1
3918+
if (i >= nextDepthAt) {
3919+
depth++;
3920+
nextDepthAt = queue.size;
3921+
}
39203922
}
39213923
}
39223924

0 commit comments

Comments
 (0)