Skip to content

Commit e52f323

Browse files
committed
optimize performance of assignDepth
1 parent 15ab027 commit e52f323

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

lib/Compilation.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,45 +1007,44 @@ class Compilation extends Tapable {
10071007
}
10081008

10091009
assignDepth(module) {
1010-
const assignDepthToModule = (module, depth) => {
1011-
// enter module
1012-
if(typeof module.depth === "number" && module.depth <= depth) return;
1013-
module.depth = depth;
1010+
const queue = new Set([module]);
1011+
let depth;
10141012

1015-
// enter it as block
1016-
assignDepthToDependencyBlock(module, depth + 1);
1013+
module.depth = 0;
1014+
1015+
const enqueueJob = module => {
1016+
const d = module.depth;
1017+
if(typeof d === "number" && d <= depth) return;
1018+
queue.add(module);
1019+
module.depth = depth;
10171020
};
10181021

10191022
const assignDepthToDependency = (dependency, depth) => {
10201023
if(dependency.module) {
1021-
queue.push(() => assignDepthToModule(dependency.module, depth));
1024+
enqueueJob(dependency.module);
10221025
}
10231026
};
10241027

1025-
const assignDepthToDependencyBlock = (block, depth) => {
1026-
const iteratorDependency = d => assignDepthToDependency(d, depth);
1027-
1028-
const iteratorBlock = b => assignDepthToDependencyBlock(b, depth);
1029-
1028+
const assignDepthToDependencyBlock = block => {
10301029
if(block.variables) {
1031-
iterationBlockVariable(block.variables, iteratorDependency);
1030+
iterationBlockVariable(block.variables, assignDepthToDependency);
10321031
}
10331032

10341033
if(block.dependencies) {
1035-
iterationOfArrayCallback(block.dependencies, iteratorDependency);
1034+
iterationOfArrayCallback(block.dependencies, assignDepthToDependency);
10361035
}
10371036

10381037
if(block.blocks) {
1039-
iterationOfArrayCallback(block.blocks, iteratorBlock);
1038+
iterationOfArrayCallback(block.blocks, assignDepthToDependencyBlock);
10401039
}
10411040
};
10421041

1043-
const queue = [() => {
1044-
assignDepthToModule(module, 0);
1045-
}];
1042+
for(module of queue) {
1043+
queue.delete(module);
1044+
depth = module.depth;
10461045

1047-
while(queue.length) {
1048-
queue.pop()();
1046+
depth++;
1047+
assignDepthToDependencyBlock(module);
10491048
}
10501049
}
10511050

0 commit comments

Comments
 (0)