Skip to content
Permalink
Browse files
Merge pull request #6563 from webpack/performance/assign-depth
optimize performance of assignDepth
  • Loading branch information
sokra committed Feb 24, 2018
2 parents c7eb895 + e52f323 commit 7a07901cab62375cc4032d71c70d3a1d0564c683
Showing with 19 additions and 20 deletions.
  1. +19 −20 lib/Compilation.js
@@ -1007,45 +1007,44 @@ class Compilation extends Tapable {
}

assignDepth(module) {
const assignDepthToModule = (module, depth) => {
// enter module
if(typeof module.depth === "number" && module.depth <= depth) return;
module.depth = depth;
const queue = new Set([module]);
let depth;

// enter it as block
assignDepthToDependencyBlock(module, depth + 1);
module.depth = 0;

const enqueueJob = module => {
const d = module.depth;
if(typeof d === "number" && d <= depth) return;
queue.add(module);
module.depth = depth;
};

const assignDepthToDependency = (dependency, depth) => {
if(dependency.module) {
queue.push(() => assignDepthToModule(dependency.module, depth));
enqueueJob(dependency.module);
}
};

const assignDepthToDependencyBlock = (block, depth) => {
const iteratorDependency = d => assignDepthToDependency(d, depth);

const iteratorBlock = b => assignDepthToDependencyBlock(b, depth);

const assignDepthToDependencyBlock = block => {
if(block.variables) {
iterationBlockVariable(block.variables, iteratorDependency);
iterationBlockVariable(block.variables, assignDepthToDependency);
}

if(block.dependencies) {
iterationOfArrayCallback(block.dependencies, iteratorDependency);
iterationOfArrayCallback(block.dependencies, assignDepthToDependency);
}

if(block.blocks) {
iterationOfArrayCallback(block.blocks, iteratorBlock);
iterationOfArrayCallback(block.blocks, assignDepthToDependencyBlock);
}
};

const queue = [() => {
assignDepthToModule(module, 0);
}];
for(module of queue) {
queue.delete(module);
depth = module.depth;

while(queue.length) {
queue.pop()();
depth++;
assignDepthToDependencyBlock(module);
}
}

0 comments on commit 7a07901

Please sign in to comment.