Skip to content

Commit 4e5ef0d

Browse files
committed
use SortableSet in modules
use SortableSet to keep "_chunks" sorted
1 parent 747efca commit 4e5ef0d

1 file changed

Lines changed: 17 additions & 33 deletions

File tree

lib/Module.js

Lines changed: 17 additions & 33 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) {
13-
return a.id - b.id;
14-
}
15-
16-
function byDebugId(a, b) {
17-
return a.debugId - b.debugId;
18-
}
19-
2014
let debugId = 1000;
2115

2216
class Module extends DependenciesBlock {
17+
18+
static sortById(a, b) {
19+
return a.id - b.id;
20+
}
21+
22+
static sortByDebugId(a, b) {
23+
return a.debugId - b.debugId;
24+
}
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, Module.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,12 @@ 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

7877
addChunk(chunk) {
7978
this._chunks.add(chunk);
8079
this._chunksDebugIdent = undefined;
81-
this._chunksIsSorted = this._chunksIsSortedByDebugId = false;
8280
}
8381

8482
removeChunk(chunk) {
@@ -96,7 +94,7 @@ class Module extends DependenciesBlock {
9694

9795
getChunkIdsIdent() {
9896
if(this._chunksDebugIdent !== undefined) return this._chunksDebugIdent;
99-
this._ensureChunksSortedByDebugId();
97+
this._chunks.sortWith(Module.sortByDebugId);
10098
const chunks = this._chunks;
10199
const list = [];
102100
for(const chunk of chunks) {
@@ -130,8 +128,8 @@ class Module extends DependenciesBlock {
130128

131129
hasEqualsChunks(otherModule) {
132130
if(this._chunks.size !== otherModule._chunks.size) return false;
133-
this._ensureChunksSortedByDebugId();
134-
otherModule._ensureChunksSortedByDebugId();
131+
this._chunks.sortWith(Module.sortByDebugId);
132+
otherModule._chunks.sortWith(Module.sortByDebugId);
135133
const a = this._chunks[Symbol.iterator]();
136134
const b = otherModule._chunks[Symbol.iterator]();
137135
while(true) { // eslint-disable-line
@@ -142,20 +140,6 @@ class Module extends DependenciesBlock {
142140
}
143141
}
144142

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-
159143
addReason(module, dependency) {
160144
this.reasons.push(new ModuleReason(module, dependency));
161145
}
@@ -221,8 +205,8 @@ class Module extends DependenciesBlock {
221205
sortItems(sortChunks) {
222206
super.sortItems();
223207
if(sortChunks)
224-
this._ensureChunksSorted();
225-
this.reasons.sort((a, b) => byId(a.module, b.module));
208+
this._chunks.sort();
209+
this.reasons.sort((a, b) => Module.sortById(a.module, b.module));
226210
if(Array.isArray(this.usedExports)) {
227211
this.usedExports.sort();
228212
}

0 commit comments

Comments
 (0)