Skip to content

Commit 8148159

Browse files
authored
perf: improve resolver cache hit rate (#481)
1 parent c1093c9 commit 8148159

18 files changed

Lines changed: 188 additions & 20 deletions

File tree

lib/ResolverFactory.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -392,22 +392,7 @@ module.exports.createResolver = function createResolver(options) {
392392
{ source: "resolve", resolveOptions: { fullySpecified } },
393393
{ source: "internal-resolve", resolveOptions: { fullySpecified: false } },
394394
]) {
395-
if (unsafeCache) {
396-
plugins.push(
397-
new UnsafeCachePlugin(
398-
source,
399-
cachePredicate,
400-
/** @type {import("./UnsafeCachePlugin").Cache} */ (unsafeCache),
401-
cacheWithContext,
402-
`new-${source}`,
403-
),
404-
);
405-
plugins.push(
406-
new ParsePlugin(`new-${source}`, resolveOptions, "parsed-resolve"),
407-
);
408-
} else {
409-
plugins.push(new ParsePlugin(source, resolveOptions, "parsed-resolve"));
410-
}
395+
plugins.push(new ParsePlugin(source, resolveOptions, "parsed-resolve"));
411396
}
412397

413398
// parsed-resolve
@@ -422,7 +407,19 @@ module.exports.createResolver = function createResolver(options) {
422407
plugins.push(new NextPlugin("after-parsed-resolve", "described-resolve"));
423408

424409
// described-resolve
425-
plugins.push(new NextPlugin("described-resolve", "raw-resolve"));
410+
if (unsafeCache) {
411+
plugins.push(
412+
new UnsafeCachePlugin(
413+
"described-resolve",
414+
cachePredicate,
415+
/** @type {import("./UnsafeCachePlugin").Cache} */ (unsafeCache),
416+
cacheWithContext,
417+
"raw-resolve",
418+
),
419+
);
420+
} else {
421+
plugins.push(new NextPlugin("described-resolve", "raw-resolve"));
422+
}
426423
if (fallback.length > 0) {
427424
plugins.push(
428425
new AliasPlugin("described-resolve", fallback, "internal-resolve"),

lib/UnsafeCachePlugin.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,55 @@
55

66
"use strict";
77

8+
const { cachedJoin } = require("./util/path");
9+
810
/** @typedef {import("./Resolver")} Resolver */
911
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
1012
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
1113
/** @typedef {import("./Resolver").ResolveContextYield} ResolveContextYield */
1214
/** @typedef {{ [k: string]: undefined | ResolveRequest | ResolveRequest[] }} Cache */
1315

16+
const RELATIVE_REQUEST_REGEXP = /^\.\.?(?:\/|$)/;
17+
18+
/**
19+
* @param {string} relativePath relative path from package root
20+
* @param {string} request relative request
21+
* @returns {string} normalized request with a preserved leading dot
22+
*/
23+
function joinRelativePreservingLeadingDot(relativePath, request) {
24+
const normalized = cachedJoin(relativePath, request);
25+
return RELATIVE_REQUEST_REGEXP.test(normalized)
26+
? normalized
27+
: `./${normalized}`;
28+
}
29+
30+
/**
31+
* @param {ResolveRequest} request request
32+
* @returns {string | false | undefined} normalized path
33+
*/
34+
function getCachePath(request) {
35+
if (request.descriptionFileRoot && !request.module) {
36+
return request.descriptionFileRoot;
37+
}
38+
return request.path;
39+
}
40+
41+
/**
42+
* @param {ResolveRequest} request request
43+
* @returns {string | undefined} normalized request string
44+
*/
45+
function getCacheRequest(request) {
46+
const requestString = request.request;
47+
if (
48+
!requestString ||
49+
!request.relativePath ||
50+
!RELATIVE_REQUEST_REGEXP.test(requestString)
51+
) {
52+
return requestString;
53+
}
54+
return joinRelativePreservingLeadingDot(request.relativePath, requestString);
55+
}
56+
1457
/**
1558
* @param {string} type type of cache
1659
* @param {ResolveRequest} request request
@@ -21,10 +64,10 @@ function getCacheId(type, request, withContext) {
2164
return JSON.stringify({
2265
type,
2366
context: withContext ? request.context : "",
24-
path: request.path,
67+
path: getCachePath(request),
2568
query: request.query,
2669
fragment: request.fragment,
27-
request: request.request,
70+
request: getCacheRequest(request),
2871
});
2972
}
3073

@@ -53,7 +96,15 @@ module.exports = class UnsafeCachePlugin {
5396
resolver
5497
.getHook(this.source)
5598
.tapAsync("UnsafeCachePlugin", (request, resolveContext, callback) => {
56-
if (!this.filterPredicate(request)) return callback();
99+
if (!this.filterPredicate(request)) {
100+
return resolver.doResolve(
101+
target,
102+
request,
103+
null,
104+
resolveContext,
105+
callback,
106+
);
107+
}
57108
const isYield = typeof resolveContext.yield === "function";
58109
const cacheId = getCacheId(
59110
isYield ? "yield" : "default",

test/fixtures/unsafe-cache-normalization/node_modules/react/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/unsafe-cache-normalization/node_modules/react/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/unsafe-cache-normalization/node_modules/shared/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/unsafe-cache-normalization/node_modules/shared/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "unsafe-cache-normalization"
3+
}

test/fixtures/unsafe-cache-normalization/packages/nested/node_modules/react/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/unsafe-cache-normalization/packages/nested/node_modules/react/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "unsafe-cache-normalization-nested"
3+
}

0 commit comments

Comments
 (0)