Skip to content

Commit 8915dbb

Browse files
authored
Merge pull request webpack#5983 from webpack/performance/module-concatenation
Performance improvements
2 parents 4019beb + 4655836 commit 8915dbb

9 files changed

Lines changed: 90 additions & 71 deletions

‎lib/Compilation.js‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,22 @@ class Compilation extends Tapable {
206206
}
207207

208208
processModuleDependencies(module, callback) {
209-
const dependencies = [];
209+
const dependencies = new Map();
210210

211211
const addDependency = dep => {
212-
for(let i = 0; i < dependencies.length; i++) {
213-
if(dep.isEqualResource(dependencies[i][0])) {
214-
return dependencies[i].push(dep);
215-
}
212+
const resourceIdent = dep.getResourceIdentifier();
213+
if(resourceIdent) {
214+
const factory = this.dependencyFactories.get(dep.constructor);
215+
if(factory === undefined)
216+
throw new Error(`No module factory available for dependency type: ${dep.constructor.name}`);
217+
let innerMap = dependencies.get(factory);
218+
if(innerMap === undefined)
219+
dependencies.set(factory, innerMap = new Map());
220+
let list = innerMap.get(resourceIdent);
221+
if(list === undefined)
222+
innerMap.set(resourceIdent, list = []);
223+
list.push(dep);
216224
}
217-
dependencies.push([dep]);
218225
};
219226

220227
const addDependenciesBlock = block => {
@@ -229,24 +236,32 @@ class Compilation extends Tapable {
229236
}
230237
};
231238

232-
addDependenciesBlock(module);
233-
this.addModuleDependencies(module, dependencies, this.bail, null, true, callback);
239+
try {
240+
addDependenciesBlock(module);
241+
} catch(e) {
242+
callback(e);
243+
}
244+
245+
const sortedDependencies = [];
246+
247+
for(const pair1 of dependencies) {
248+
for(const pair2 of pair1[1]) {
249+
sortedDependencies.push({
250+
factory: pair1[0],
251+
dependencies: pair2[1]
252+
});
253+
}
254+
}
255+
256+
this.addModuleDependencies(module, sortedDependencies, this.bail, null, true, callback);
234257
}
235258

236259
addModuleDependencies(module, dependencies, bail, cacheGroup, recursive, callback) {
237260
let _this = this;
238261
const start = _this.profile && Date.now();
239262

240-
const factories = [];
241-
for(let i = 0; i < dependencies.length; i++) {
242-
const factory = _this.dependencyFactories.get(dependencies[i][0].constructor);
243-
if(!factory) {
244-
return callback(new Error(`No module factory available for dependency type: ${dependencies[i][0].constructor.name}`));
245-
}
246-
factories[i] = [factory, dependencies[i]];
247-
}
248-
asyncLib.forEach(factories, (item, callback) => {
249-
const dependencies = item[1];
263+
asyncLib.forEach(dependencies, (item, callback) => {
264+
const dependencies = item.dependencies;
250265

251266
const errorAndCallback = err => {
252267
err.origin = module;
@@ -267,7 +282,7 @@ class Compilation extends Tapable {
267282
semaphore.acquire(() => {
268283
if(_this === null) return semaphore.release();
269284

270-
const factory = item[0];
285+
const factory = item.factory;
271286
factory.create({
272287
contextInfo: {
273288
issuer: module.nameForCondition && module.nameForCondition(),

‎lib/Dependency.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Dependency {
1212
this.optional = false;
1313
}
1414

15-
isEqualResource() {
16-
return false;
15+
getResourceIdentifier() {
16+
return null;
1717
}
1818

1919
// Returns the referenced module and export

‎lib/dependencies/ContextDependency.js‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
const Dependency = require("../Dependency");
77
const CriticalDependencyWarning = require("./CriticalDependencyWarning");
88

9-
const equalRegExp = (a, b) => {
10-
if(a === b) return true;
11-
if(typeof a !== "object" || typeof b !== "object") return false;
12-
return a + "" === b + "";
13-
};
9+
const regExpToString = r => r ? r + "" : "";
1410

1511
class ContextDependency extends Dependency {
1612
// options: { request, recursive, regExp, include, exclude, mode, chunkName }
@@ -25,17 +21,10 @@ class ContextDependency extends Dependency {
2521
}
2622
}
2723

28-
isEqualResource(other) {
29-
if(!(other instanceof ContextDependency))
30-
return false;
31-
32-
return this.options.request === other.options.request &&
33-
this.options.recursive === other.options.recursive &&
34-
equalRegExp(this.options.regExp, other.options.regExp) &&
35-
equalRegExp(this.options.include, other.options.include) &&
36-
equalRegExp(this.options.exclude, other.options.exclude) &&
37-
this.options.mode === other.options.mode &&
38-
this.options.chunkName === other.options.chunkName;
24+
getResourceIdentifier() {
25+
return `context${this.options.request} ${this.options.recursive} ` +
26+
`${regExpToString(this.options.regExp)} ${regExpToString(this.options.include)} ${regExpToString(this.options.exclude)} ` +
27+
`${this.options.mode} ${this.options.chunkName}`;
3928
}
4029

4130
getWarnings() {

‎lib/dependencies/LoaderPlugin.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class LoaderPlugin {
1919
loaderContext.loadModule = (request, callback) => {
2020
const dep = new LoaderDependency(request);
2121
dep.loc = request;
22-
compilation.addModuleDependencies(module, [
23-
[dep]
24-
], true, "lm", false, err => {
22+
const factory = compilation.dependencyFactories.get(dep.constructor);
23+
if(factory === undefined)
24+
return callback(new Error(`No module factory available for dependency type: ${dep.constructor.name}`));
25+
compilation.addModuleDependencies(module, [{
26+
factory,
27+
dependencies: [dep]
28+
}], true, "lm", false, err => {
2529
if(err) return callback(err);
2630

2731
if(!dep.module) return callback(new Error("Cannot load the module"));

‎lib/dependencies/ModuleDependency.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ class ModuleDependency extends Dependency {
1212
this.userRequest = request;
1313
}
1414

15-
isEqualResource(other) {
16-
if(!(other instanceof ModuleDependency))
17-
return false;
18-
19-
return this.request === other.request;
15+
getResourceIdentifier() {
16+
return `module${this.request}`;
2017
}
2118
}
2219

‎lib/dependencies/NullDependency.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class NullDependency extends Dependency {
1010
return "null";
1111
}
1212

13-
isEqualResource() {
14-
return false;
15-
}
16-
1713
updateHash() {}
1814
}
1915

‎lib/optimize/ModuleConcatenationPlugin.js‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ModuleHotAcceptDependency = require("../dependencies/ModuleHotAcceptDepend
99
const ModuleHotDeclineDependency = require("../dependencies/ModuleHotDeclineDependency");
1010
const ConcatenatedModule = require("./ConcatenatedModule");
1111
const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
12+
const StackedSetMap = require("../util/StackedSetMap");
1213

1314
const formatBailoutReason = msg => {
1415
return "ModuleConcatenation bailout: " + msg;
@@ -126,7 +127,7 @@ class ModuleConcatenationPlugin {
126127
}
127128
if(!currentConfiguration.isEmpty()) {
128129
concatConfigurations.push(currentConfiguration);
129-
for(const module of currentConfiguration.modules) {
130+
for(const module of currentConfiguration.getModules()) {
130131
if(module !== currentConfiguration.rootModule)
131132
usedAsInner.add(module);
132133
}
@@ -143,9 +144,9 @@ class ModuleConcatenationPlugin {
143144
for(const concatConfiguration of concatConfigurations) {
144145
if(usedModules.has(concatConfiguration.rootModule))
145146
continue;
146-
const newModule = new ConcatenatedModule(concatConfiguration.rootModule, Array.from(concatConfiguration.modules));
147-
concatConfiguration.sortWarnings();
148-
for(const warning of concatConfiguration.warnings) {
147+
const modules = concatConfiguration.getModules();
148+
const newModule = new ConcatenatedModule(concatConfiguration.rootModule, modules);
149+
for(const warning of concatConfiguration.getWarningsSorted()) {
149150
newModule.optimizationBailout.push((requestShortener) => {
150151
const reason = getBailoutReason(warning[0], requestShortener);
151152
const reasonWithPrefix = reason ? ` (<- ${reason})` : "";
@@ -156,7 +157,7 @@ class ModuleConcatenationPlugin {
156157
});
157158
}
158159
const chunks = concatConfiguration.rootModule.getChunks();
159-
for(const m of concatConfiguration.modules) {
160+
for(const m of modules) {
160161
usedModules.add(m);
161162
chunks.forEach(chunk => chunk.removeModule(m));
162163
}
@@ -259,10 +260,16 @@ class ModuleConcatenationPlugin {
259260
}
260261

261262
class ConcatConfiguration {
262-
constructor(rootModule) {
263+
constructor(rootModule, cloneFrom) {
263264
this.rootModule = rootModule;
264-
this.modules = new Set([rootModule]);
265-
this.warnings = new Map();
265+
if(cloneFrom) {
266+
this.modules = cloneFrom.modules.createChild(5);
267+
this.warnings = cloneFrom.warnings.createChild(5);
268+
} else {
269+
this.modules = new StackedSetMap();
270+
this.modules.add(rootModule);
271+
this.warnings = new StackedSetMap();
272+
}
266273
}
267274

268275
add(module) {
@@ -281,8 +288,8 @@ class ConcatConfiguration {
281288
this.warnings.set(module, problem);
282289
}
283290

284-
sortWarnings() {
285-
this.warnings = new Map(Array.from(this.warnings).sort((a, b) => {
291+
getWarningsSorted() {
292+
return new Map(this.warnings.asPairArray().sort((a, b) => {
286293
const ai = a[0].identifier();
287294
const bi = b[0].identifier();
288295
if(ai < bi) return -1;
@@ -291,19 +298,18 @@ class ConcatConfiguration {
291298
}));
292299
}
293300

301+
getModules() {
302+
return this.modules.asArray();
303+
}
304+
294305
clone() {
295-
const clone = new ConcatConfiguration(this.rootModule);
296-
for(const module of this.modules)
297-
clone.add(module);
298-
for(const pair of this.warnings)
299-
clone.addWarning(pair[0], pair[1]);
300-
return clone;
306+
return new ConcatConfiguration(this.rootModule, this);
301307
}
302308

303309
set(config) {
304310
this.rootModule = config.rootModule;
305-
this.modules = new Set(config.modules);
306-
this.warnings = new Map(config.warnings);
311+
this.modules = config.modules;
312+
this.warnings = config.warnings;
307313
}
308314
}
309315

‎lib/util/StackedSetMap.js‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class StackedSetMap {
6666
}
6767

6868
_compress() {
69+
if(this.stack.length === 1) return;
6970
this.map = new Map();
7071
for(const data of this.stack) {
7172
for(const pair of data) {
@@ -78,14 +79,27 @@ class StackedSetMap {
7879
this.stack = [this.map];
7980
}
8081

82+
asArray() {
83+
this._compress();
84+
return Array.from(this.map.entries(), pair => pair[0]);
85+
}
86+
8187
asSet() {
88+
return new Set(this.asArray());
89+
}
90+
91+
asPairArray() {
8292
this._compress();
83-
return new Set(Array.from(this.map.entries()).map(pair => pair[0]));
93+
return Array.from(this.map.entries(), pair => pair[1] === UNDEFINED_MARKER ? [pair[0], undefined] : pair);
8494
}
8595

8696
asMap() {
97+
return new Map(this.asPairArray());
98+
}
99+
100+
get size() {
87101
this._compress();
88-
return new Map(this.map.entries());
102+
return this.map.size;
89103
}
90104

91105
createChild() {

‎test/NullDependency.unittest.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ describe("NullDependency", () => {
1616

1717
it("has a null type", () => env.nullDependency.type.should.be.exactly("null"));
1818

19-
it("is not an equal resource", () => env.nullDependency.isEqualResource().should.be.False());
20-
2119
it("has update hash function", () => env.nullDependency.updateHash.should.be.Function());
2220

2321
it("does not update hash", () => {

0 commit comments

Comments
 (0)