Skip to content

Commit af90de8

Browse files
authored
Merge pull request webpack#6236 from ooflorent/use_map_set
Replace dictionaries by Map or Set
2 parents d5725ff + 1590751 commit af90de8

8 files changed

+39
-46
lines changed

lib/Compilation.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class Compilation extends Tapable {
231231
this.dependencyTemplates = new Map();
232232
this.dependencyTemplates.set("hash", "");
233233
this.childrenCounters = {};
234+
this.usedChunkIds = null;
235+
this.usedModuleIds = null;
234236

235237
this._buildingModules = new Map();
236238
this._rebuildingModules = new Map();
@@ -1289,33 +1291,25 @@ class Compilation extends Tapable {
12891291
applyModuleIds() {
12901292
const unusedIds = [];
12911293
let nextFreeModuleId = 0;
1292-
const usedIds = [];
1293-
// TODO consider Map when performance has improved https://gist.github.com/sokra/234c077e1299b7369461f1708519c392
1294-
const usedIdMap = Object.create(null);
1294+
const usedIds = new Set();
12951295
if(this.usedModuleIds) {
12961296
Object.keys(this.usedModuleIds).forEach(key => {
12971297
const id = this.usedModuleIds[key];
1298-
if(!usedIdMap[id]) {
1299-
usedIds.push(id);
1300-
usedIdMap[id] = true;
1301-
}
1298+
usedIds.add(id);
13021299
});
13031300
}
13041301

13051302
const modules1 = this.modules;
13061303
for(let indexModule1 = 0; indexModule1 < modules1.length; indexModule1++) {
13071304
const module1 = modules1[indexModule1];
1308-
if(module1.id && !usedIdMap[module1.id]) {
1309-
usedIds.push(module1.id);
1310-
usedIdMap[module1.id] = true;
1305+
if(module1.id !== null) {
1306+
usedIds.add(module1.id);
13111307
}
13121308
}
13131309

1314-
if(usedIds.length > 0) {
1310+
if(usedIds.size > 0) {
13151311
let usedIdMax = -1;
1316-
for(let index = 0; index < usedIds.length; index++) {
1317-
const usedIdKey = usedIds[index];
1318-
1312+
for(const usedIdKey of usedIds) {
13191313
if(typeof usedIdKey !== "number") {
13201314
continue;
13211315
}
@@ -1326,7 +1320,7 @@ class Compilation extends Tapable {
13261320
let lengthFreeModules = nextFreeModuleId = usedIdMax + 1;
13271321

13281322
while(lengthFreeModules--) {
1329-
if(!usedIdMap[lengthFreeModules]) {
1323+
if(!usedIds.has(lengthFreeModules)) {
13301324
unusedIds.push(lengthFreeModules);
13311325
}
13321326
}
@@ -1609,20 +1603,20 @@ class Compilation extends Tapable {
16091603
}
16101604

16111605
checkConstraints() {
1612-
const usedIds = {};
1606+
const usedIds = new Set();
16131607

16141608
const modules = this.modules;
16151609
for(let indexModule = 0; indexModule < modules.length; indexModule++) {
16161610
const moduleId = modules[indexModule].id;
1617-
1618-
if(usedIds[moduleId])
1611+
if(moduleId === null) continue;
1612+
if(usedIds.has(moduleId))
16191613
throw new Error(`checkConstraints: duplicate module id ${moduleId}`);
1614+
usedIds.add(moduleId);
16201615
}
16211616

16221617
const chunks = this.chunks;
16231618
for(let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
16241619
const chunk = chunks[indexChunk];
1625-
16261620
if(chunks.indexOf(chunk) !== indexChunk)
16271621
throw new Error(`checkConstraints: duplicate chunk in compilation ${chunk.debugId}`);
16281622
chunk.checkConstraints();

lib/FlagInitialModulesAsUsedPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class FlagInitialModulesAsUsedPlugin {
1616
if(!chunk.isInitial()) {
1717
return;
1818
}
19-
chunk.forEachModule((module) => {
19+
for(const module of chunk.modulesIterable) {
2020
module.used = true;
2121
module.usedExports = true;
2222
module.addReason(null, null, this.explanation);
23-
});
23+
}
2424
});
2525
});
2626
});

lib/HotModuleReplacementPlugin.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ module.exports = class HotModuleReplacementPlugin {
115115
const currentChunk = compilation.chunks.find(chunk => chunk.id === chunkId);
116116
if(currentChunk) {
117117
const newModules = currentChunk.getModules().filter(module => module.hotUpdate);
118-
const allModules = {};
119-
currentChunk.forEachModule(module => {
120-
allModules[module.id] = true;
121-
});
122-
const removedModules = records.chunkModuleIds[chunkId].filter(id => !allModules[id]);
118+
const allModules = new Set();
119+
for(const module of currentChunk.modulesIterable) {
120+
allModules.add(module.id);
121+
}
122+
const removedModules = records.chunkModuleIds[chunkId].filter(id => !allModules.has(id));
123123
if(newModules.length > 0 || removedModules.length > 0) {
124124
const source = hotUpdateChunkTemplate.render(chunkId, newModules, removedModules, compilation.hash, compilation.moduleTemplates.javascript, compilation.dependencyTemplates);
125125
const filename = compilation.getPath(hotUpdateChunkFilename, {

lib/MultiCompiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ module.exports = class MultiCompiler extends Tapable {
141141
}
142142

143143
runWithDependencies(compilers, fn, callback) {
144-
let fulfilledNames = {};
144+
const fulfilledNames = new Set();
145145
let remainingCompilers = compilers;
146-
const isDependencyFulfilled = (d) => fulfilledNames[d];
146+
const isDependencyFulfilled = (d) => fulfilledNames.has(d);
147147
const getReadyCompilers = () => {
148148
let readyCompilers = [];
149149
let list = remainingCompilers;
@@ -162,7 +162,7 @@ module.exports = class MultiCompiler extends Tapable {
162162
asyncLib.map(getReadyCompilers(), (compiler, callback) => {
163163
fn(compiler, (err) => {
164164
if(err) return callback(err);
165-
fulfilledNames[compiler.name] = true;
165+
fulfilledNames.add(compiler.name);
166166
runCompilers(callback);
167167
});
168168
}, callback);

lib/RecordIdsPlugin.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class RecordIdsPlugin {
2727
compilation.hooks.reviveModules.tap("RecordIdsPlugin", (modules, records) => {
2828
if(!records.modules) return;
2929
if(records.modules.byIdentifier) {
30-
const usedIds = {};
30+
const usedIds = new Set();
3131
modules.forEach(module => {
3232
if(module.id !== null) return;
3333
const identifier = portableIds ? identifierUtils.makePathsRelative(compiler.context, module.identifier(), compilation.cache) : module.identifier();
3434
const id = records.modules.byIdentifier[identifier];
3535
if(id === undefined) return;
36-
if(usedIds[id]) return;
37-
usedIds[id] = true;
36+
if(usedIds.has(id)) return;
37+
usedIds.add(id);
3838
module.id = id;
3939
});
4040
}
@@ -76,15 +76,15 @@ class RecordIdsPlugin {
7676
});
7777
compilation.hooks.reviveChunks.tap("RecordIdsPlugin", (chunks, records) => {
7878
if(!records.chunks) return;
79-
const usedIds = {};
79+
const usedIds = new Set();
8080
if(records.chunks.byName) {
8181
chunks.forEach(chunk => {
8282
if(chunk.id !== null) return;
8383
if(!chunk.name) return;
8484
const id = records.chunks.byName[chunk.name];
8585
if(id === undefined) return;
86-
if(usedIds[id]) return;
87-
usedIds[id] = true;
86+
if(usedIds.has(id)) return;
87+
usedIds.add(id);
8888
chunk.id = id;
8989
});
9090
}
@@ -105,11 +105,11 @@ class RecordIdsPlugin {
105105
blockIdentsCount = Object.keys(blockIdentsCount).map(accessor => [blockIdentsCount[accessor]].concat(accessor.split(":").map(Number))).sort((a, b) => b[0] - a[0]);
106106
blockIdentsCount.forEach(arg => {
107107
const id = arg[1];
108-
if(usedIds[id]) return;
108+
if(usedIds.has(id)) return;
109109
const idx = arg[2];
110110
const chunk = argumentedChunks[idx].chunk;
111111
if(chunk.id !== null) return;
112-
usedIds[id] = true;
112+
usedIds.add(id);
113113
chunk.id = id;
114114
});
115115
}

lib/Template.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ module.exports = class Template {
152152
var maxId = bounds[1];
153153
if(minId !== 0) source.add("Array(" + minId + ").concat(");
154154
source.add("[\n");
155-
const modules = {};
155+
const modules = new Map();
156156
allModules.forEach(module => {
157-
modules[module.id] = module;
157+
modules.set(module.id, module);
158158
});
159159
for(var idx = minId; idx <= maxId; idx++) {
160-
var module = modules[idx];
160+
var module = modules.get(idx);
161161
if(idx !== minId) source.add(",\n");
162162
source.add("/* " + idx + " */");
163163
if(module) {

lib/optimize/CommonsChunkPlugin.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55
"use strict";
6-
let nextIdent = 0;
76

87
const validateOptions = require("schema-utils");
98
const schema = require("../../schemas/plugins/optimize/CommonsChunkPlugin.json");
@@ -40,7 +39,7 @@ The available options are:
4039
this.deepChildren = normalizedOptions.deepChildren;
4140
this.async = normalizedOptions.async;
4241
this.minSize = normalizedOptions.minSize;
43-
this.ident = __filename + (nextIdent++);
42+
this.alreadyOptimized = new WeakSet();
4443
}
4544

4645
normalizeOptions(options) {
@@ -93,8 +92,8 @@ You can however specify the name of the async chunk by passing the desired strin
9392
compiler.hooks.thisCompilation.tap("CommonsChunkPlugin", (compilation) => {
9493
const handler = (chunks) => {
9594
// only optimize once
96-
if(compilation[this.ident]) return;
97-
compilation[this.ident] = true;
95+
if(this.alreadyOptimized.has(compilation)) return;
96+
this.alreadyOptimized.add(compilation);
9897

9998
/**
10099
* Creates a list of "common"" chunks based on the options.

lib/optimize/EnsureChunkConditionsPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EnsureChunkConditionsPlugin {
1212
const handler = (chunks) => {
1313
let changed = false;
1414
chunks.forEach((chunk) => {
15-
chunk.forEachModule((module) => {
15+
for(const module of chunk.modulesIterable) {
1616
if(!module.chunkCondition) return;
1717
if(!module.chunkCondition(chunk)) {
1818
let usedChunks = triesMap.get(module);
@@ -30,7 +30,7 @@ class EnsureChunkConditionsPlugin {
3030
chunk.removeModule(module);
3131
changed = true;
3232
}
33-
});
33+
}
3434
});
3535
if(changed) return true;
3636
};

0 commit comments

Comments
 (0)