Skip to content

Commit 93171b9

Browse files
committed
replace isEqualResource with more performance implementation
1 parent 4019beb commit 93171b9

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

lib/Compilation.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,20 @@ 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+
let innerMap = dependencies.get(factory);
216+
if(innerMap === undefined)
217+
dependencies.set(factory, innerMap = new Map());
218+
let list = innerMap.get(resourceIdent);
219+
if(list === undefined)
220+
innerMap.set(resourceIdent, list = []);
221+
list.push(dep);
216222
}
217-
dependencies.push([dep]);
218223
};
219224

220225
const addDependenciesBlock = block => {
@@ -230,23 +235,27 @@ class Compilation extends Tapable {
230235
};
231236

232237
addDependenciesBlock(module);
233-
this.addModuleDependencies(module, dependencies, this.bail, null, true, callback);
238+
239+
const sortedDependencies = [];
240+
241+
for(const pair1 of dependencies) {
242+
for(const pair2 of pair1[1]) {
243+
sortedDependencies.push({
244+
factory: pair1[0],
245+
dependencies: pair2[1]
246+
});
247+
}
248+
}
249+
250+
this.addModuleDependencies(module, sortedDependencies, this.bail, null, true, callback);
234251
}
235252

236253
addModuleDependencies(module, dependencies, bail, cacheGroup, recursive, callback) {
237254
let _this = this;
238255
const start = _this.profile && Date.now();
239256

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];
257+
asyncLib.forEach(dependencies, (item, callback) => {
258+
const dependencies = item.dependencies;
250259

251260
const errorAndCallback = err => {
252261
err.origin = module;
@@ -267,7 +276,7 @@ class Compilation extends Tapable {
267276
semaphore.acquire(() => {
268277
if(_this === null) return semaphore.release();
269278

270-
const factory = item[0];
279+
const factory = item.factory;
271280
factory.create({
272281
contextInfo: {
273282
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: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const equalRegExp = (a, b) => {
1212
return a + "" === b + "";
1313
};
1414

15+
const regExpToString = r => r ? r + "" : "";
16+
1517
class ContextDependency extends Dependency {
1618
// options: { request, recursive, regExp, include, exclude, mode, chunkName }
1719
constructor(options) {
@@ -25,17 +27,10 @@ class ContextDependency extends Dependency {
2527
}
2628
}
2729

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;
30+
getResourceIdentifier() {
31+
return `context${this.options.request} ${this.options.recursive} ` +
32+
`${regExpToString(this.options.regExp)} ${regExpToString(this.options.include)} ${regExpToString(this.options.exclude)} ` +
33+
`${this.options.mode} ${this.options.chunkName}`;
3934
}
4035

4136
getWarnings() {

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

0 commit comments

Comments
 (0)