Repository navigation
Commit f00ad10
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: Internal1 parent b28de70 commit f00ad10
2 files changed
Lines changed: 99 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
845 | 845 | | |
846 | 846 | | |
847 | 847 | | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
848 | 851 | | |
849 | 852 | | |
850 | 853 | | |
| |||
864 | 867 | | |
865 | 868 | | |
866 | 869 | | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
867 | 876 | | |
868 | 877 | | |
869 | 878 | | |
| |||
882 | 891 | | |
883 | 892 | | |
884 | 893 | | |
885 | | - | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
886 | 899 | | |
887 | 900 | | |
888 | 901 | | |
| |||
955 | 968 | | |
956 | 969 | | |
957 | 970 | | |
958 | | - | |
959 | | - | |
960 | | - | |
961 | | - | |
962 | | - | |
963 | | - | |
964 | | - | |
965 | | - | |
| 971 | + | |
966 | 972 | | |
967 | 973 | | |
968 | 974 | | |
| |||
1002 | 1008 | | |
1003 | 1009 | | |
1004 | 1010 | | |
1005 | | - | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
1006 | 1014 | | |
1007 | 1015 | | |
1008 | 1016 | | |
| |||
1031 | 1039 | | |
1032 | 1040 | | |
1033 | 1041 | | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
1034 | 1052 | | |
1035 | 1053 | | |
1036 | 1054 | | |
1037 | 1055 | | |
1038 | 1056 | | |
1039 | | - | |
1040 | | - | |
1041 | | - | |
1042 | | - | |
1043 | | - | |
| 1057 | + | |
1044 | 1058 | | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
1045 | 1085 | | |
1046 | 1086 | | |
1047 | 1087 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
471 | 471 | | |
472 | 472 | | |
473 | 473 | | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
474 | 516 | | |
475 | 517 | | |
476 | 518 | | |
| |||
605 | 647 | | |
606 | 648 | | |
607 | 649 | | |
| 650 | + | |
| 651 | + | |
608 | 652 | | |
609 | 653 | | |
610 | 654 | | |
| |||
624 | 668 | | |
625 | 669 | | |
626 | 670 | | |
627 | | - | |
628 | | - | |
629 | 671 | | |
630 | 672 | | |
631 | 673 | | |
| |||
0 commit comments