Skip to content

Commit 996b6c8

Browse files
committed
cache parent checks, check faster array for contain
1 parent 7a15344 commit 996b6c8

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/Chunk.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
var debugId = 1000;
6+
57
function Chunk(name, module, loc) {
68
this.id = null;
79
this.ids = null;
10+
this.debugId = debugId++;
811
this.name = name;
912
this.modules = [];
1013
this.chunks = [];

lib/optimize/RemoveParentModulesPlugin.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
function chunkContainsModule(chunk, module) {
6+
var chunks = module.chunks;
7+
var modules = chunk.modules;
8+
if(chunks.length > modules.length) {
9+
return chunks.indexOf(chunk) >= 0;
10+
} else {
11+
return modules.indexOf(module) >= 0;
12+
}
13+
}
14+
515
function hasModule(chunk, module, checkedChunks) {
6-
if(chunk.modules.indexOf(module) >= 0) return [chunk];
16+
if(chunkContainsModule(chunk, module)) return [chunk];
717
if(chunk.entry) return false;
818
return allHaveModule(chunk.parents.filter(function(c) {
919
return checkedChunks.indexOf(c) < 0;
@@ -29,16 +39,34 @@ function addToSet(set, items) {
2939
});
3040
}
3141

42+
function debugIds(chunks) {
43+
var list = chunks.map(function(chunk) {
44+
return chunk.debugId;
45+
});
46+
if(list.some(function(dId) {
47+
return typeof dId !== "number";
48+
})) return "no";
49+
list.sort();
50+
return list.join(",");
51+
}
52+
3253
function RemoveParentModulesPlugin() {}
3354
module.exports = RemoveParentModulesPlugin;
3455

3556
RemoveParentModulesPlugin.prototype.apply = function(compiler) {
3657
compiler.plugin("compilation", function(compilation) {
3758
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], function(chunks) {
3859
chunks.forEach(function(chunk) {
60+
var cache = {};
3961
chunk.modules.slice().forEach(function(module) {
4062
if(chunk.entry) return;
41-
var parentChunksWithModule = allHaveModule(chunk.parents, module);
63+
var dId = "$" + debugIds(module.chunks);
64+
var parentChunksWithModule;
65+
if((dId in cache) && dId !== "$no") {
66+
parentChunksWithModule = cache[dId];
67+
} else {
68+
parentChunksWithModule = cache[dId] = allHaveModule(chunk.parents, module);
69+
}
4270
if(parentChunksWithModule) {
4371
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
4472
chunk.removeModule(module);

0 commit comments

Comments
 (0)