Skip to content

Commit

Permalink
more performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 14, 2016
1 parent da29d21 commit f4ab7fc
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 146 deletions.
4 changes: 2 additions & 2 deletions benchmark/createFixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ try {
fs.mkdirSync(fixtures);
} catch(e) {}

for(var i = 0; i < 1000; i++) {
for(var i = 0; i < 10000; i++) {
var source = [];
if(i > 8)
source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".js") + ");");
Expand All @@ -21,7 +21,7 @@ for(var i = 0; i < 1000; i++) {
fs.writeFileSync(path.join(fixtures, i + ".js"), source.join("\n"), "utf-8");
}

for(var i = 0; i < 1000; i++) {
for(var i = 0; i < 10000; i++) {
var source = [];
source.push("require.ensure([], function(require) {");
if(i > 8)
Expand Down
2 changes: 1 addition & 1 deletion bin/convert-argv.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var path = require("path");
var fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
var resolve = require("enhanced-resolve");
var interpret = require("interpret");
var WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");

Expand Down Expand Up @@ -249,6 +248,7 @@ module.exports = function(yargs, argv, convertOptions) {

var path;
try {
var resolve = require("enhanced-resolve");
path = resolve.sync(process.cwd(), name);
} catch(e) {
console.log("Cannot resolve plugin " + name + ".");
Expand Down
6 changes: 5 additions & 1 deletion lib/Chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
};

function byId(a, b) {
return a.id - b.id;
if(a.id < b.id) return -1;
if(b.id < a.id) return 1;
return 0;
}

Chunk.prototype.sortItems = function() {
Expand All @@ -332,6 +334,8 @@ Chunk.prototype.sortItems = function() {
if(origin.reasons)
origin.reasons.sort();
});
this.parents.sort(byId);
this.chunks.sort(byId);
};

Chunk.prototype.toString = function() {
Expand Down
149 changes: 106 additions & 43 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Compilation.prototype.findModule = function(identifier) {

Compilation.prototype.buildModule = function(module, optional, origin, dependencies, thisCallback) {
var _this = this;
_this.applyPlugins("build-module", module);
_this.applyPlugins1("build-module", module);
if(module.building) return module.building.push(thisCallback);
var building = module.building = [thisCallback];

Expand All @@ -142,10 +142,10 @@ Compilation.prototype.buildModule = function(module, optional, origin, dependenc
}, this);
module.dependencies.sort(Dependency.compare);
if(err) {
_this.applyPlugins("failed-module", module, err);
_this.applyPlugins2("failed-module", module, err);
return callback(err);
}
_this.applyPlugins("succeed-module", module);
_this.applyPlugins1("succeed-module", module);
return callback();
});
};
Expand Down Expand Up @@ -509,7 +509,7 @@ Compilation.prototype.finish = function finish() {
};

Compilation.prototype.unseal = function unseal() {
this.applyPlugins("unseal");
this.applyPlugins0("unseal");
this.chunks.length = 0;
this.namedChunks = {};
this.additionalChunkAssets.length = 0;
Expand All @@ -533,13 +533,8 @@ Compilation.prototype.seal = function seal(callback) {
chunk.addModule(module);
module.addChunk(chunk);
chunk.entryModule = module;
if(typeof module.index !== "number") {
module.index = self.nextFreeModuleIndex++;
}
self.assignIndex(module);
self.processDependenciesBlockForChunk(module, chunk);
if(typeof module.index2 !== "number") {
module.index2 = self.nextFreeModuleIndex2++;
}
}, self);
self.sortModules(self.modules);
self.applyPlugins0("optimize");
Expand Down Expand Up @@ -583,7 +578,7 @@ Compilation.prototype.seal = function seal(callback) {
self.applyPlugins1("optimize-chunk-ids", self.chunks);
self.applyPlugins1("after-optimize-chunk-ids", self.chunks);

self.sortItemswithChunkIds();
self.sortItemsWithChunkIds();

if(shouldRecord)
self.applyPlugins2("record-modules", self.modules, self.records);
Expand Down Expand Up @@ -676,47 +671,112 @@ Compilation.prototype.addChunk = function addChunk(name, module, loc) {
return chunk;
};

Compilation.prototype.processDependenciesBlockForChunk = function processDependenciesBlockForChunk(block, chunk) {
if(block.variables) {
block.variables.forEach(function(v) {
v.dependencies.forEach(iteratorDependency, this);
}, this);
Compilation.prototype.assignIndex = function assignIndex(module) {
var _this = this;
function assignIndexToModule(module) {
// enter module
if(typeof module.index !== "number") {
module.index = _this.nextFreeModuleIndex++;

queue.push(function() {
// leave module
module.index2 = _this.nextFreeModuleIndex2++;
});

// enter it as block
assignIndexToDependencyBlock(module);
}
}
if(block.dependencies) {
block.dependencies.forEach(iteratorDependency, this);
function assignIndexToDependency(dependency) {
if(dependency.module) {
queue.push(function() {
assignIndexToModule(dependency.module);
});
}
}
if(block.blocks) {
block.blocks.forEach(function(b) {
var c;
if(!b.chunks) {
c = this.addChunk(b.chunkName, b.module, b.loc);
b.chunks = [c];
c.addBlock(b);
} else {
c = b.chunks[0];
}
chunk.addChunk(c);
c.addParent(chunk);
this.processDependenciesBlockForChunk(b, c);
}, this);
function assignIndexToDependencyBlock(block) {
var allDependencies = [];

function iteratorDependency(d) {
allDependencies.push(d);
}
function iteratorBlock(b) {
queue.push(function() {
assignIndexToDependencyBlock(b);
});
}

if(block.variables) {
block.variables.forEach(function(v) {
v.dependencies.forEach(iteratorDependency);
});
}
if(block.dependencies) {
block.dependencies.forEach(iteratorDependency);
}
if(block.blocks) {
block.blocks.slice().reverse().forEach(iteratorBlock, this);
}

allDependencies.reverse();
allDependencies.forEach(function(d) {
queue.push(function() {
assignIndexToDependency(d);
});
});
}

var queue = [function() {
assignIndexToModule(module);
}];
while(queue.length) {
queue.pop()();
}
};

Compilation.prototype.processDependenciesBlockForChunk = function processDependenciesBlockForChunk(block, chunk) {
var queue = [[block, chunk]];
while(queue.length) {
var queueItem = queue.pop();
block = queueItem[0];
chunk = queueItem[1];
if(block.variables) {
block.variables.forEach(function(v) {
v.dependencies.forEach(iteratorDependency, this);
}, this);
}
if(block.dependencies) {
block.dependencies.forEach(iteratorDependency, this);
}
if(block.blocks) {
block.blocks.forEach(iteratorBlock, this);
}
}

function iteratorBlock(b) {
var c;
if(!b.chunks) {
c = this.addChunk(b.chunkName, b.module, b.loc);
b.chunks = [c];
c.addBlock(b);
} else {
c = b.chunks[0];
}
chunk.addChunk(c);
c.addParent(chunk);
queue.push([b, c]);
}

function iteratorDependency(d) {
if(!d.module) {
return;
}
if(typeof d.module.index !== "number") {
d.module.index = this.nextFreeModuleIndex++;
}
if(d.weak) {
return;
}
if(chunk.addModule(d.module)) {
d.module.addChunk(chunk);
this.processDependenciesBlockForChunk(d.module, chunk);
}
if(typeof d.module.index2 !== "number") {
d.module.index2 = this.nextFreeModuleIndex2++;
queue.push([d.module, chunk]);
}
}
};
Expand Down Expand Up @@ -842,11 +902,14 @@ Compilation.prototype.sortItemsWithModuleIds = function sortItemsWithModuleIds()
});
};

Compilation.prototype.sortItemswithChunkIds = function sortItemswithChunkIds() {
Compilation.prototype.sortItemsWithChunkIds = function sortItemsWithChunkIds() {
this.chunks.sort(byId);
this.modules.forEach(function(module) {
module.sortItems();
});
this.chunks.forEach(function(chunk) {
chunk.sortItems();
});
};

Compilation.prototype.summarizeDependencies = function summarizeDependencies() {
Expand Down Expand Up @@ -924,7 +987,7 @@ Compilation.prototype.createHash = function createHash() {
} else {
this.chunkTemplate.updateHashForChunk(chunkHash);
}
this.applyPlugins("chunk-hash", chunk, chunkHash);
this.applyPlugins2("chunk-hash", chunk, chunkHash);
chunk.hash = chunkHash.digest(hashDigest);
hash.update(chunk.hash);
chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
Expand All @@ -949,7 +1012,7 @@ Compilation.prototype.createModuleAssets = function createModuleAssets() {
var cacheAssetsAndApplyPlugins = function cacheAssetsAndApplyPlugins(name) {
var file = this.getPath(name);
this.assets[file] = module.assets[name];
this.applyPlugins("module-asset", module, file);
this.applyPlugins2("module-asset", module, file);
}

for(var i = 0; i < this.modules.length; i++) {
Expand Down Expand Up @@ -999,7 +1062,7 @@ Compilation.prototype.createChunkAssets = function createChunkAssets() {
throw new Error("Conflict: Multiple assets emit to the same filename '" + file + "'");
this.assets[file] = source;
chunk.files.push(file);
this.applyPlugins("chunk-asset", chunk, file);
this.applyPlugins2("chunk-asset", chunk, file);
} catch(err) {
this.errors.push(new ChunkRenderError(chunk, file || filenameTemplate, err));
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ function Compiler() {
this.contextTimestamps = {};

this.resolvers = {
normal: new Resolver(null),
loader: new Resolver(null),
context: new Resolver(null)
normal: null,
loader: null,
context: null
};
var deprecationReported = false;
this.parser = {
Expand Down
Loading

0 comments on commit f4ab7fc

Please sign in to comment.