Skip to content

Commit b04d510

Browse files
authored
Merge pull request webpack#6510 from webpack/performance/chunk-splitting-combinations
improve performance of chunk splitting combinations
2 parents ddcfa37 + 244d27a commit b04d510

26 files changed

Lines changed: 250 additions & 209 deletions

File tree

‎lib/Stats.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ class Stats {
448448
names: chunk.name ? [chunk.name] : [],
449449
files: chunk.files.slice(),
450450
hash: chunk.renderedHash,
451-
siblings: Array.from(siblings),
452-
parents: Array.from(parents),
453-
children: Array.from(children)
451+
siblings: Array.from(siblings).sort(),
452+
parents: Array.from(parents).sort(),
453+
children: Array.from(children).sort()
454454
};
455455
if(showChunkModules) {
456456
obj.modules = chunk
@@ -472,7 +472,17 @@ class Stats {
472472
loc: formatLocation(origin.loc),
473473
request: origin.request,
474474
reasons: origin.reasons || []
475-
}));
475+
})).sort((a, b) => {
476+
if(typeof a.moduleId === "number" && typeof b.moduleId !== "number") return 1;
477+
if(typeof a.moduleId !== "number" && typeof b.moduleId === "number") return -1;
478+
if(typeof a.moduleId === "number" && typeof b.moduleId === "number") {
479+
const diffId = a.moduleId - b.moduleId;
480+
if(diffId !== 0) return diffId;
481+
}
482+
if(a.loc < b.loc) return -1;
483+
if(a.loc > b.loc) return 1;
484+
return 0;
485+
});
476486
}
477487
return obj;
478488
});

‎lib/WebpackOptionsDefaulter.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
168168
this.set("optimization.usedExports", "make", options => isProductionLikeMode(options));
169169
this.set("optimization.concatenateModules", "make", options => isProductionLikeMode(options));
170170
this.set("optimization.splitChunks", {});
171-
this.set("optimization.splitChunks.maxComplexity", "make", options => isProductionLikeMode(options) ? 3 : 1);
172171
this.set("optimization.splitChunks.chunks", "async");
173172
this.set("optimization.splitChunks.minSize", 30000);
174173
this.set("optimization.splitChunks.minChunks", 1);

‎lib/optimize/SplitChunksPlugin.js‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const crypto = require("crypto");
88
const SortableSet = require("../util/SortableSet");
99
const GraphHelpers = require("../GraphHelpers");
10+
const isSubset = require("../util/SetHelpers").isSubset;
1011

1112
const hashFilename = (name) => {
1213
return crypto.createHash("md5").update(name).digest("hex").slice(0, 8);
@@ -40,23 +41,6 @@ const isOverlap = (a, b) => {
4041
return false;
4142
};
4243

43-
const getCombinations = (array, maxDepth) => {
44-
const results = [];
45-
getCombinationsInteral(array, maxDepth, results);
46-
return results;
47-
};
48-
49-
const getCombinationsInteral = (array, maxDepth, results) => {
50-
results.push(array);
51-
if(maxDepth === 0 || array.length === 1) {
52-
return;
53-
}
54-
for(let i = 0; i < array.length; i++) {
55-
const newArray = array.slice(0, i).concat(array.slice(i + 1, array.length));
56-
getCombinationsInteral(newArray, maxDepth - 1, results);
57-
}
58-
};
59-
6044
const compareEntries = (a, b) => {
6145
// 1. by priority
6246
const diffPriority = a.cacheGroup.priority - b.cacheGroup.priority;
@@ -97,7 +81,6 @@ module.exports = class SplitChunksPlugin {
9781

9882
static normalizeOptions(options) {
9983
return {
100-
maxComplexity: options.maxComplexity || 0,
10184
chunks: options.chunks || "all",
10285
minSize: options.minSize || 0,
10386
minChunks: options.minChunks || 1,
@@ -227,6 +210,25 @@ module.exports = class SplitChunksPlugin {
227210
for(const chunk of chunks) {
228211
indexMap.set(chunk, index++);
229212
}
213+
const getKey = chunks => {
214+
return Array.from(chunks, c => indexMap.get(c)).sort().join();
215+
};
216+
// Create a list of possible combinations
217+
const chunkSetsInGraph = new Map(); // Map<string, Set<Chunk>>
218+
for(const module of compilation.modules) {
219+
const chunkIndices = getKey(module.chunksIterable);
220+
chunkSetsInGraph.set(chunkIndices, new Set(module.chunksIterable));
221+
}
222+
const combinations = new Map(); // Map<string, Set<Chunk>[]>
223+
for(const [key, chunksSet] of chunkSetsInGraph) {
224+
var array = [];
225+
for(const set of chunkSetsInGraph.values()) {
226+
if(isSubset(chunksSet, set)) {
227+
array.push(set);
228+
}
229+
}
230+
combinations.set(key, array);
231+
}
230232
// Map a list of chunks to a list of modules
231233
// For the key the chunk "index" is used, the value is a SortableSet of modules
232234
const chunksInfoMap = new Map();
@@ -249,24 +251,24 @@ module.exports = class SplitChunksPlugin {
249251
getName: cacheGroupSource.getName !== undefined ? cacheGroupSource.getName : this.options.getName,
250252
reuseExistingChunk: cacheGroupSource.reuseExistingChunk
251253
};
252-
// Select chunks by configuration
253-
const maxSelectedChunks = cacheGroup.chunks === "initial" ? chunks.filter(chunk => chunk.canBeInitial()) :
254-
cacheGroup.chunks === "async" ? chunks.filter(chunk => !chunk.canBeInitial()) :
255-
chunks;
256-
// For all (nearly all) combination of chunk selection
257-
for(const selectedChunks of getCombinations(maxSelectedChunks, this.options.maxComplexity)) {
254+
// For all combination of chunk selection
255+
for(const chunkCombination of combinations.get(getKey(chunks))) {
258256
// Get indices of chunks in which this module occurs
259-
const chunkIndices = selectedChunks.map(chunk => indexMap.get(chunk));
257+
const chunkIndices = Array.from(chunkCombination, chunk => indexMap.get(chunk));
260258
// Break if minimum number of chunks is not reached
261259
if(chunkIndices.length < cacheGroup.minChunks)
262260
continue;
261+
// Select chunks by configuration
262+
const selectedChunks = cacheGroup.chunks === "initial" ? Array.from(chunkCombination).filter(chunk => chunk.canBeInitial()) :
263+
cacheGroup.chunks === "async" ? Array.from(chunkCombination).filter(chunk => !chunk.canBeInitial()) :
264+
Array.from(chunkCombination);
263265
// Determine name for split chunk
264266
const name = cacheGroup.getName(module, selectedChunks, cacheGroup.key);
265267
// Create key for maps
266268
// When it has a name we use the name as key
267269
// Elsewise we create the key from chunks and cache group key
268270
// This automatically merges equal names
269-
const chunksKey = chunkIndices.sort().join();
271+
const chunksKey = getKey(selectedChunks);
270272
const key = name && `name:${name}` ||
271273
`chunks:${chunksKey} key:${cacheGroup.key}`;
272274
// Add module to maps

‎lib/util/SetHelpers.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ exports.intersect = sets => {
2424
}
2525
return current;
2626
};
27+
28+
exports.isSubset = (bigSet, smallSet) => {
29+
if(bigSet.size < smallSet.size) return false;
30+
for(const item of smallSet) {
31+
if(!bigSet.has(item)) return false;
32+
}
33+
return true;
34+
};

‎schemas/WebpackOptions.json‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,11 +1335,6 @@
13351335
"type": "object",
13361336
"additionalProperties": false,
13371337
"properties": {
1338-
"maxComplexity": {
1339-
"description": "Limits the algorithmic complexity",
1340-
"type": "number",
1341-
"minimum": 0
1342-
},
13431338
"chunks": {
13441339
"description": "Select chunks for determining shared modules (defaults to \"async\", \"initial\" and \"all\" requires adding these chunks to the HTML)",
13451340
"enum": [

‎test/statsCases/aggressive-splitting-entry/expected.txt‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ Child fitting:
99
4c485a9e1dc58eedfbd1.js 1.94 KiB 2 [emitted]
1010
337c857e3e34cc5ef72d.js 1.94 KiB 3 [emitted]
1111
Entrypoint main = 4c485a9e1dc58eedfbd1.js 337c857e3e34cc5ef72d.js a65f8652fd07c12d0049.js
12-
chunk {0} 706f97eb6a2b8fb1a17e.js 916 bytes <{2}> <{3}> <{1}>
12+
chunk {0} 706f97eb6a2b8fb1a17e.js 916 bytes <{1}> <{2}> <{3}>
1313
> ./g [4] ./index.js 7:0-13
1414
[7] ./g.js 916 bytes {0} [built]
1515
chunk {1} a65f8652fd07c12d0049.js 1.87 KiB ={2}= ={3}= >{0}< [entry] [rendered]
1616
> ./index main
1717
[3] ./e.js 899 bytes {1} [built]
1818
[4] ./index.js 111 bytes {1} [built]
1919
[6] ./f.js 900 bytes {1} [built]
20-
chunk {2} 4c485a9e1dc58eedfbd1.js 1.76 KiB ={3}= ={1}= >{0}< [initial] [rendered] [recorded] aggressive splitted
20+
chunk {2} 4c485a9e1dc58eedfbd1.js 1.76 KiB ={1}= ={3}= >{0}< [initial] [rendered] [recorded] aggressive splitted
2121
> ./index main
2222
[0] ./b.js 899 bytes {2} [built]
2323
[5] ./a.js 899 bytes {2} [built]
24-
chunk {3} 337c857e3e34cc5ef72d.js 1.76 KiB ={2}= ={1}= >{0}< [initial] [rendered] [recorded] aggressive splitted
24+
chunk {3} 337c857e3e34cc5ef72d.js 1.76 KiB ={1}= ={2}= >{0}< [initial] [rendered] [recorded] aggressive splitted
2525
> ./index main
2626
[1] ./c.js 899 bytes {3} [built]
2727
[2] ./d.js 899 bytes {3} [built]
@@ -35,19 +35,19 @@ Child content-change:
3535
4c485a9e1dc58eedfbd1.js 1.94 KiB 2 [emitted]
3636
337c857e3e34cc5ef72d.js 1.94 KiB 3 [emitted]
3737
Entrypoint main = 4c485a9e1dc58eedfbd1.js 337c857e3e34cc5ef72d.js a65f8652fd07c12d0049.js
38-
chunk {0} 706f97eb6a2b8fb1a17e.js 916 bytes <{2}> <{3}> <{1}>
38+
chunk {0} 706f97eb6a2b8fb1a17e.js 916 bytes <{1}> <{2}> <{3}>
3939
> ./g [4] ./index.js 7:0-13
4040
[7] ./g.js 916 bytes {0} [built]
4141
chunk {1} a65f8652fd07c12d0049.js 1.87 KiB ={2}= ={3}= >{0}< [entry] [rendered]
4242
> ./index main
4343
[3] ./e.js 899 bytes {1} [built]
4444
[4] ./index.js 111 bytes {1} [built]
4545
[6] ./f.js 900 bytes {1} [built]
46-
chunk {2} 4c485a9e1dc58eedfbd1.js 1.76 KiB ={3}= ={1}= >{0}< [initial] [rendered] [recorded] aggressive splitted
46+
chunk {2} 4c485a9e1dc58eedfbd1.js 1.76 KiB ={1}= ={3}= >{0}< [initial] [rendered] [recorded] aggressive splitted
4747
> ./index main
4848
[0] ./b.js 899 bytes {2} [built]
4949
[5] ./a.js 899 bytes {2} [built]
50-
chunk {3} 337c857e3e34cc5ef72d.js 1.76 KiB ={2}= ={1}= >{0}< [initial] [rendered] [recorded] aggressive splitted
50+
chunk {3} 337c857e3e34cc5ef72d.js 1.76 KiB ={1}= ={2}= >{0}< [initial] [rendered] [recorded] aggressive splitted
5151
> ./index main
5252
[1] ./c.js 899 bytes {3} [built]
5353
[2] ./d.js 899 bytes {3} [built]

‎test/statsCases/aggressive-splitting-on-demand/expected.txt‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ a28f1d7d7367daaaf00a.js 1 KiB 7 [emitted]
1414
9ac0014114101e5f7d2c.js 1.94 KiB 9, 1 [emitted]
1515
f507739d37522e79688a.js 8.38 KiB 10 [emitted] main
1616
Entrypoint main = f507739d37522e79688a.js
17-
chunk {0} ba1a8dd27d611254d495.js 1.76 KiB <{10}> ={2}= ={3}= ={9}= ={7}= ={1}= [recorded] aggressive splitted
18-
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
17+
chunk {0} ba1a8dd27d611254d495.js 1.76 KiB <{10}> ={1}= ={2}= ={3}= ={7}= ={9}= [recorded] aggressive splitted
1918
> ./b ./d ./e ./f ./g [11] ./index.js 5:0-44
19+
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
2020
[0] ./b.js 899 bytes {0} {5} [built]
2121
[1] ./d.js 899 bytes {0} {8} [built]
22-
chunk {1} f4818881f9e5138ad8ac.js 899 bytes <{10}> ={8}= ={0}= ={2}=
22+
chunk {1} f4818881f9e5138ad8ac.js 899 bytes <{10}> ={0}= ={2}= ={8}=
2323
> ./c ./d ./e [11] ./index.js 3:0-30
2424
> ./b ./d ./e ./f ./g [11] ./index.js 5:0-44
2525
[2] ./e.js 899 bytes {1} {9} [built]
26-
chunk {2} 81950df8ced1664c8037.js 1.76 KiB <{10}> ={0}= ={3}= ={9}= ={7}= ={1}= ={6}= [recorded] aggressive splitted
27-
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
28-
> ./b ./d ./e ./f ./g [11] ./index.js 5:0-44
26+
chunk {2} 81950df8ced1664c8037.js 1.76 KiB <{10}> ={0}= ={1}= ={3}= ={6}= ={7}= ={9}= [recorded] aggressive splitted
2927
> ./f ./g ./h ./i ./j ./k [11] ./index.js 4:0-51
28+
> ./b ./d ./e ./f ./g [11] ./index.js 5:0-44
29+
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
3030
[3] ./f.js 899 bytes {2} [built]
3131
[4] ./g.js 901 bytes {2} [built]
32-
chunk {3} 2a7cf3b38962fac54bb9.js 1.76 KiB <{10}> ={0}= ={2}= ={9}= ={7}= ={6}= [recorded] aggressive splitted
33-
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
32+
chunk {3} 2a7cf3b38962fac54bb9.js 1.76 KiB <{10}> ={0}= ={2}= ={6}= ={7}= ={9}= [recorded] aggressive splitted
3433
> ./f ./g ./h ./i ./j ./k [11] ./index.js 4:0-51
34+
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
3535
[6] ./h.js 899 bytes {3} [built]
3636
[7] ./i.js 899 bytes {3} [built]
3737
chunk {4} acfde31fdfbd1bdb41c4.js 899 bytes <{10}>
@@ -56,6 +56,6 @@ chunk {9} 9ac0014114101e5f7d2c.js 1.76 KiB <{10}> ={0}= ={2}= ={3}= ={7}= [re
5656
> ./b ./d ./e ./f ./g ./h ./i ./j ./k [11] ./index.js 6:0-72
5757
[2] ./e.js 899 bytes {1} {9} [built]
5858
[8] ./j.js 901 bytes {6} {9} [built]
59-
chunk {10} f507739d37522e79688a.js (main) 248 bytes >{0}< >{2}< >{1}< >{3}< >{9}< >{7}< >{6}< >{4}< >{5}< >{8}< [entry]
59+
chunk {10} f507739d37522e79688a.js (main) 248 bytes >{0}< >{1}< >{2}< >{3}< >{4}< >{5}< >{6}< >{7}< >{8}< >{9}< [entry]
6060
> ./index main
6161
[11] ./index.js 248 bytes {10} [built]

0 commit comments

Comments
 (0)