Skip to content

Commit

Permalink
perf(pnp): cache findApiPath lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Jun 13, 2020
1 parent 3c904ab commit 206b955
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
36 changes: 34 additions & 2 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

29 changes: 27 additions & 2 deletions packages/yarnpkg-pnp/sources/loader/makeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,49 @@ export function makeManager(pnpapi: PnpApi, opts: MakeManagerOptions) {
return apiEntry;
}

const findApiPathCache = new Map<PortablePath,PortablePath | null>();

function addToCache(start: PortablePath, end: PortablePath, target: PortablePath | null) {
let curr;
let next = start;

do {
curr = next;
findApiPathCache.set(curr, target);
next = ppath.dirname(curr);
} while (curr !== end);
}

function findApiPathFor(modulePath: NativePath) {
let curr: PortablePath;
let next = ppath.resolve(npath.toPortablePath(modulePath));
const start = next;

do {
curr = next;

const cached = findApiPathCache.get(curr);
if (cached !== undefined) {
addToCache(start, curr, cached);
return cached;
}

const candidate = ppath.join(curr, `.pnp.js` as Filename);
if (xfs.existsSync(candidate) && xfs.statSync(candidate).isFile())
if (xfs.existsSync(candidate) && xfs.statSync(candidate).isFile()) {
addToCache(start, curr, candidate);
return candidate;
}

const cjsCandidate = ppath.join(curr, `.pnp.cjs` as Filename);
if (xfs.existsSync(cjsCandidate) && xfs.statSync(cjsCandidate).isFile())
if (xfs.existsSync(cjsCandidate) && xfs.statSync(cjsCandidate).isFile()) {
addToCache(start, curr, cjsCandidate);
return cjsCandidate;
}

next = ppath.dirname(curr);
} while (curr !== PortablePath.root);

addToCache(start, curr, null);
return null;
}

Expand Down

0 comments on commit 206b955

Please sign in to comment.