Skip to content

Commit

Permalink
optimize performance of assignDepth
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 24, 2018
1 parent 15ab027 commit e52f323
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
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 e52f323

Please sign in to comment.