Skip to content

Commit f00ad10

Browse files
committed
metro-file-map: Check single-segment subpaths in hierarchicalLookup with one map lookup
Stacked on #1945. ## Summary `hierarchicalLookup` walks up from a start path asking whether each ancestor contains a given subpath - it's how `DependencyGraph` finds the closest `package.json`. For every candidate ancestor it joined the candidate's path to the subpath and traversed the result from the root again, even though the traversal that collected the ancestors already holds each candidate's node. (The `start` argument the loop built for this was never read.) When the subpath is a single path segment, which is the only way Metro calls it, each candidate is now checked with one `Map.get` on its own node. The API is unchanged, and a deep subpath behaves exactly as before. Whether the fast path applies is decided once per call, by checking the subpath for a path separator. With a single segment, two cases still traverse: - A symlinked child, because we need to follow it to learn its type and real path. - Ancestors of the project root. Their nodes don't hold the segment that leads back down towards the root (with a root of `/A/B/C`, the node for `/A/B` has no `C` entry), so a map lookup there could wrongly report a directory as missing. An empty subpath, `.` and `..` also take the traversal, since a map lookup would answer those differently. Observations are recorded exactly as before - the existing test table, which asserts the recorded sets, passes unchanged. I also tried the more explicit alternative of having the caller pass either a `basename` or a `subpath`, which avoids the separator check. It measured 387ns per walk against 394ns for this, within noise, and it changes the call shape for every caller - including Expo CLI's `createTypescriptResolver`, which passes deep subpaths - so it isn't worth it. ## Benchmark Same harness as #1944 (6,935 resolutions, 7 interleaved rounds). Resolver output is byte-identical. | metric | #1945 | this diff | |---|---|---| | first pass after startup (ms) | 85.2 | **82.2 (−3.5%)** | | warm pass (ms) | 53.3 | 54.8 | | after a source change (ms) | 61.1 | 62.3 | | after a `package.json` change (ms) | 64.2 | 64.2 | Only the first pass can move. The other passes make no `hierarchicalLookup` calls at all, because they're served by `PackageCache`, so those rows are run-to-run noise. Isolating the walks themselves, by replaying the 4,483 closest-package walks of one pass directly against `hierarchicalLookup`: 870ns -> 406ns per walk (−53%). A deep subpath (`node_modules/react/package.json` from the same start paths) is 1,084ns -> 1,111ns, for the extra branch per candidate. ## Test plan ``` yarn jest packages/metro-file-map packages/metro-resolver packages/metro/src/node-haste packages/metro/src/DeltaBundler/__tests__/resolver-test.js yarn flow check yarn typecheck-ts yarn verify-api-snapshots ``` New tests cover a directory-type lookup with a single-segment subpath, and a deep subpath. The existing fixture already covers a symlinked `package.json`, a broken link, and a directory named `package.json`. Changelog: Internal
1 parent b28de70 commit f00ad10

2 files changed

Lines changed: 99 additions & 17 deletions

File tree

‎packages/metro-file-map/src/lib/TreeFS.js‎

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,9 @@ export default class TreeFS implements MutableFileSystem {
845845
* X = dirname(X)
846846
* while X !== dirname(X)
847847
*
848+
* A subpath that is a single path segment, such as `package.json`, is
849+
* checked with one map lookup per candidate rather than a traversal.
850+
*
848851
* If `observations` is given, records the canonical paths this result
849852
* depends upon.
850853
*
@@ -864,6 +867,12 @@ export default class TreeFS implements MutableFileSystem {
864867
absolutePath: string,
865868
containerRelativePath: string,
866869
} {
870+
// Only a single path segment can be looked up on a candidate's own node.
871+
const isBasename =
872+
subpath !== '' &&
873+
subpath !== '.' &&
874+
subpath !== '..' &&
875+
!subpath.includes(path.sep);
867876
const ancestorsOfInput: Array<{
868877
ancestorOfRootIdx: ?number,
869878
node: DirectoryNode,
@@ -882,7 +891,11 @@ export default class TreeFS implements MutableFileSystem {
882891
subpath,
883892
opts.subpathType,
884893
observations,
885-
null,
894+
isBasename &&
895+
(closestLookup.ancestorOfRootIdx == null ||
896+
closestLookup.ancestorOfRootIdx === 0)
897+
? closestLookup.node
898+
: null,
886899
);
887900
if (maybeAbsolutePathMatch != null) {
888901
return {
@@ -955,14 +968,7 @@ export default class TreeFS implements MutableFileSystem {
955968
subpath,
956969
opts.subpathType,
957970
observations,
958-
{
959-
ancestorOfRootIdx: candidate.ancestorOfRootIdx,
960-
node: candidate.node,
961-
pathIdx:
962-
candidate.normalPath.length > 0
963-
? candidate.normalPath.length + 1
964-
: 0,
965-
},
971+
isBasename ? candidate.node : null,
966972
);
967973
if (maybeAbsolutePathMatch != null) {
968974
// Determine the input path relative to the current candidate. Note
@@ -1002,7 +1008,9 @@ export default class TreeFS implements MutableFileSystem {
10021008
subpath,
10031009
opts.subpathType,
10041010
observations,
1005-
null,
1011+
isBasename && depthBelowCommonRoot === 0 && commonRootDepth === 0
1012+
? nextNode
1013+
: null,
10061014
);
10071015
if (maybeAbsolutePathMatch != null) {
10081016
const rootDirParts = this.#pathUtils.getParts();
@@ -1031,17 +1039,49 @@ export default class TreeFS implements MutableFileSystem {
10311039
return null;
10321040
}
10331041

1042+
/**
1043+
* Whether the real directory at `normalCandidatePath` has `subpath` of the
1044+
* given type, returning the absolute real path of the match.
1045+
*
1046+
* `candidateNode` is given when `subpath` is a single path segment and the
1047+
* candidate is the root or a descendant of it, and the child is then checked
1048+
* with one map lookup. It is omitted for an ancestor of the root, whose node
1049+
* does not hold the segment leading back towards the root, and for a deep
1050+
* subpath. Those are found by traversal instead.
1051+
*/
10341052
#checkCandidateHasSubpath(
10351053
normalCandidatePath: string,
10361054
subpath: string,
10371055
subpathType: 'f' | 'd',
10381056
observations: ?Observations,
1039-
start: ?{
1040-
ancestorOfRootIdx: ?number,
1041-
node: DirectoryNode,
1042-
pathIdx: number,
1043-
},
1057+
candidateNode: ?DirectoryNode,
10441058
): ?string {
1059+
if (candidateNode != null) {
1060+
const childNode = candidateNode.get(subpath);
1061+
// A symlink must be traversed to learn its type and real path, so only
1062+
// a missing child, a directory or a regular file is answered here.
1063+
if (
1064+
childNode == null ||
1065+
isDirectory(childNode) ||
1066+
isRegularFile(childNode)
1067+
) {
1068+
const isMatch =
1069+
childNode != null && isDirectory(childNode) === (subpathType === 'd');
1070+
if (!isMatch && observations == null) {
1071+
return null;
1072+
}
1073+
const childNormalPath =
1074+
normalCandidatePath === ''
1075+
? subpath
1076+
: normalCandidatePath + path.sep + subpath;
1077+
if (observations) {
1078+
observations.existence.add(childNormalPath);
1079+
}
1080+
return isMatch
1081+
? this.#pathUtils.normalToAbsolute(childNormalPath)
1082+
: null;
1083+
}
1084+
}
10451085
const lookupResult = this.#lookupByNormalPath(
10461086
this.#pathUtils.joinNormalToRelative(normalCandidatePath, subpath)
10471087
.normalPath,

‎packages/metro-file-map/src/lib/__tests__/TreeFS-test.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,48 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
471471
});
472472
});
473473

474+
test('finds the closest directory with a single-segment subpath', () => {
475+
const observations = emptyObservations();
476+
expect(
477+
tfs.hierarchicalLookup(
478+
p('/A/B/C/a/n_m/pkg/subpath/deep'),
479+
'n_m',
480+
{breakOnSegment: null, subpathType: 'd'},
481+
observations,
482+
),
483+
).toEqual({
484+
absolutePath: p('/A/B/C/a/n_m/pkg/n_m'),
485+
containerRelativePath: p('subpath/deep'),
486+
});
487+
expect(observations).toEqual({
488+
content: new Set(),
489+
existence: new Set([
490+
p('a/n_m/pkg/subpath/deep/n_m'),
491+
p('a/n_m/pkg/subpath/n_m'),
492+
p('a/n_m/pkg/n_m'),
493+
]),
494+
});
495+
});
496+
497+
test('finds the closest match for a deep subpath', () => {
498+
const observations = emptyObservations();
499+
expect(
500+
tfs.hierarchicalLookup(
501+
p('/A/B/C/a/b/c'),
502+
p('n_m/pkg'),
503+
{breakOnSegment: null, subpathType: 'd'},
504+
observations,
505+
),
506+
).toEqual({
507+
absolutePath: p('/A/B/C/a/n_m/pkg'),
508+
containerRelativePath: p('b/c'),
509+
});
510+
expect(observations).toEqual({
511+
content: new Set(),
512+
existence: new Set([p('a/b/c/n_m'), p('a/b/n_m'), p('a/n_m/pkg')]),
513+
});
514+
});
515+
474516
// The fourth column is paths, besides the match itself, whose addition or
475517
// removal changes the result, the fifth those whose modification does too -
476518
// the symlinks traversed.
@@ -605,6 +647,8 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
605647
) => {
606648
const pathMap = (normalPosixPath: string) =>
607649
mockPathModule.resolve(p('/A/B/C'), p(normalPosixPath));
650+
const toCanonical = (posixPath: string) =>
651+
canonicalTo(p('/A/B/C'))(pathMap(posixPath));
608652
const observations = emptyObservations();
609653
expect(
610654
tfs.hierarchicalLookup(
@@ -624,8 +668,6 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
624668
containerRelativePath: p(expectedRelativeSubpath),
625669
},
626670
);
627-
const toCanonical = (posixPath: string) =>
628-
canonicalTo(p('/A/B/C'))(pathMap(posixPath));
629671
expect(observations).toEqual({
630672
content: new Set(expectedContent.map(toCanonical)),
631673
existence: new Set(

0 commit comments

Comments
 (0)