Repository navigation
Conversation
This is needed because we won't mark unvisited files as changed anymore. This causes them to not be read or processed. We could exclude symlinks from this, but the lazy symlinking makes sense in conjunction with the lazy lstat change. This is a trade-off, we skip the readlinks on startup that are async, and instead run them synchronously. However, they're skipped if the symlink is never accessed, which in workspaces with isolated dependency installations can be beneficial. In most cases, this is unlikely to impact performance as much as eagerly populating symlinks, as the previous code seems to mostly assume a low number of symlinks anyway (which isn't true for isolated installations)
ad0b094 to
3c2d74c
Compare
|
This (LLM-generated) branch of a squash of this PR and #1687 combined demonstrates the rough performance when both changes are combined: https://github.com/kitten/metro/tree/%40kitten/bench/metro-file-map/crawl-and-lookup The goal (which seems to be reached, at least in the synthetic tests on the branch) is to keep performance roughly the same when all files are accessed. Performance should improve when a large amount of crawled files are never accessed. |
Resolves conflicts with react#1687 (symlink targets stored lexically resolved, as normal POSIX paths) and react#1693 (native `find` crawler removed). - The Node crawler keeps this PR's lazy `lstat` inside main's single `find` path. - Symlinks with a known `mtime` are still read eagerly by `FileMap#maybeReadLink`, which now stores main's format. A symlink crawled without an `lstat` is still `1` until a lookup traverses it, and `TreeFS#readSymlinkTarget` then reads it and stores the target in that same format, so a target read lazily and one read eagerly are indistinguishable. This replaces the `WeakMap`-based `#resolveSymlinkTargetToNormalPath`, which main removed. - The `find` binary tests are dropped along with the crawler they covered, the committed `types/` defs are dropped as on main, and the tests mock `node:fs` to match main's imports.
|
AI: I've merged Benchmarked the current head (d7e1754) against The first ten rows are
Bold where the CI excludes zero. |
|
AI: I split this into its parts to see where the numbers above come from. Same app, benchmarks and stats as above, against
Changes are the median per-round delta against
|
Resolves conflicts with react#1677, which rewrote the Node crawler: its crawler already checks the extension before `lstat` and builds normal paths per directory, so this PR's crawler change reduces to skipping the `lstat` for a file with no previous mtime, reading `previousState` from the crawler options. The crawler tests follow: new files are now recorded without an `lstat`, the filesystem root test checks the path passed to `ignore()` rather than `lstat`, and `forceNodeFilesystemAPI` (removed by react#1693) is dropped from this PR's new tests. Also updates the API snapshot for `FileSystem.getMtimeByNormalPath`.
|
High level thoughts having looked closely at this and rebased on two related, now merged ones:
Parking this for now, but hopefully we can get the benefits of lazy statting soon. |
|
Didn't see the comment here before, but to answer this small part on why it's in here:
The problem that's solving is "untouched" symlinks. The cost of the synchronous I/O amortises over a bundling pass given a set of Benchmarking this without a few "real" projects (that e.g. use pnpm w/ isoated deps) can be tricky, and the gains may depend largely on the structure of such a target project. |
Without Watchman, the node crawler `lstat`s every file on every start, so that `getDifference` can tell what changed while Metro wasn't running: https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/crawlers/node/index.js#L83-L96 Most of those files are never read, and an mtime only matters for a file with something derived from it (a SHA-1 or plugin data) - that's all a change can invalidate. This takes the idea in @kitten's #1686 and makes it hold across restarts. A regular file is crawled without a stat, and its mtime is recorded when data is first derived from it, from a stat taken before it's read. A file with no mtime then has nothing that could be stale, so a start only stats the files that had an mtime before. - A recrawl while watching still stats every file, because consumers like `PackageCache` hold contents read outside the file map. - Symlinks are still read eagerly - every lookup through a link needs its target. - `linkStats()` gives a null `modifiedTime` and `size` for a file that hasn't been stat'd. - `getDifference()` also returns the files it can't compare without a stat (`filesToStat`), so the crawler stats only those. - `CACHE_BREAKER` is bumped, or an existing cache would keep every file stat'd. ## Benchmark **Without Watchman, this takes 43-50% off a cold or warm start on Linux and Windows** (Linux warm: 635ms -> 360ms), and 25-27% off on macOS. Watch mode gains less on Linux and Windows, where setting up the watcher dominates - the rest of this stack deals with that. The cost is a stat when a file is first read: 9-30% more on a first SHA-1 pass over the bundle's files (6-46ms here), once per file per cache. | | `main` | This PR | vs `main` | |---|---|---|---| | macOS: Cold start | 127ms | 93ms | -26.7% (-35.5% to -19.2%) | | Linux: Cold start | 645ms | 331ms | -48.8% (-49.6% to -48.0%) | | Windows: Cold start | 361ms | 182ms | -49.7% (-51.1% to -49.2%) | | macOS: Warm start | 119ms | 90ms | -24.8% (-26.1% to -22.7%) | | Linux: Warm start | 635ms | 360ms | -43.3% (-44.1% to -42.6%) | | Windows: Warm start | 354ms | 203ms | -42.6% (-43.0% to -42.1%) | | macOS: Watch-mode start, warm cache | 130ms | 104ms | -20.0% (-21.2% to -18.2%) | | Linux: Watch-mode start, warm cache | 1,887ms | 1,614ms | -14.5% (-15.4% to -13.7%) | | Windows: Watch-mode start, warm cache | 2,577ms | 2,417ms | -6.2% (-6.7% to -5.8%) | | macOS: First SHA-1 of the bundle's 3,125 files | 68ms | 74ms | +8.8% (+7.7% to +10.6%) | | Linux: First SHA-1 of the bundle's 3,125 files | 100ms | 130ms | +29.6% (+24.1% to +37.2%) | | Windows: First SHA-1 of the bundle's 3,125 files | 258ms | 304ms | +17.7% (+14.9% to +19.2%) | ## Changelog ``` - **[Performance]**: metro-file-map: Start faster without Watchman, by not stat-ing files whose contents Metro hasn't read ``` ## Test plan New unit tests, and a `stats-between-starts-test.js` that edits files between two runs of FileMap over a real directory and cache. SHA-1s, eager and lazy plugin data and a retargeted symlink are recomputed, and an unread file is stat'd only once it's read. These include the cases #1686 gets wrong. Benchmark app (AI-driven harness, used throughout this stack): a React Native 0.87.1 app with 23 common dependencies, 34k files in the file map and 3.1k modules in its iOS dev bundle, started through Metro's `DependencyGraph` with `resolver.useWatchman: false`. Each round runs, per variant: 2 cold starts, a watch-mode session that computes the SHA-1s of the bundle's files (as transforming them does) and saves them to the cache, 3 warm starts and 3 watch-mode starts from that cache, and 2 watch-mode cold starts. 10 interleaved rounds per OS, alternating the variant order, then medians and bootstrap 95% CIs. Heap is measured after a forced GC. Machines: Mac mini M4 Pro (macOS, APFS, Node 26.11.1), Mac mini i5-4278U (Linux Mint 22.3, ext4, Node 24.21.0) and a Ryzen 7 7730U laptop (Windows 11, NTFS, Defender on, Node 24.21.0). Measured on 1461d81 (`main` on 2026-11-10), with this stack on top.
Summary
Note
This is part of the same pick/patch-set from an experiment on the Expo repo,
like #1676, #1677, and #1687, for
metro-file-map(conflicts are expected, since changes were pulled out into separate PRs for clarity)After #1677 the highest impact on initial crawling performance with the Node crawler are the individual
lstatcalls andreadlinkcalls. The former happens for every file that's discovered and can hence get quite large for monorepos (or assets that aren't excluded with blocklists). The latter, readlinks, are expensive if we're in an isolated installation (pnpm & bun), which can contain a much larger amount of symlinks than was likely anticipated by the current code paths.This is the last (and most minimal iteration) of a few attempts to reduce the cost.
The idea is to skip the
lstatcall for new files that aren't in the previous FS snapshot (entry doesn't exist). To make this safe, these files are also excluded fromgetDifference, i.e. when both the previous and new entries have0 | nullmtime values. In both paths0 | nullindicates that the file is skipped forlstat.The
mtimeis later added via anfs.promises.lstatcall ingetOrComputeSha1i.e. when the file is actually accessed. If the file isn't accessed the mtime value remains at null.This cascades to require/allow us to lazily evaluate symlinks for mtime values of null. Their values aren't read until accessed in
#resolveSymlinkTargetToNormalPathinTreeFS. This does introduce an update on read, but lazily populates the symlink value. This trades off the asyncreadlinkcall on all symlinks for a syncreadlinkcall on a subset of symlinks.We've observed this to be an overall win, but this could be influenced further by this commit: expo/expo@af67435 (which could also be upstreamed but changes the
getContentsignature. This change can be avoided by awaiting first, but seems overall like a better change than awaitinggetContentunconditionally before plugins)Changelog: [Internal] Lazily stat Node crawled files and lazily populate symlink targets
Test plan