Skip to content

Commit bfdea76

Browse files
authored
Merge pull request webpack#5155 from timse/use-set-in-chunks
Use sortable set for "chunks" of Chunks
2 parents c47bdc6 + c7b80cf commit bfdea76

12 files changed

Lines changed: 152 additions & 101 deletions

‎lib/Chunk.js‎

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Chunk {
3030
this.name = name;
3131
this._modules = new SortableSet(undefined, sortByIdentifier);
3232
this.entrypoints = [];
33-
this.chunks = [];
33+
this._chunks = new SortableSet(undefined, sortById);
3434
this.parents = [];
3535
this.blocks = [];
3636
this.origins = [];
@@ -61,6 +61,21 @@ class Chunk {
6161
throw new Error("Chunk.initial was removed. Use isInitial()");
6262
}
6363

64+
/**
65+
* @return {Array} - an array containing the chunks
66+
*/
67+
getChunks() {
68+
return Array.from(this._chunks);
69+
}
70+
71+
getNumberOfChunks() {
72+
return this._chunks.size;
73+
}
74+
75+
get chunksIterable() {
76+
return this._chunks;
77+
}
78+
6479
hasRuntime() {
6580
if(this.entrypoints.length === 0) return false;
6681
return this.entrypoints[0].getRuntimeChunk() === this;
@@ -88,7 +103,11 @@ class Chunk {
88103
}
89104

90105
addChunk(chunk) {
91-
return this.addToCollection(this.chunks, chunk);
106+
if(this._chunks.has(chunk)) {
107+
return false;
108+
}
109+
this._chunks.add(chunk);
110+
return true;
92111
}
93112

94113
addParent(parentChunk) {
@@ -116,13 +135,13 @@ class Chunk {
116135
}
117136

118137
removeChunk(chunk) {
119-
const idx = this.chunks.indexOf(chunk);
120-
if(idx >= 0) {
121-
this.chunks.splice(idx, 1);
122-
chunk.removeParent(this);
123-
return true;
138+
if(!this._chunks.has(chunk)) {
139+
return false;
124140
}
125-
return false;
141+
142+
this._chunks.delete(chunk);
143+
chunk.removeParent(this);
144+
return true;
126145
}
127146

128147
removeParent(chunk) {
@@ -208,13 +227,10 @@ class Chunk {
208227
// cleanup parents
209228
this.parents.forEach(parentChunk => {
210229
// remove this chunk from its parents
211-
const idx = parentChunk.chunks.indexOf(this);
212-
if(idx >= 0) {
213-
parentChunk.chunks.splice(idx, 1);
214-
}
230+
parentChunk._chunks.delete(this);
215231

216232
// cleanup "sub chunks"
217-
this.chunks.forEach(chunk => {
233+
this._chunks.forEach(chunk => {
218234
/**
219235
* remove this chunk as "intermediary" and connect
220236
* it "sub chunks" and parents directly
@@ -232,7 +248,7 @@ class Chunk {
232248
* This can not be done in the above loop
233249
* as it is not garuanteed that `this.parents` contains anything.
234250
*/
235-
this.chunks.forEach(chunk => {
251+
this._chunks.forEach(chunk => {
236252
// remove this as parent of every "sub chunk"
237253
const idx = chunk.parents.indexOf(this);
238254
if(idx >= 0) {
@@ -261,10 +277,7 @@ class Chunk {
261277
}
262278

263279
replaceChunk(oldChunk, newChunk) {
264-
const idx = this.chunks.indexOf(oldChunk);
265-
if(idx >= 0) {
266-
this.chunks.splice(idx, 1);
267-
}
280+
this._chunks.delete(oldChunk);
268281
if(this !== newChunk && newChunk.addParent(this)) {
269282
this.addChunk(newChunk);
270283
}
@@ -293,8 +306,8 @@ class Chunk {
293306
otherChunk.parents.forEach(parentChunk => parentChunk.replaceChunk(otherChunk, this));
294307
otherChunk.parents.length = 0;
295308

296-
otherChunk.chunks.forEach(chunk => chunk.replaceParentChunk(otherChunk, this));
297-
otherChunk.chunks.length = 0;
309+
otherChunk._chunks.forEach(chunk => chunk.replaceParentChunk(otherChunk, this));
310+
otherChunk._chunks.clear();
298311

299312
otherChunk.blocks.forEach(b => {
300313
b.chunks = b.chunks ? b.chunks.map(c => {
@@ -315,9 +328,8 @@ class Chunk {
315328
origin.reasons.unshift(reason);
316329
}
317330
});
318-
this.chunks = this.chunks.filter(chunk => {
319-
return chunk !== otherChunk && chunk !== this;
320-
});
331+
this._chunks.delete(otherChunk);
332+
this._chunks.delete(this);
321333
this.parents = this.parents.filter(parentChunk => {
322334
return parentChunk !== otherChunk && parentChunk !== this;
323335
});
@@ -329,12 +341,12 @@ class Chunk {
329341
newChunk.blocks.push(block);
330342
block.chunks.push(newChunk);
331343
});
332-
this.chunks.forEach(chunk => {
333-
newChunk.chunks.push(chunk);
344+
this._chunks.forEach(chunk => {
345+
newChunk.addChunk(chunk);
334346
chunk.parents.push(newChunk);
335347
});
336348
this.parents.forEach(parentChunk => {
337-
parentChunk.chunks.push(newChunk);
349+
parentChunk.addChunk(newChunk);
338350
newChunk.parents.push(parentChunk);
339351
});
340352
this.entrypoints.forEach(entrypoint => {
@@ -413,7 +425,7 @@ class Chunk {
413425
if(chunk.name)
414426
chunkNameMap[chunk.id] = chunk.name;
415427
}
416-
chunk.chunks.forEach(addChunk);
428+
chunk._chunks.forEach(addChunk);
417429
}(this));
418430
return {
419431
hash: chunkHashMap,
@@ -439,7 +451,7 @@ class Chunk {
439451
origin.reasons.sort();
440452
});
441453
this.parents.sort(sortById);
442-
this.chunks.sort(sortById);
454+
this._chunks.sort();
443455
}
444456

445457
toString() {
@@ -448,16 +460,19 @@ class Chunk {
448460

449461
checkConstraints() {
450462
const chunk = this;
451-
chunk.chunks.forEach((child, idx) => {
452-
if(chunk.chunks.indexOf(child) !== idx)
463+
const chunks = Array.from(chunk._chunks);
464+
chunks.forEach((child, idx) => {
465+
if(chunks.indexOf(child) !== idx)
453466
throw new Error(`checkConstraints: duplicate child in chunk ${chunk.debugId} ${child.debugId}`);
454467
if(child.parents.indexOf(chunk) < 0)
455468
throw new Error(`checkConstraints: child missing parent ${chunk.debugId} -> ${child.debugId}`);
456469
});
457470
chunk.parents.forEach((parentChunk, idx) => {
458471
if(chunk.parents.indexOf(parentChunk) !== idx)
459472
throw new Error(`checkConstraints: duplicate parent in chunk ${chunk.debugId} ${parentChunk.debugId}`);
460-
if(parentChunk.chunks.indexOf(chunk) < 0)
473+
474+
const chunks = Array.from(parentChunk._chunks);
475+
if(chunks.indexOf(chunk) < 0)
461476
throw new Error(`checkConstraints: parent missing child ${parentChunk.debugId} <- ${chunk.debugId}`);
462477
});
463478
}
@@ -466,11 +481,21 @@ class Chunk {
466481
Object.defineProperty(Chunk.prototype, "modules", {
467482
configurable: false,
468483
get: util.deprecate(function() {
469-
return Array.from(this._modules);
484+
return this._modules.getFrozenArray();
470485
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
471486
set: util.deprecate(function(value) {
472487
this.setModules(value);
473488
}, "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead.")
474489
});
475490

491+
Object.defineProperty(Chunk.prototype, "chunks", {
492+
configurable: false,
493+
get: util.deprecate(function() {
494+
return this._chunks.getFrozenArray();
495+
}, "Chunk.chunks: Use Chunk.getChunks() instead"),
496+
set() {
497+
throw new Error("Readonly. Use Chunk.addChunk/removeChunk/getChunks to access/modify chunks.");
498+
}
499+
});
500+
476501
module.exports = Chunk;

‎lib/HotModuleReplacementPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ module.exports = class HotModuleReplacementPlugin {
166166
.replace(/\$require\$/g, this.requireFn)
167167
.replace(/\$hash\$/g, JSON.stringify(hash))
168168
.replace(/\$requestTimeout\$/g, requestTimeout)
169-
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : `var chunkId = ${JSON.stringify(chunk.id)};`)
169+
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.getNumberOfChunks() > 0 ? "for(var chunkId in installedChunks)" : `var chunkId = ${JSON.stringify(chunk.id)};`)
170170
]);
171171
});
172172

‎lib/JsonpMainTemplatePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JsonpMainTemplatePlugin {
1313
var otherChunksInEntry = chunk.entrypoints.some(function(entrypoint) {
1414
return entrypoint.chunks.length > 1;
1515
});
16-
var onDemandChunks = chunk.chunks.length > 0;
16+
var onDemandChunks = chunk.getNumberOfChunks() > 0;
1717
return otherChunksInEntry || onDemandChunks;
1818
}
1919
mainTemplate.plugin("local-vars", function(source, chunk) {
@@ -130,7 +130,7 @@ class JsonpMainTemplatePlugin {
130130
]);
131131
});
132132
mainTemplate.plugin("require-extensions", function(source, chunk) {
133-
if(chunk.chunks.length === 0) return source;
133+
if(chunk.getNumberOfChunks() === 0) return source;
134134

135135
return this.asString([
136136
source,

‎lib/MainTemplate.js‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module.exports = class MainTemplate extends Template {
9999
});
100100
this.plugin("require-extensions", (source, chunk, hash) => {
101101
const buf = [];
102-
if(chunk.chunks.length > 0) {
102+
if(chunk.getNumberOfChunks() > 0) {
103103
buf.push("// This file contains only the entry chunk.");
104104
buf.push("// The chunk loading function for additional chunks");
105105
buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`);
@@ -199,13 +199,16 @@ module.exports = class MainTemplate extends Template {
199199

200200
entryPointInChildren(chunk) {
201201
const checkChildren = (chunk, alreadyCheckedChunks) => {
202-
return chunk.chunks.some((child) => {
203-
if(alreadyCheckedChunks.indexOf(child) >= 0) return;
204-
alreadyCheckedChunks.push(child);
205-
return child.hasEntryModule() || checkChildren(child, alreadyCheckedChunks);
206-
});
202+
for(const child of chunk.chunksIterable) {
203+
if(!alreadyCheckedChunks.has(child)) {
204+
alreadyCheckedChunks.add(child);
205+
if(child.hasEntryModule() || checkChildren(child, alreadyCheckedChunks)) {
206+
return true;
207+
}
208+
}
209+
}
207210
};
208-
return checkChildren(chunk, []);
211+
return checkChildren(chunk, new Set()) === true;
209212
}
210213

211214
getPublicPath(options) {

‎lib/Module.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class Module extends DependenciesBlock {
129129
return this._chunks.size;
130130
}
131131

132+
get chunksIterable() {
133+
return this._chunks;
134+
}
135+
132136
hasEqualsChunks(otherModule) {
133137
if(this._chunks.size !== otherModule._chunks.size) return false;
134138
this._chunks.sortWith(sortByDebugId);
@@ -233,7 +237,7 @@ Object.defineProperty(Module.prototype, "entry", {
233237
Object.defineProperty(Module.prototype, "chunks", {
234238
configurable: false,
235239
get: util.deprecate(function() {
236-
return Array.from(this._chunks);
240+
return this._chunks.getFrozenArray();
237241
}, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
238242
set() {
239243
throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");

‎lib/node/NodeMainTemplatePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = class NodeMainTemplatePlugin {
1414
apply(mainTemplate) {
1515
const asyncChunkLoading = this.asyncChunkLoading;
1616
mainTemplate.plugin("local-vars", function(source, chunk) {
17-
if(chunk.chunks.length > 0) {
17+
if(chunk.getNumberOfChunks() > 0) {
1818
return this.asString([
1919
source,
2020
"",
@@ -28,7 +28,7 @@ module.exports = class NodeMainTemplatePlugin {
2828
return source;
2929
});
3030
mainTemplate.plugin("require-extensions", function(source, chunk) {
31-
if(chunk.chunks.length > 0) {
31+
if(chunk.getNumberOfChunks() > 0) {
3232
return this.asString([
3333
source,
3434
"",

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,7 @@ Take a look at the "name"/"names" or async/children option.`);
223223
}
224224

225225
if(asyncOrNoSelectedChunk) {
226-
// nothing to do here
227-
if(!targetChunk.chunks) {
228-
return [];
229-
}
230-
231-
return targetChunk.chunks.filter((chunk) => {
226+
return targetChunk.getChunks().filter((chunk) => {
232227
// we only are interested in on-demand chunks
233228
if(chunk.isInitial())
234229
return false;

‎lib/util/SortableSet.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = class SortableSet extends Set {
66
super(initialIterable);
77
this._sortFn = defaultSort;
88
this._lastActiveSortFn = null;
9+
this._frozenArray = null;
910
}
1011

1112
/**
@@ -14,10 +15,21 @@ module.exports = class SortableSet extends Set {
1415
*/
1516
add(value) {
1617
this._lastActiveSortFn = null;
18+
this._frozenArray = null;
1719
super.add(value);
1820
return this;
1921
}
2022

23+
delete(value) {
24+
this._frozenArray = null;
25+
return super.delete(value);
26+
}
27+
28+
clear() {
29+
this._frozenArray = null;
30+
return super.clear();
31+
}
32+
2133
/**
2234
* @param {Function} sortFn - function to sort the set
2335
* @returns {void}
@@ -42,4 +54,14 @@ module.exports = class SortableSet extends Set {
4254
sort() {
4355
this.sortWith(this._sortFn);
4456
}
57+
58+
/**
59+
* @returns {any[]} - returns content as frozen array
60+
*/
61+
getFrozenArray() {
62+
if(this._frozenArray === null) {
63+
this._frozenArray = Object.freeze(Array.from(this));
64+
}
65+
return this._frozenArray;
66+
}
4567
};

‎lib/webworker/WebWorkerMainTemplatePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Template = require("../Template");
99
class WebWorkerMainTemplatePlugin {
1010
apply(mainTemplate) {
1111
mainTemplate.plugin("local-vars", function(source, chunk) {
12-
if(chunk.chunks.length > 0) {
12+
if(chunk.getNumberOfChunks() > 0) {
1313
return this.asString([
1414
source,
1515
"",
@@ -48,7 +48,7 @@ class WebWorkerMainTemplatePlugin {
4848
]);
4949
});
5050
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
51-
if(chunk.chunks.length > 0) {
51+
if(chunk.getNumberOfChunks() > 0) {
5252
const chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
5353
return this.asString([
5454
source,

0 commit comments

Comments
 (0)