Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 7a07901
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/Compilation.js
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 7a07901

Please sign in to comment.