Skip to content

Commit 25a904b

Browse files
authored
Merge pull request webpack#5083 from timse/sortable-set
Sortable set
2 parents 9128f96 + 8f3d65a commit 25a904b

6 files changed

Lines changed: 111 additions & 55 deletions

File tree

‎lib/Chunk.js‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@
66

77
const util = require("util");
88
const compareLocations = require("./compareLocations");
9+
const SortableSet = require("./util/SortableSet");
910
let debugId = 1000;
1011

11-
const byId = (a, b) => {
12+
const sortById = (a, b) => {
1213
if(a.id < b.id) return -1;
1314
if(b.id < a.id) return 1;
1415
return 0;
1516
};
1617

18+
const sortByIdentifier = (a, b) => {
19+
if(a.identifier() > b.identifier()) return 1;
20+
if(a.identifier() < b.identifier()) return -1;
21+
return 0;
22+
};
23+
1724
class Chunk {
1825

1926
constructor(name, module, loc) {
2027
this.id = null;
2128
this.ids = null;
2229
this.debugId = debugId++;
2330
this.name = name;
24-
this._modules = new Set();
25-
this._modulesIsSorted = true;
31+
this._modules = new SortableSet(undefined, sortByIdentifier);
2632
this.entrypoints = [];
2733
this.chunks = [];
2834
this.parents = [];
@@ -92,7 +98,6 @@ class Chunk {
9298
addModule(module) {
9399
if(!this._modules.has(module)) {
94100
this._modules.add(module);
95-
this._modulesIsSorted = false;
96101
return true;
97102
}
98103
return false;
@@ -139,8 +144,7 @@ class Chunk {
139144
}
140145

141146
setModules(modules) {
142-
this._modules = new Set(modules);
143-
this._modulesIsSorted = false;
147+
this._modules = new SortableSet(modules, sortByIdentifier);
144148
}
145149

146150
getNumberOfModules() {
@@ -159,19 +163,9 @@ class Chunk {
159163
return Array.from(this._modules, fn);
160164
}
161165

162-
_ensureModulesSorted() {
163-
if(this._modulesIsSorted) return;
164-
this._modules = new Set(Array.from(this._modules).sort((a, b) => {
165-
if(a.identifier() > b.identifier()) return 1;
166-
if(a.identifier() < b.identifier()) return -1;
167-
return 0;
168-
}));
169-
this._modulesIsSorted = true;
170-
}
171-
172166
compareTo(otherChunk) {
173-
this._ensureModulesSorted();
174-
otherChunk._ensureModulesSorted();
167+
this._modules.sort();
168+
otherChunk._modules.sort();
175169
if(this._modules.size > otherChunk._modules.size) return -1;
176170
if(this._modules.size < otherChunk._modules.size) return 1;
177171
const a = this._modules[Symbol.iterator]();
@@ -196,7 +190,7 @@ class Chunk {
196190
}
197191

198192
getModulesIdent() {
199-
this._ensureModulesSorted();
193+
this._modules.sort();
200194
let str = "";
201195
this._modules.forEach(m => {
202196
str += m.identifier() + "#";
@@ -428,7 +422,7 @@ class Chunk {
428422
}
429423

430424
sortItems() {
431-
this._modules = new Set(Array.from(this._modules).sort(byId));
425+
this._modules.sortWith(sortById);
432426
this.origins.sort((a, b) => {
433427
const aIdent = a.module.identifier();
434428
const bIdent = b.module.identifier();
@@ -440,8 +434,8 @@ class Chunk {
440434
if(origin.reasons)
441435
origin.reasons.sort();
442436
});
443-
this.parents.sort(byId);
444-
this.chunks.sort(byId);
437+
this.parents.sort(sortById);
438+
this.chunks.sort(sortById);
445439
}
446440

447441
toString() {

‎lib/Module.js‎

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55
"use strict";
66

77
const util = require("util");
8+
89
const DependenciesBlock = require("./DependenciesBlock");
910
const ModuleReason = require("./ModuleReason");
11+
const SortableSet = require("./util/SortableSet");
1012
const Template = require("./Template");
1113

12-
function byId(a, b) {
14+
let debugId = 1000;
15+
16+
const sortById = (a, b) => {
1317
return a.id - b.id;
14-
}
18+
};
1519

16-
function byDebugId(a, b) {
20+
const sortByDebugId = (a, b) => {
1721
return a.debugId - b.debugId;
18-
}
19-
20-
let debugId = 1000;
22+
};
2123

2224
class Module extends DependenciesBlock {
25+
2326
constructor() {
2427
super();
2528
this.context = null;
@@ -34,9 +37,7 @@ class Module extends DependenciesBlock {
3437
this.used = null;
3538
this.usedExports = null;
3639
this.providedExports = null;
37-
this._chunks = new Set();
38-
this._chunksIsSorted = true;
39-
this._chunksIsSortedByDebugId = true;
40+
this._chunks = new SortableSet(undefined, sortById);
4041
this._chunksDebugIdent = undefined;
4142
this.warnings = [];
4243
this.dependenciesWarnings = [];
@@ -59,7 +60,6 @@ class Module extends DependenciesBlock {
5960
this.providedExports = null;
6061
this._chunks.clear();
6162
this._chunksDebugIdent = undefined;
62-
this._chunksIsSorted = this._chunksIsSortedByDebugId = false;
6363
super.disconnect();
6464
}
6565

@@ -71,14 +71,16 @@ class Module extends DependenciesBlock {
7171
this.depth = null;
7272
this._chunks.clear();
7373
this._chunksDebugIdent = undefined;
74-
this._chunksIsSorted = this._chunksIsSortedByDebugId = false;
7574
super.unseal();
7675
}
7776

77+
setChunks(chunks) {
78+
this._chunks = new SortableSet(chunks, sortById);
79+
}
80+
7881
addChunk(chunk) {
7982
this._chunks.add(chunk);
8083
this._chunksDebugIdent = undefined;
81-
this._chunksIsSorted = this._chunksIsSortedByDebugId = false;
8284
}
8385

8486
removeChunk(chunk) {
@@ -96,7 +98,7 @@ class Module extends DependenciesBlock {
9698

9799
getChunkIdsIdent() {
98100
if(this._chunksDebugIdent !== undefined) return this._chunksDebugIdent;
99-
this._ensureChunksSortedByDebugId();
101+
this._chunks.sortWith(sortByDebugId);
100102
const chunks = this._chunks;
101103
const list = [];
102104
for(const chunk of chunks) {
@@ -130,8 +132,8 @@ class Module extends DependenciesBlock {
130132

131133
hasEqualsChunks(otherModule) {
132134
if(this._chunks.size !== otherModule._chunks.size) return false;
133-
this._ensureChunksSortedByDebugId();
134-
otherModule._ensureChunksSortedByDebugId();
135+
this._chunks.sortWith(sortByDebugId);
136+
otherModule._chunks.sortWith(sortByDebugId);
135137
const a = this._chunks[Symbol.iterator]();
136138
const b = otherModule._chunks[Symbol.iterator]();
137139
while(true) { // eslint-disable-line
@@ -142,20 +144,6 @@ class Module extends DependenciesBlock {
142144
}
143145
}
144146

145-
_ensureChunksSorted() {
146-
if(this._chunksIsSorted) return;
147-
this._chunks = new Set(Array.from(this._chunks).sort(byId));
148-
this._chunksIsSortedByDebugId = false;
149-
this._chunksIsSorted = true;
150-
}
151-
152-
_ensureChunksSortedByDebugId() {
153-
if(this._chunksIsSortedByDebugId) return;
154-
this._chunks = new Set(Array.from(this._chunks).sort(byDebugId));
155-
this._chunksIsSorted = false;
156-
this._chunksIsSortedByDebugId = true;
157-
}
158-
159147
addReason(module, dependency) {
160148
this.reasons.push(new ModuleReason(module, dependency));
161149
}
@@ -221,8 +209,8 @@ class Module extends DependenciesBlock {
221209
sortItems(sortChunks) {
222210
super.sortItems();
223211
if(sortChunks)
224-
this._ensureChunksSorted();
225-
this.reasons.sort((a, b) => byId(a.module, b.module));
212+
this._chunks.sort();
213+
this.reasons.sort((a, b) => sortById(a.module, b.module));
226214
if(Array.isArray(this.usedExports)) {
227215
this.usedExports.sort();
228216
}

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ function getPathInAst(ast, node) {
129129
class ConcatenatedModule extends Module {
130130
constructor(rootModule, modules) {
131131
super();
132+
super.setChunks(rootModule._chunks);
132133
this.rootModule = rootModule;
133134
this.modules = modules;
134135
this.usedExports = rootModule.usedExports;
135136
this.providedExports = rootModule.providedExports;
136137
this.optimizationBailout = rootModule.optimizationBailout;
137138
this.used = rootModule.used;
138-
this._chunks = new Set(rootModule._chunks);
139139
this.index = rootModule.index;
140140
this.index2 = rootModule.index2;
141141
this.depth = rootModule.depth;

‎lib/util/SortableSet.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
module.exports = class SortableSet extends Set {
4+
5+
constructor(initialIterable, defaultSort) {
6+
super(initialIterable);
7+
this._sortFn = defaultSort;
8+
this._lastActiveSortFn = null;
9+
}
10+
11+
/**
12+
* @param {any} value - value to add to set
13+
* @returns {SortableSet} - returns itself
14+
*/
15+
add(value) {
16+
this._lastActiveSortFn = null;
17+
super.add(value);
18+
return this;
19+
}
20+
21+
/**
22+
* @param {Function} sortFn - function to sort the set
23+
* @returns {void}
24+
*/
25+
sortWith(sortFn) {
26+
if(this.size === 0 || sortFn === this._lastActiveSortFn) {
27+
// already sorted - nothing to do
28+
return;
29+
}
30+
31+
const sortedArray = Array.from(this).sort(sortFn);
32+
super.clear();
33+
for(let i = 0; i < sortedArray.length; i += 1) {
34+
this.add(sortedArray[i]);
35+
}
36+
this._lastActiveSortFn = sortFn;
37+
}
38+
39+
/**
40+
* @returns {void}
41+
*/
42+
sort() {
43+
this.sortWith(this._sortFn);
44+
}
45+
};

‎test/SortableSet.test.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* globals describe, it */
2+
"use strict";
3+
4+
const SortableSet = require("../lib/util/SortableSet");
5+
6+
describe("util/SortableSet", () => {
7+
it("Can be constructed like a normal Set", () => {
8+
const sortableSet = new SortableSet([1, 1, 1, 1, 1, 4, 5, 2], () => {});
9+
Array.from(sortableSet).should.eql([1, 4, 5, 2]);
10+
});
11+
12+
it("Can sort its content", () => {
13+
const sortableSet = new SortableSet(
14+
[1, 1, 1, 6, 6, 1, 1, 4, 5, 2, 3, 8, 5, 7, 9, 0, 3, 1],
15+
(a, b) => a - b
16+
);
17+
sortableSet.sort();
18+
Array.from(sortableSet).should.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
19+
});
20+
21+
it("Can sort by a specified function", () => {
22+
const sortableSet = new SortableSet(
23+
[1, 1, 1, 6, 6, 1, 1, 4, 5, 2, 3, 8, 5, 7, 9, 0, 3, 1],
24+
(a, b) => a - b
25+
);
26+
sortableSet.sortWith((a, b) => b - a);
27+
Array.from(sortableSet).should.eql([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
28+
});
29+
});

‎test/statsCases/scope-hoisting-multi/expected.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Child
1919
[0] (webpack)/test/statsCases/scope-hoisting-multi/common_lazy_shared.js 25 bytes {0} {1} {2} [built]
2020
[1] (webpack)/test/statsCases/scope-hoisting-multi/vendor.js 25 bytes {5} [built]
2121
ModuleConcatenation (inner): module is an entrypoint
22-
[2] (webpack)/test/statsCases/scope-hoisting-multi/common.js + 1 modules 62 bytes {4} {3} [built]
22+
[2] (webpack)/test/statsCases/scope-hoisting-multi/common.js + 1 modules 62 bytes {3} {4} [built]
2323
[3] (webpack)/test/statsCases/scope-hoisting-multi/common_lazy.js 25 bytes {1} {2} [built]
2424
[4] (webpack)/test/statsCases/scope-hoisting-multi/first.js + 1 modules 238 bytes {4} [built]
2525
ModuleConcatenation (inner): module is an entrypoint

0 commit comments

Comments
 (0)