You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 51d99a7
Browse filesBrowse the repository at this point in the historyBrowse files
metro-file-map: Let hierarchicalLookup take a basename, checked 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.)
This lets the caller give either `opts.subpath`, which may be deep and behaves as before, or `opts.basename`, a single path segment. A basename is checked with one `Map.get` on the candidate's own node. It isn't validated to be a single segment - that'd be a cost on every call for something the caller already knows.
With a basename, 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.
Observations are recorded exactly as before. The existing test table now runs every row both as a `basename` and as a `subpath`, and the two must agree on the result and on the recorded sets.
### Compatibility
The subpath moves from a positional argument into `opts`, so this changes the call shape for anything calling `hierarchicalLookup`. Metro's only caller passes `package.json`. Expo CLI's `createTypescriptResolver` passes deep subpaths (`node_modules/${targetPath}/tsconfig.json`) - that currently runs against `@expo/metro-file-map`'s own port of `TreeFS`, so it isn't affected, and deep subpaths remain supported here via `opts.subpath`.
## 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.9 | **81.8 (−4.8%)** |
| warm pass (ms) | 54.0 | 54.0 |
| after a source change (ms) | 61.6 | 61.8 (+0.3%) |
| after a `package.json` change (ms) | 64.4 | 63.7 (−1.1%) |
Only the first pass moves, which is expected - after that these lookups are served by `PackageCache`.
Isolating the walks themselves, by replaying the 4,483 closest-package walks of one pass directly against `hierarchicalLookup` (9 interleaved rounds):
| | per walk | |
|---|---|---|
| #1945 | 865ns | |
| this diff, `basename: 'package.json'` | **387ns** | **−55%** |
| alternative: API unchanged, take the fast path when `subpath` has no separator | 394ns | −54% |
The alternative is within noise of this, so the choice between them is about the API rather than speed. A deep subpath (`node_modules/react/package.json` from the same start paths) is 1,083ns -> 1,120ns per walk with either, about 3%, 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 by basename, and a deep subpath. The existing fixture already covers a symlinked `package.json`, a broken link, and a directory named `package.json`.
Changelog: Internal
0 commit comments