Skip to content

Commit a014332

Browse files
authored
Merge pull request webpack#5298 from asolove/cache-relative-paths
[Perf] Memoize relative paths to speed up builds
2 parents 0e69f1b + 0a540b4 commit a014332

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

lib/optimize/AggressiveSplittingPlugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class AggressiveSplittingPlugin {
5656

5757
const nameToModuleMap = new Map();
5858
chunk.forEachModule(m => {
59-
const name = identifierUtils.makePathsRelative(compiler.context, m.identifier());
59+
const name = identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache);
6060
nameToModuleMap.set(name, m);
6161
});
6262

@@ -127,7 +127,7 @@ class AggressiveSplittingPlugin {
127127
newChunk.origins = chunk.origins.map(copyWithReason);
128128
chunk.origins = chunk.origins.map(copyWithReason);
129129
compilation._aggressiveSplittingSplits = (compilation._aggressiveSplittingSplits || []).concat({
130-
modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()))
130+
modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache))
131131
});
132132
return true;
133133
} else {
@@ -144,7 +144,7 @@ class AggressiveSplittingPlugin {
144144
if(chunk.hasEntryModule()) return;
145145
const size = chunk.size(this.options);
146146
const incorrectSize = size < minSize;
147-
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()));
147+
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache));
148148
if(typeof chunk._fromAggressiveSplittingIndex === "undefined") {
149149
if(incorrectSize) return;
150150
chunk.recorded = true;

lib/util/identifier.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,32 @@ const looksLikeAbsolutePath = (maybeAbsolutePath) => {
77

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

10-
exports.makePathsRelative = (context, identifier) => {
10+
const _makePathsRelative = (context, identifier) => {
1111
return identifier
1212
.split(/([|! ])/)
1313
.map(str => looksLikeAbsolutePath(str) ?
1414
normalizePathSeparator(path.relative(context, str)) : str)
1515
.join("");
1616
};
17+
18+
exports.makePathsRelative = (context, identifier, cache) => {
19+
if(!cache) return _makePathsRelative(context, identifier);
20+
21+
const relativePaths = cache.relativePaths || (cache.relativePaths = new Map());
22+
23+
let cachedResult;
24+
let contextCache = relativePaths.get(context);
25+
if(typeof contextCache === "undefined") {
26+
relativePaths.set(context, contextCache = new Map());
27+
} else {
28+
cachedResult = contextCache.get(identifier);
29+
}
30+
31+
if(typeof cachedResult !== "undefined") {
32+
return cachedResult;
33+
} else {
34+
const relativePath = _makePathsRelative(context, identifier);
35+
contextCache.set(identifier, relativePath);
36+
return relativePath;
37+
}
38+
};

0 commit comments

Comments
 (0)