Merge pull request #9984 from smelukov/module-factory-cache

Improve getting generator/parse cache in module factory
This commit is contained in:
Tobias Koppers 2019-11-19 14:11:02 +01:00 committed by GitHub
commit 0291bd0d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 33 deletions

View File

@ -25,6 +25,7 @@ const LazySet = require("./util/LazySet");
const { cachedCleverMerge } = require("./util/cleverMerge");
const { join } = require("./util/fs");
/** @typedef {import("./Generator")} Generator */
/** @typedef {import("./ModuleFactory").ModuleFactoryCreateData} ModuleFactoryCreateData */
/** @typedef {import("./ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */
/** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
@ -42,7 +43,7 @@ const { join } = require("./util/fs");
* @property {LazySet<string>} contextDependencies
*/
const EMPTY_RESOLVE_OPTIONS = {};
const EMPTY_OBJECT = {};
const MATCH_RESOURCE_REGEX = /^([^!]+)!=!/;
@ -164,8 +165,14 @@ class NormalModuleFactory extends ModuleFactory {
: () => true;
this.context = context || "";
this.fs = fs;
this.parserCache = Object.create(null);
this.generatorCache = Object.create(null);
/**
* @type {Map<string, WeakMap<Object, TODO>>}
*/
this.parserCache = new Map();
/**
* @type {Map<string, WeakMap<Object, Generator>>}
*/
this.generatorCache = new Map();
this.hooks.factorize.tapAsync(
/** @type {TODO} */ ({
name: "NormalModuleFactory",
@ -494,7 +501,7 @@ class NormalModuleFactory extends ModuleFactory {
if (cacheEntry) return callback(null, cacheEntry);
}
const context = data.context || this.context;
const resolveOptions = data.resolveOptions || EMPTY_RESOLVE_OPTIONS;
const resolveOptions = data.resolveOptions || EMPTY_OBJECT;
const dependency = dependencies[0];
const request = dependency.request;
const contextInfo = data.contextInfo;
@ -621,19 +628,22 @@ class NormalModuleFactory extends ModuleFactory {
);
}
getParser(type, parserOptions) {
let ident = type;
if (parserOptions) {
if (parserOptions.ident) {
ident = `${type}|${parserOptions.ident}`;
} else {
ident = JSON.stringify([type, parserOptions]);
}
getParser(type, parserOptions = EMPTY_OBJECT) {
let cache = this.parserCache.get(type);
if (cache === undefined) {
cache = new WeakMap();
this.parserCache.set(type, cache);
}
if (ident in this.parserCache) {
return this.parserCache[ident];
let parser = cache.get(parserOptions);
if (parser === undefined) {
parser = this.createParser(type, parserOptions);
cache.set(parserOptions, parser);
}
return (this.parserCache[ident] = this.createParser(type, parserOptions));
return parser;
}
createParser(type, parserOptions = {}) {
@ -645,22 +655,22 @@ class NormalModuleFactory extends ModuleFactory {
return parser;
}
getGenerator(type, generatorOptions) {
let ident = type;
if (generatorOptions) {
if (generatorOptions.ident) {
ident = `${type}|${generatorOptions.ident}`;
} else {
ident = JSON.stringify([type, generatorOptions]);
}
getGenerator(type, generatorOptions = EMPTY_OBJECT) {
let cache = this.generatorCache.get(type);
if (cache === undefined) {
cache = new WeakMap();
this.generatorCache.set(type, cache);
}
if (ident in this.generatorCache) {
return this.generatorCache[ident];
let generator = cache.get(generatorOptions);
if (generator === undefined) {
generator = this.createGenerator(type, generatorOptions);
cache.set(generatorOptions, generator);
}
return (this.generatorCache[ident] = this.createGenerator(
type,
generatorOptions
));
return generator;
}
createGenerator(type, generatorOptions = {}) {
@ -675,10 +685,7 @@ class NormalModuleFactory extends ModuleFactory {
}
getResolver(type, resolveOptions) {
return this.resolverFactory.get(
type,
resolveOptions || EMPTY_RESOLVE_OPTIONS
);
return this.resolverFactory.get(type, resolveOptions || EMPTY_OBJECT);
}
}