Skip to content

Commit

Permalink
Merge pull request webpack#5298 from asolove/cache-relative-paths
Browse files Browse the repository at this point in the history
[Perf] Memoize relative paths to speed up builds
  • Loading branch information
sokra authored Jul 18, 2017
2 parents 0e69f1b + 0a540b4 commit a014332
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/optimize/AggressiveSplittingPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AggressiveSplittingPlugin {

const nameToModuleMap = new Map();
chunk.forEachModule(m => {
const name = identifierUtils.makePathsRelative(compiler.context, m.identifier());
const name = identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache);
nameToModuleMap.set(name, m);
});

Expand Down Expand Up @@ -127,7 +127,7 @@ class AggressiveSplittingPlugin {
newChunk.origins = chunk.origins.map(copyWithReason);
chunk.origins = chunk.origins.map(copyWithReason);
compilation._aggressiveSplittingSplits = (compilation._aggressiveSplittingSplits || []).concat({
modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()))
modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache))
});
return true;
} else {
Expand All @@ -144,7 +144,7 @@ class AggressiveSplittingPlugin {
if(chunk.hasEntryModule()) return;
const size = chunk.size(this.options);
const incorrectSize = size < minSize;
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()));
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache));
if(typeof chunk._fromAggressiveSplittingIndex === "undefined") {
if(incorrectSize) return;
chunk.recorded = true;
Expand Down
24 changes: 23 additions & 1 deletion lib/util/identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@ const looksLikeAbsolutePath = (maybeAbsolutePath) => {

const normalizePathSeparator = (p) => p.replace(/\\/g, "/");

exports.makePathsRelative = (context, identifier) => {
const _makePathsRelative = (context, identifier) => {
return identifier
.split(/([|! ])/)
.map(str => looksLikeAbsolutePath(str) ?
normalizePathSeparator(path.relative(context, str)) : str)
.join("");
};

exports.makePathsRelative = (context, identifier, cache) => {
if(!cache) return _makePathsRelative(context, identifier);

const relativePaths = cache.relativePaths || (cache.relativePaths = new Map());

let cachedResult;
let contextCache = relativePaths.get(context);
if(typeof contextCache === "undefined") {
relativePaths.set(context, contextCache = new Map());
} else {
cachedResult = contextCache.get(identifier);
}

if(typeof cachedResult !== "undefined") {
return cachedResult;
} else {
const relativePath = _makePathsRelative(context, identifier);
contextCache.set(identifier, relativePath);
return relativePath;
}
};

0 comments on commit a014332

Please sign in to comment.