Merge pull request #9906 from webpack/perf/smaller-improvements

Smaller performance improvements
This commit is contained in:
Tobias Koppers 2019-10-28 16:16:28 +01:00 committed by GitHub
commit 6f88c5f1e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 67 deletions

View File

@ -175,6 +175,16 @@ class ChunkGraph {
this.moduleGraph = moduleGraph;
this._getGraphRoots = this._getGraphRoots.bind(this);
// Caching
this._cacheChunkGraphModuleKey1 = undefined;
this._cacheChunkGraphModuleValue1 = undefined;
this._cacheChunkGraphModuleKey2 = undefined;
this._cacheChunkGraphModuleValue2 = undefined;
this._cacheChunkGraphChunkKey1 = undefined;
this._cacheChunkGraphChunkValue1 = undefined;
this._cacheChunkGraphChunkKey2 = undefined;
this._cacheChunkGraphChunkValue2 = undefined;
}
/**
@ -183,12 +193,20 @@ class ChunkGraph {
* @returns {ChunkGraphModule} internal module
*/
_getChunkGraphModule(module) {
let m = this._modules.get(module);
if (m === undefined) {
m = new ChunkGraphModule();
this._modules.set(module, m);
if (this._cacheChunkGraphModuleKey1 === module)
return this._cacheChunkGraphModuleValue1;
if (this._cacheChunkGraphModuleKey2 === module)
return this._cacheChunkGraphModuleValue2;
let cgm = this._modules.get(module);
if (cgm === undefined) {
cgm = new ChunkGraphModule();
this._modules.set(module, cgm);
}
return m;
this._cacheChunkGraphModuleKey2 = this._cacheChunkGraphModuleKey1;
this._cacheChunkGraphModuleValue2 = this._cacheChunkGraphModuleValue1;
this._cacheChunkGraphModuleKey1 = module;
this._cacheChunkGraphModuleValue1 = cgm;
return cgm;
}
/**
@ -197,12 +215,20 @@ class ChunkGraph {
* @returns {ChunkGraphChunk} internal chunk
*/
_getChunkGraphChunk(chunk) {
let c = this._chunks.get(chunk);
if (c === undefined) {
c = new ChunkGraphChunk();
this._chunks.set(chunk, c);
if (this._cacheChunkGraphChunkKey1 === chunk)
return this._cacheChunkGraphChunkValue1;
if (this._cacheChunkGraphChunkKey2 === chunk)
return this._cacheChunkGraphChunkValue2;
let cgc = this._chunks.get(chunk);
if (cgc === undefined) {
cgc = new ChunkGraphChunk();
this._chunks.set(chunk, cgc);
}
return c;
this._cacheChunkGraphChunkKey2 = this._cacheChunkGraphChunkKey1;
this._cacheChunkGraphChunkValue2 = this._cacheChunkGraphChunkValue1;
this._cacheChunkGraphChunkKey1 = chunk;
this._cacheChunkGraphChunkValue1 = cgc;
return cgc;
}
/**

View File

@ -215,6 +215,17 @@ const byNameOrHash = concatComparators(
)
);
const byMessage = compareSelect(err => `${err.message}`, compareStringsNumeric);
const byModule = compareSelect(
err => (err.module && err.module.identifier()) || "",
compareStringsNumeric
);
const byLocation = compareSelect(err => err.loc, compareLocations);
const compareErrors = concatComparators(byModule, byLocation, byMessage);
/**
* @param {Source} a a source
* @param {Source} b another source
@ -1814,41 +1825,21 @@ class Compilation {
* @param {Module} module module for processeing
* @returns {void}
*/
const enqueueJob = module => {
const processModule = module => {
if (!moduleGraph.setDepthIfLower(module, depth)) return;
queue.add(module);
};
/**
* @param {Dependency} dependency dependency to assign depth to
* @returns {void}
*/
const assignDepthToDependency = dependency => {
const module = this.moduleGraph.getModule(dependency);
if (module) {
enqueueJob(module);
}
};
/**
* @param {DependenciesBlock} block block to assign depth to
* @returns {void}
*/
const assignDepthToDependencyBlock = block => {
if (block.dependencies) {
for (const dep of block.dependencies) assignDepthToDependency(dep);
}
if (block.blocks) {
for (const b of block.blocks) assignDepthToDependencyBlock(b);
}
};
for (module of queue) {
queue.delete(module);
depth = moduleGraph.getDepth(module) + 1;
assignDepthToDependencyBlock(module);
for (const connection of moduleGraph.getOutgoingConnections(module)) {
const refModule = connection.module;
if (refModule) {
processModule(refModule);
}
}
}
}
@ -1948,17 +1939,6 @@ class Compilation {
chunkGroup.sortItems();
}
const byMessage = compareSelect(
err => `${err.message}`,
compareStringsNumeric
);
const byModule = compareSelect(
err => (err.module && err.module.identifier()) || "",
compareStringsNumeric
);
const byLocation = compareSelect(err => err.loc, compareLocations);
const compareErrors = concatComparators(byModule, byLocation, byMessage);
this.errors.sort(compareErrors);
this.warnings.sort(compareErrors);
this.children.sort(byNameOrHash);

View File

@ -206,13 +206,6 @@ class FlagDependencyExportsPlugin {
}
}
/*for (const module of modules) {
console.log(
module.identifier(),
moduleGraph.getExportsInfo(module)
);
}*/
asyncLib.each(
modulesToStore,
(module, callback) => {

View File

@ -31,6 +31,16 @@ const getHash = (str, len) => {
* @returns {string} string prefixed by an underscore if it is a number
*/
const avoidNumber = str => {
// max length of a number is 21 chars, bigger numbers a written as "...e+xx"
if (str.length > 21) return str;
const firstChar = str.charCodeAt(0);
// skip everything that doesn't look like a number
// charCodes: "-": 45, "1": 49, "9": 57
if (firstChar < 49) {
if (firstChar !== 45) return str;
} else if (firstChar > 57) {
return str;
}
if (str === +str + "") {
return `_${str}`;
}

View File

@ -8,21 +8,35 @@
/**
* @template T
* @param {Set<T>} targetSet set where items should be added
* @param {Set<Iterable<T> | LazySet<T>>} toMerge iterables or lazy set to be merged
* @param {Set<Iterable<T>>} toMerge iterables to be merged
* @returns {void}
*/
const merge = (targetSet, toMerge) => {
for (const set of toMerge) {
for (const item of set) {
targetSet.add(item);
}
}
};
/**
* @template T
* @param {Set<Iterable<T>>} targetSet set where iterables should be added
* @param {Set<Iterable<T> | LazySet<T>>} toDeepMerge iterables or lazy set to be flattened
* @returns {void}
*/
const flatten = (targetSet, toDeepMerge) => {
for (const set of toDeepMerge) {
if (set instanceof LazySet) {
for (const item of set._set) {
targetSet.add(item);
}
targetSet.add(set._set);
if (set._needMerge) {
merge(targetSet, set._toMerge);
for (const mergedSet of set._toMerge) {
targetSet.add(mergedSet);
}
flatten(targetSet, set._toDeepMerge);
}
} else {
for (const item of set) {
targetSet.add(item);
}
targetSet.add(set);
}
}
};
@ -40,13 +54,21 @@ class LazySet {
constructor(iterable) {
/** @type {Set<T>} */
this._set = new Set(iterable);
/** @type {Set<Iterable<T> | LazySet<T>>} */
/** @type {Set<Iterable<T>>} */
this._toMerge = new Set();
/** @type {Set<Iterable<T> | LazySet<T>>} */
this._toDeepMerge = new Set();
this._needMerge = false;
this._deopt = false;
}
_flatten() {
flatten(this._toMerge, this._toDeepMerge);
this._toDeepMerge.clear();
}
_merge() {
this._flatten();
merge(this._set, this._toMerge);
this._toMerge.clear();
this._needMerge = false;
@ -77,10 +99,13 @@ class LazySet {
_set.add(item);
}
} else {
this._toMerge.add(iterable);
this._toDeepMerge.add(iterable);
this._needMerge = true;
// Avoid a memory leak
if (this._toMerge.size > 100000) this._merge();
// Avoid being too memory hungry
if (this._toDeepMerge.size > 100000) {
this._flatten();
if (this._toMerge.size > 100000) this._merge();
}
}
return this;
}
@ -88,6 +113,7 @@ class LazySet {
clear() {
this._set.clear();
this._toMerge.clear();
this._toDeepMerge.clear();
this._needMerge = false;
this._deopt = false;
}