Skip to content

metro-file-map: Lazily stat files in the node crawler - #2104

Merged
robhogan merged 1 commit into
mainfrom
pr2104
Nov 17, 2026
Merged

robhogan merged 1 commit into
mainfrom
pr2104

Conversation

@robhogan

@robhogan robhogan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Without Watchman, the node crawler lstats every file on every start, so that getDifference can tell what changed while Metro wasn't running:

activeCalls++;
fs.lstat(file, (err, stat) => {
activeCalls--;
if (!err && stat) {
result.set(childNormal, [
stat.mtime.getTime(),
stat.size,
0,
null,
isSymbolicLink ? 1 : 0,
]);
}
resolveIfDone();

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 25, 2026
@robhogan
robhogan added this pull request to stack #2109 October 25, 2026 15:22
robhogan added a commit that referenced this pull request Oct 25, 2026
Without Watchman, `Watcher` starts its backends before the crawl and buffers their events, so that a change made during the crawl isn't missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking every root to watch each directory, and then the node crawler walks the same tree again. This lets the crawl do both. Just before the crawler lists a directory it passes it to the watcher, which watches it - the order the watcher's own walk uses, so a change still can't slip between the two. Directories the crawl skips as ignored are passed on too, and the watcher walks those itself unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

The hand-off is an internal second parameter of the built-in node crawler, so there's no API change. A `crawlerFactory` crawler, a scoped recrawl and a root added with `addRoot` keep the watcher's own walk. The longer-term shape for this is a backend that owns both crawling and watching, which this would move into.

## Benchmark

This takes another 25% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,894ms to 508ms (-73%) on Linux, and from 2,580ms to 539ms (-79%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure the same as the first PR (124ms).

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,894ms | 685ms | 508ms | -73.2% (-73.4% to -72.9%) | -25.9% (-26.4% to -25.4%) |
| Windows: Watch-mode start, warm cache | 2,580ms | 721ms | 539ms | -79.1% (-79.3% to -78.3%) | -25.2% (-27.3% to -22.3%) |
| Linux: Watch-mode start, cold cache | 1,876ms | 652ms | 472ms | -74.8% (-75.2% to -74.5%) | -27.6% (-28.5% to -26.7%) |
| Windows: Watch-mode start, cold cache | 2,567ms | 695ms | 516ms | -79.9% (-80.2% to -79.8%) | -25.7% (-28.2% to -24.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.0%) | -0.0% (-0.0% to -0.0%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.0% (-0.0% to -0.0%) |
| Linux: RSS after a watch-mode start | 197.4MB | 176.3MB | 170.9MB | -13.4% (-15.0% to -12.0%) | -3.1% (-4.6% to -1.6%) |
| Windows: RSS after a watch-mode start | 205.7MB | 182.6MB | 184.6MB | -10.3% (-10.7% to -9.4%) | +1.1% (+0.9% to +1.4%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New `Watcher-test.js` cases: the crawl's directories reach a `FallbackWatcher`, and a recrawl or a `crawlerFactory` crawler leaves it to walk. New `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Oct 25, 2026
`NativeWatcher.startWatching` resolves as soon as `fs.watch` returns, but on macOS that can be before any changes are reported. libuv only signals another thread to (re)create the FSEvents stream, and the stream reports only changes made after it starts:

https://github.com/libuv/libuv/blob/2b4b918d3381100854250c89d5159d4206daafb7/src/unix/fsevents.c#L362-L372

So a change made straight after startup can be silently missed, and since #2054 started non-Watchman backends before the crawl, so can one made during the crawl. On a slow host the gap is long enough to hit regularly - it's the remaining flake in the Native watcher integration tests, where the suite's first event never arrives. 14 of 297 macOS test jobs since #1974 hit it, across all three Node versions.

This has `Watcher` confirm each root's watch before relying on it. Once every backend has started, it writes a health check file into each root and waits for it to be reported, as `checkHealth()` already does for the project root. If a watch can't be confirmed within 10s, Metro warns and carries on. Roots added later with `addRoot` are confirmed the same way.

A backend opts in by returning `false` from a new static `isLiveOnStart()`, which only `NativeWatcher` does - Watchman subscribes from the crawl's clock, and `FallbackWatcher`'s watches are live as soon as `fs.watch` returns. It's done in `Watcher`, after all the backends have started, because `NativeWatcher`s for multiple roots share one FSEvents stream, which restarts as each root is added.

This adds ~10ms to a watch-mode start on macOS without Watchman (145ms -> 155ms on our benchmark app).

Changelog:
```
 - **[Fix]**: Fix changes made immediately after startup sometimes being missed on macOS
```

Test plan:
New `Watcher-test.js` cases cover confirming each root before the crawl, warning and carrying on when a watch can't be confirmed, confirming a root added later, and leaving backends that are live on start alone. The backend integration tests now wait for a cookie of their own, as `Watcher` does.

To reproduce the race, I patched `NativeWatcher` to drop all events for its first 300ms. A `FileMap` in watch mode with two roots, writing a file to each as soon as `build()` resolves, sees neither change without this PR (3/3 runs) and both with it (3/3).

The `metro-file-map` suite passes on macOS, Linux and Windows. Benchmark (AI-driven): the harness from #2104, 8 interleaved rounds on an M4 Pro Mac mini, n=32 warm watch-mode starts per variant (+6.7%, 95% CI +5.2% to +8.0%).
robhogan added a commit that referenced this pull request Oct 25, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan force-pushed the pr2104 branch 2 times, most recently from 2f43eb5 to 81f4c5f Compare October 26, 2026 11:50
robhogan added a commit that referenced this pull request Oct 26, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Oct 26, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan marked this pull request as ready for review November 10, 2026 11:12
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 27-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,933ms to 505ms (-74%) on Linux, and from 2,584ms to 547ms (-79%) on Windows. RSS after startup is 6MB (4%) higher on Linux, though the heap is unchanged. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 6% of #2104, which is noise: #2106 changes nothing there and measures +5.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,933ms | 716ms | 505ms | -73.9% (-74.0% to -73.6%) | -29.5% (-29.8% to -28.8%) |
| Windows: Watch-mode start, warm cache | 2,584ms | 745ms | 547ms | -78.8% (-78.9% to -78.7%) | -26.6% (-26.9% to -26.3%) |
| Linux: Watch-mode start, cold cache | 1,923ms | 706ms | 503ms | -73.8% (-74.0% to -73.4%) | -28.7% (-29.2% to -27.6%) |
| Windows: Watch-mode start, cold cache | 2,569ms | 733ms | 532ms | -79.3% (-79.4% to -79.2%) | -27.4% (-27.7% to -27.0%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.2% (-11.2% to -11.2%) | -0.0% (-0.0% to -0.0%) |
| Linux: RSS after a watch-mode start | 188.0MB | 160.9MB | 166.7MB | -11.3% (-11.7% to -10.5%) | +3.6% (+3.2% to +3.9%) |
| Windows: RSS after a watch-mode start | 209.5MB | 185.4MB | 183.8MB | -12.2% (-12.7% to -10.7%) | -0.8% (-1.0% to -0.6%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 11, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,887ms to 492ms (-74%) on Linux, and from 2,577ms to 525ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 5% of #2104, which is noise: #2106 changes nothing there and measures +4.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 695ms | 492ms | -73.9% (-74.2% to -73.7%) | -29.2% (-29.9% to -28.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 727ms | 525ms | -79.6% (-79.7% to -79.5%) | -27.7% (-28.1% to -27.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 652ms | 462ms | -75.5% (-75.8% to -75.0%) | -29.2% (-30.0% to -27.7%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 701ms | 493ms | -80.8% (-80.8% to -80.6%) | -29.6% (-29.9% to -29.3%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.3% (-11.3% to -11.3%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -0.0% (-0.0% to -0.0%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 12, 2026
Without Watchman, a watch-mode start has to be watching before it crawls, so that a change made during the crawl isn't missed. `NativeWatcher` (macOS) isn't live as soon as it starts, so `Watcher` also confirms each root's watch with a health check before the crawl begins:

https://github.com/react/metro/blob/ef50b94fba3bac242d06b20f03c7149987b3c2d6/packages/metro-file-map/src/Watcher.js#L375-L382

That takes about 13ms on macOS, and `build()` only starts it once the cache has been read, although the watch doesn't need anything from the cache.

This starts the watch, and the Watchman capability check (which spawns `watchman`), while the cache is read. The crawl still waits for the confirmed watch, so nothing can be missed that wasn't before. To allow this, `Watcher` takes the cached state in `crawl()` rather than at construction.

## Benchmark

This takes 13% off a watch-mode start on macOS (102ms -> 88ms). Linux and Windows watch with `FallbackWatcher`, which is live as soon as it starts, so they're unchanged, as are starts outside watch mode. With a cold cache there's no read to overlap with.

|  | `main` | #2220 | This PR | vs `main` | vs #2220 |
|---|---|---|---|---|---|
| macOS: Watch-mode start | 130ms | 102ms | 88ms | -32.1% (-32.8% to -31.2%) | -13.0% (-14.0% to -11.3%) |
| Linux: Watch-mode start | 1,889ms | 493ms | 491ms | -74.0% (-74.2% to -73.6%) | -0.5% (-1.4% to +0.5%) |
| Windows: Watch-mode start | 2,569ms | 524ms | 524ms | -79.6% (-79.7% to -79.5%) | +0.1% (-0.5% to +0.6%) |
| macOS: Warm start | 118ms | 88ms | 88ms | -25.1% (-26.4% to -23.6%) | +0.1% (-1.8% to +1.9%) |
| Linux: Warm start | 636ms | 353ms | 353ms | -44.5% (-45.3% to -44.1%) | -0.1% (-1.8% to +1.1%) |
| Windows: Warm start | 355ms | 201ms | 201ms | -43.4% (-43.7% to -42.7%) | +0.0% (-0.9% to +0.7%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start watching while the cache is read
```

## Test plan

New unit tests. Benchmark as in #2104, with 22 rounds on macOS.
robhogan added a commit that referenced this pull request Nov 12, 2026
Without Watchman, a watch-mode start has to be watching before it crawls, so that a change made during the crawl isn't missed. `NativeWatcher` (macOS) isn't live as soon as it starts, so `Watcher` also confirms each root's watch with a health check before the crawl begins:

https://github.com/react/metro/blob/ef50b94fba3bac242d06b20f03c7149987b3c2d6/packages/metro-file-map/src/Watcher.js#L375-L382

That takes about 13ms on macOS, and `build()` only starts it once the cache has been read, although the watch doesn't need anything from the cache.

This starts the watch, and the Watchman capability check (which spawns `watchman`), while the cache is read. The crawl still waits for the confirmed watch, so nothing can be missed that wasn't before. To allow this, `Watcher` takes the cached state in `crawl()` rather than at construction.

## Benchmark

This takes 13% off a watch-mode start on macOS (102ms -> 88ms). Linux and Windows watch with `FallbackWatcher`, which is live as soon as it starts, so they're unchanged, as are starts outside watch mode. With a cold cache there's no read to overlap with.

|  | `main` | #2220 | This PR | vs `main` | vs #2220 |
|---|---|---|---|---|---|
| macOS: Watch-mode start | 130ms | 102ms | 88ms | -32.1% (-32.8% to -31.2%) | -13.0% (-14.0% to -11.3%) |
| Linux: Watch-mode start | 1,889ms | 493ms | 491ms | -74.0% (-74.2% to -73.6%) | -0.5% (-1.4% to +0.5%) |
| Windows: Watch-mode start | 2,569ms | 524ms | 524ms | -79.6% (-79.7% to -79.5%) | +0.1% (-0.5% to +0.6%) |
| macOS: Warm start | 118ms | 88ms | 88ms | -25.1% (-26.4% to -23.6%) | +0.1% (-1.8% to +1.9%) |
| Linux: Warm start | 636ms | 353ms | 353ms | -44.5% (-45.3% to -44.1%) | -0.1% (-1.8% to +1.1%) |
| Windows: Warm start | 355ms | 201ms | 201ms | -43.4% (-43.7% to -42.7%) | +0.0% (-0.9% to +0.7%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start watching while the cache is read
```

## Test plan

New unit tests. Benchmark as in #2104, with 22 rounds on macOS.
@robhogan
robhogan requested a review from vzaidman November 12, 2026 17:27
robhogan added a commit that referenced this pull request Nov 12, 2026
Without Watchman, a watch-mode start has to be watching before it crawls, so that a change made during the crawl isn't missed. `NativeWatcher` (macOS) isn't live as soon as it starts, so `Watcher` also confirms each root's watch with a health check before the crawl begins:

https://github.com/react/metro/blob/ef50b94fba3bac242d06b20f03c7149987b3c2d6/packages/metro-file-map/src/Watcher.js#L375-L382

That takes about 13ms on macOS, and `build()` only starts it once the cache has been read, although the watch doesn't need anything from the cache.

This starts the watch, and the Watchman capability check (which spawns `watchman`), while the cache is read. The crawl still waits for the confirmed watch, so nothing can be missed that wasn't before. To allow this, `Watcher` takes the cached state in `crawl()` rather than at construction.

## Benchmark

This takes 13% off a watch-mode start on macOS (102ms -> 88ms). Linux and Windows watch with `FallbackWatcher`, which is live as soon as it starts, so they're unchanged, as are starts outside watch mode. With a cold cache there's no read to overlap with.

|  | `main` | #2220 | This PR | vs `main` | vs #2220 |
|---|---|---|---|---|---|
| macOS: Watch-mode start | 130ms | 102ms | 88ms | -32.1% (-32.8% to -31.2%) | -13.0% (-14.0% to -11.3%) |
| Linux: Watch-mode start | 1,889ms | 493ms | 491ms | -74.0% (-74.2% to -73.6%) | -0.5% (-1.4% to +0.5%) |
| Windows: Watch-mode start | 2,569ms | 524ms | 524ms | -79.6% (-79.7% to -79.5%) | +0.1% (-0.5% to +0.6%) |
| macOS: Warm start | 118ms | 88ms | 88ms | -25.1% (-26.4% to -23.6%) | +0.1% (-1.8% to +1.9%) |
| Linux: Warm start | 636ms | 353ms | 353ms | -44.5% (-45.3% to -44.1%) | -0.1% (-1.8% to +1.1%) |
| Windows: Warm start | 355ms | 201ms | 201ms | -43.4% (-43.7% to -42.7%) | +0.0% (-0.9% to +0.7%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start watching while the cache is read
```

## Test plan

New unit tests. Benchmark as in #2104, with 22 rounds on macOS.
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.
robhogan added a commit that referenced this pull request Nov 16, 2026
Without Watchman, a watch-mode start has to be watching before it crawls, so that a change made during the crawl isn't missed. `NativeWatcher` (macOS) isn't live as soon as it starts, so `Watcher` also confirms each root's watch with a health check before the crawl begins:

https://github.com/react/metro/blob/ef50b94fba3bac242d06b20f03c7149987b3c2d6/packages/metro-file-map/src/Watcher.js#L375-L382

That takes about 13ms on macOS, and `build()` only starts it once the cache has been read, although the watch doesn't need anything from the cache.

This starts the watch, and the Watchman capability check (which spawns `watchman`), while the cache is read. The crawl still waits for the confirmed watch, so nothing can be missed that wasn't before. To allow this, `Watcher` takes the cached state in `crawl()` rather than at construction.

## Benchmark

This takes 13% off a watch-mode start on macOS (102ms -> 88ms). Linux and Windows watch with `FallbackWatcher`, which is live as soon as it starts, so they're unchanged, as are starts outside watch mode. With a cold cache there's no read to overlap with.

|  | `main` | #2220 | This PR | vs `main` | vs #2220 |
|---|---|---|---|---|---|
| macOS: Watch-mode start | 130ms | 102ms | 88ms | -32.1% (-32.8% to -31.2%) | -13.0% (-14.0% to -11.3%) |
| Linux: Watch-mode start | 1,889ms | 493ms | 491ms | -74.0% (-74.2% to -73.6%) | -0.5% (-1.4% to +0.5%) |
| Windows: Watch-mode start | 2,569ms | 524ms | 524ms | -79.6% (-79.7% to -79.5%) | +0.1% (-0.5% to +0.6%) |
| macOS: Warm start | 118ms | 88ms | 88ms | -25.1% (-26.4% to -23.6%) | +0.1% (-1.8% to +1.9%) |
| Linux: Warm start | 636ms | 353ms | 353ms | -44.5% (-45.3% to -44.1%) | -0.1% (-1.8% to +1.1%) |
| Windows: Warm start | 355ms | 201ms | 201ms | -43.4% (-43.7% to -42.7%) | +0.0% (-0.9% to +0.7%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start watching while the cache is read
```

## Test plan

New unit tests. Benchmark as in #2104, with 22 rounds on macOS.

@huntie huntie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test plan machine orchestration 😄

@robhogan
robhogan removed this pull request from stack #2109 November 16, 2026 22:09
@robhogan
robhogan added this pull request to stack #2241 November 16, 2026 22:10
@robhogan
robhogan requested review from huntie and removed request for huntie November 17, 2026 18:40
@robhogan
robhogan merged commit c620f7e into main Nov 17, 2026
16 checks passed
robhogan added a commit that referenced this pull request Nov 17, 2026
)

Without Watchman, Metro watches with `FallbackWatcher` on Linux and Windows. Its startup walk `lstat`s every entry, files included, just to learn its type:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L505-L515

This walks by the types `readdir` reports instead. A directory that appears while watching still needs stats for its events, so that walk now stats only the entries it reports. The walk also now descends into a watch root that's a symlink, as the crawler does.

## Benchmark

This takes **40% off watch-mode startup on Linux (1,614ms -> 975ms) and 20% off on Windows**. macOS uses `NativeWatcher`, so it's unaffected.

|  | `main` | #2104 | This PR | vs `main` | vs #2104 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 1,614ms | 975ms | -48.3% (-49.0% to -47.9%) | -39.6% (-40.3% to -39.1%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 2,417ms | 1,941ms | -24.7% (-25.1% to -24.3%) | -19.7% (-20.0% to -19.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 1,579ms | 921ms | -51.1% (-51.6% to -50.5%) | -41.7% (-42.3% to -41.0%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 2,386ms | 1,918ms | -25.2% (-25.3% to -24.7%) | -19.6% (-19.8% to -19.3%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start the file watcher without stat-ing every file (Linux and Windows, without Watchman)
```

## Test plan

New unit test that startup doesn't `lstat` anything it finds. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 17, 2026
…2106)

On Windows, `FallbackWatcher` waits a second after setting up its watches:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L77-L82

That's a holdover from `sane`. libuv issues `ReadDirectoryChangesW` before `uv_fs_event_start` returns, so a watch is live as soon as `fs.watch` returns (here in libuv 1.49.2, which Node 22.13 ships):

https://github.com/libuv/libuv/blob/e1095c7a4373ce00cd8874d8e820de5afb25776e/src/win/fs-event.c#L298-L320

## Benchmark

Unsurprisingly, this takes ~exactly one second off watch-mode startup on Windows (1,941ms -> 935ms), and is a no-op elsewhere.

|  | `main` | #2105 | This PR | vs `main` | vs #2105 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 975ms | 969ms | -48.7% (-49.2% to -47.9%) | -0.7% (-1.4% to +1.2%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 1,941ms | 935ms | -63.7% (-63.9% to -63.6%) | -51.8% (-52.0% to -51.7%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 921ms | 931ms | -50.6% (-51.1% to -49.8%) | +1.1% (-0.2% to +2.6%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 1,918ms | 917ms | -64.2% (-64.4% to -64.0%) | -52.2% (-52.5% to -52.1%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Remove a 1s delay from file watcher startup on Windows without Watchman
```

## Test plan

New unit test that changes made as soon as `startWatching` resolves are reported. It runs on Linux and Windows, and is skipped on macOS, where `fs.watch` starts FSEvents asynchronously (but `FallbackWatcher` isn't used). Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 17, 2026
#2107)

`FallbackWatcher` keeps an entry for every file it has seen:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L53-L56

It only reads them when something is deleted. Deleting `src/a.js` is reported because there's an entry for it, and deleting `src` reports `src/a.js`, `src/b.js` and so on from the entries, then `src` itself.

Neither needs an entry per file:

 - A file's name says whether it was reported: `src/a.js` passes the include checks, `src/README.md` doesn't.
 - Told that `src` is deleted, the file map removes everything under it (that's how `NativeWatcher` reports it), so one `delete` is enough.

So this keeps entries only for directories (including ones it couldn't watch) and symlinks. Those are reported whatever their name, so once `src/components` is gone, only an entry says whether it was a directory or a file the include checks left out.

## Benchmark

This takes another 22-30% off watch-mode startup on Linux and Windows (Linux: 969ms -> 695ms), and 3MB (10%) off the heap after startup on this app.

|  | `main` | #2106 | This PR | vs `main` | vs #2106 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 969ms | 695ms | -63.2% (-63.6% to -62.8%) | -28.2% (-29.2% to -27.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 935ms | 727ms | -71.8% (-71.9% to -71.7%) | -22.2% (-22.4% to -21.9%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 931ms | 652ms | -65.4% (-65.6% to -65.1%) | -30.0% (-30.9% to -29.2%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 917ms | 701ms | -72.7% (-72.8% to -72.5%) | -23.6% (-23.8% to -23.1%) |
| Linux: Heap after a watch-mode start | 31.7MB | 31.2MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -9.9% (-9.9% to -9.9%) |
| Windows: Heap after a watch-mode start | 31.7MB | 31.3MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -9.9% (-9.9% to -9.9%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start the file watcher faster, and with less memory, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` that drives FileMap in watch mode over a real directory, deleting directories and symlinks and moving directories out (with `FallbackWatcher`, or `NativeWatcher` on macOS). Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 17, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,887ms to 492ms (-74%) on Linux, and from 2,577ms to 525ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 5% of #2104, which is noise: #2106 changes nothing there and measures +4.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 695ms | 492ms | -73.9% (-74.2% to -73.7%) | -29.2% (-29.9% to -28.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 727ms | 525ms | -79.6% (-79.7% to -79.5%) | -27.7% (-28.1% to -27.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 652ms | 462ms | -75.5% (-75.8% to -75.0%) | -29.2% (-30.0% to -27.7%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 701ms | 493ms | -80.8% (-80.8% to -80.6%) | -29.6% (-29.9% to -29.3%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.3% (-11.3% to -11.3%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -0.0% (-0.0% to -0.0%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 17, 2026
…2219)

`TreeFS.getSerializableSnapshot()` deep-copies the whole tree - a `Map` per directory and an array per file - before every cache write:

https://github.com/react/metro/blob/1461d81e97fa8fb83a1017925c2870e260d833cc/packages/metro-file-map/src/lib/TreeFS.js#L1476-L1486

`DiskCacheManager` serialises the snapshot as soon as it asks for it, so nothing can change it in between and the copy is thrown away:

https://github.com/react/metro/blob/1461d81e97fa8fb83a1017925c2870e260d833cc/packages/metro-file-map/src/cache/DiskCacheManager.js#L119-L121

This adds `getSerializableState()`, which returns the tree itself, and writes the cache from that, saving a copy of the file map on every cold start and on every save of the cache while watching. `getSerializableSnapshot()` still returns a copy for now, and a follow-up removes it.

A custom cache manager (`unstable_fileMapCacheManagerFactory`) that holds on to the snapshot to serialise later would now see later changes, so `CacheManager.write`'s docs now say to serialise what `getSnapshot` returns before any `await`, or copy it. Expo's fork (`@expo/metro-file-map`) already skips the deep copy, for the same reason.

## Benchmark

This takes 28-51% off writing the cache (Windows: 19.4ms -> 9.5ms), or 2-5% off a cold start on Linux and Windows. macOS's cold starts are too noisy to show it. The same copy was made on every save of the cache while watching, which this benchmark doesn't exercise.

|  | `main` | #2108 | This PR | vs `main` | vs #2108 |
|---|---|---|---|---|---|
| macOS: Cache write in a cold start | 7.8ms | 6.6ms | 4.3ms | -44.4% (-45.1% to -43.6%) | -34.2% (-35.4% to -33.7%) |
| Linux: Cache write in a cold start | 30ms | 28ms | 20ms | -31.9% (-39.7% to -22.0%) | -27.9% (-32.0% to -25.1%) |
| Windows: Cache write in a cold start | 20ms | 19.4ms | 9.5ms | -53.5% (-54.9% to -36.4%) | -51.0% (-53.2% to -32.1%) |
| macOS: Cold start | 129ms | 96ms | 90ms | -30.2% (-36.0% to -22.6%) | -5.7% (-14.6% to +8.9%) |
| Linux: Cold start | 648ms | 324ms | 319ms | -50.8% (-51.7% to -50.1%) | -1.8% (-5.0% to -0.5%) |
| Windows: Cold start | 363ms | 179ms | 170ms | -53.2% (-53.8% to -52.5%) | -5.1% (-6.8% to -2.6%) |
| macOS: Watch-mode start, cold cache | 134ms | 98ms | 96ms | -28.6% (-29.5% to -27.0%) | -2.7% (-4.5% to +0.2%) |
| Linux: Watch-mode start, cold cache | 1,892ms | 470ms | 451ms | -76.1% (-76.3% to -75.8%) | -3.9% (-4.9% to -0.9%) |
| Windows: Watch-mode start, cold cache | 2,556ms | 496ms | 490ms | -80.8% (-80.9% to -80.8%) | -1.1% (-1.6% to -0.7%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Write the cache without copying the file map first
```

## Test plan

New unit tests that `DiskCacheManager` serialises the snapshot before anything else can run, and that only `getSerializableSnapshot()` copies. Benchmark as in #2104, with 24 rounds on macOS.
robhogan added a commit that referenced this pull request Nov 19, 2026
…2220)

* Only add the Haste plugin when Haste is configured

With Metro's defaults - no `hasteImplModulePath` and `enableGlobalPackages: false` - no file can have a Haste name, but `createFileMap` adds the Haste plugin anyway. It never finds anything, but still costs something for every file: each crawled file goes through `FileProcessor` to ask the plugin's filter whether it needs a worker, and the plugin then iterates every added file on a cold start, and every file outside `node_modules` on a warm start.

This only adds the Haste plugin when there's a `hasteImplModulePath` or Haste packages are enabled. Otherwise `createFileMap` returns `null` as its `hasteMap`, and `DependencyGraph` resolves no Haste names. Since `hasteMap` is now typed `?HasteMap`, any other caller of `createFileMap` that reads it needs a null check, eg `hasteMap?.getModule(...)`.

A config that sets either option is unchanged: `createFileMap` returns the same `HastePlugin` as its `hasteMap`, with the same cache. Without them, leaving the plugin out changes the cache key, so `CACHE_BREAKER` doesn't need a bump.

## Benchmark

This takes 14-19% off applying the crawl on a cold start (Linux: 84ms -> 68ms), and 5-6% off a cold start on Linux and Windows (Linux: 314ms -> 297ms). macOS's cold starts are too noisy to show it.

On Linux and Windows, a cache write takes one of two times (on Linux, about 14ms or 20ms), and this PR's writes take the shorter one more often (Linux: 31 of 40 runs against 10, Windows: 23 against 15). That's presumably because there's less garbage to collect.

|  | `main` | #2219 | This PR | vs `main` | vs #2219 |
|---|---|---|---|---|---|
| macOS: Applying the crawl in a cold start | 19.2ms | 17.9ms | 15.4ms | -19.7% (-21.9% to -17.1%) | -13.7% (-16.8% to -10.5%) |
| Linux: Applying the crawl in a cold start | 83ms | 84ms | 68ms | -18.1% (-20.3% to -15.3%) | -19.2% (-21.1% to -16.3%) |
| Windows: Applying the crawl in a cold start | 47ms | 41ms | 34ms | -28.6% (-30.8% to -26.9%) | -18.3% (-23.3% to -15.3%) |
| macOS: Cache write in a cold start | 7.6ms | 4.4ms | 4.4ms | -42.4% (-44.7% to -39.8%) | -1.1% (-3.5% to +1.1%) |
| Linux: Cache write in a cold start | 32ms | 20ms | 15.0ms | -53.2% (-55.5% to -47.9%) | -26.0% (-28.5% to -18.0%) |
| Windows: Cache write in a cold start | 20ms | 13.6ms | 10.6ms | -47.7% (-50.5% to -28.3%) | -21.8% (-26.3% to +13.4%) |
| macOS: Cold start | 146ms | 94ms | 90ms | -38.1% (-44.6% to -33.5%) | -4.2% (-16.6% to +7.8%) |
| Linux: Cold start | 647ms | 314ms | 297ms | -54.1% (-54.7% to -53.4%) | -5.7% (-6.7% to -4.1%) |
| Windows: Cold start | 358ms | 170ms | 162ms | -54.7% (-55.1% to -54.2%) | -4.9% (-5.6% to -3.5%) |

## Changelog

```
 - **[Performance]**: Skip Haste's work at startup when Haste isn't configured
```

## Test plan

New unit tests. Benchmark as in #2104 (cold starts only), with 12 rounds on macOS.

* Assign hasteMap directly and build the test config with mergeConfig
robhogan added a commit to robhogan/expo that referenced this pull request Nov 19, 2026
# Why

Expo's `FallbackWatcher`, which Metro uses on Linux and Windows while we default Watchman off, is upstream's from March, plus twins of two later upstream PRs. Upstream has since fixed several bugs in it, and made it start faster with less memory:

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before listing it | expo#49363 |
| react/metro#1967 | Recrawl a directory when Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` | expo#51093 |
| react/metro#2004 | Close every watch under a deleted directory | - |
| react/metro#2105 | Start without a stat per file | - |
| react/metro#2106 | Remove the 1s startup delay on Windows | - |
| react/metro#2107 | Track only directories and symlinks | - |

The fixes matter most on Windows. A deleted watched directory reports its own deletion to its handle again and again until the handle is closed (before libuv 1.53), and the open handle stops the directory being watched again if it's recreated. When changes overflow a directory's buffer, Windows doesn't name what changed, and Expo's watcher reported one file it guessed had changed and missed the rest.

Elsewhere, a handle on Linux follows a directory that's moved aside (as npm does to update a package), so it went on watching the old copy. And `walker` doesn't descend into a watch root that's a symlink, so nothing below one was watched.

The faster startup also offsets the cost of reading symlinks at crawl since expo#53732, on Linux and Windows.

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's `FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It leaves out `isLiveOnStart()` from react/metro#2003, which only upstream's `Watcher` calls, and react/metro#2108, which needs `Watcher` to start its backends before the crawl (react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher that errors does nothing, since Node closes the handle itself before emitting `error`.

A directory replaced at the same path, a symlink replaced by a directory and a change Windows doesn't name now have the file map recrawl the path, which `FileMap` already handles. For the last, a gap remains until we port react/metro#2104: a recrawl doesn't stat a file the file map never has, so a change to one is still missed, as all but one were before.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two tests for expo#48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms -> 338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node 22.13.0 and 24, and on Windows 11 with Node 24, where it's the default watcher.
- Checked that a watch root that's a symlink is now watched below the root.

Benchmark (AI-driven), against `main`: starting the watcher on a default Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default source extensions, 16 interleaved rounds per platform, each run in a fresh process, with a byte-identical control whose medians are all within 0.5% of `main`. Heap and RSS are the growth while watching, after a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |
robhogan added a commit to expo/expo that referenced this pull request Nov 21, 2026
# Why

The node crawler lists a regular file without a stat when it has no mtime in the previous file map, and only `getOrComputeSha1` stats it later. Eager SHA-1s (`watcher.unstable_lazySha1: false`) and plugin data such as dependencies are computed at startup without a stat, so those files keep a null mtime forever. Edit one while Metro is stopped and the next start sees null on both sides, calls it unchanged, and keeps the stale SHA-1 and plugin data.

Recrawls have the same gap. A recrawl while watching diffs against the live tree, so a file that was never stat'd and changed in the recrawled directory looks unchanged, even though consumers like the package cache may hold its old contents.

On a warm start, the crawler also looks up every file's previous mtime in `TreeFS`, one lookup per file.

# How

This ports react/metro#2104. Its invariant is that a file without an mtime has nothing that could be stale: the worker stats a file before reading it whenever a SHA-1 or plugin data is first derived from it, and records the mtime and size. The crawler lists regular files without a stat, `getDifference` returns the files that had an mtime before (`filesToStat`), and the crawler stats only those once the difference is known. A recrawl (`subpath` set) stats every regular file, as upstream does. Symlinks are always stat'd, as they already were.

This removes the per-file lookup and `getMtimeByNormalPath`, and `TreeFS` no longer touches the file system. The crawler's `find` moves to upstream's promise-based `find(options)` shape, keeping the fork's VCS and `.cxx` exclusions and directory ignore patterns. `CACHE_BREAKER` is bumped, so an existing cache doesn't keep files that were never stat'd.

Upstream's hunk for lazy plugin workers (react/metro#1950) has no counterpart here and isn't ported.

# Test Plan

Ported upstream's new TreeFS, node crawler, FileProcessor and worker tests, and added a worker suite. Upstream's `stats-between-starts-test` runs `FileMap` over a real directory, so this adds a memfs version that edits files between two `FileMap` sessions sharing a disk cache. The eager SHA-1, plugin data and "stats only files with derived data" cases fail on `main` and pass here. The new recrawl test fails without the recrawl stat.
robhogan added a commit to expo/expo that referenced this pull request Nov 23, 2026
# Why

Without Watchman, `@expo/metro-file-map` crawls regular files without a stat when the previous file map has no mtime for them, as upstream Metro now does too. The two got there differently, and the fork's version leaves data stale:

- **Edits while Metro is stopped:** Plugin data, such as Haste names and dependencies, is derived at startup without a stat, so those files keep a null mtime. Edit one while Metro is stopped, and the next start sees null on both sides, calls it unchanged, and keeps the stale plugin data. The same goes for eager SHA-1s (`watcher.unstable_lazySha1: false`) read through `getSha1()`, although `getOrComputeSha1()`, which Metro calls, stats the file and recovers.
- **Recrawls:** A recrawl while watching diffs against the live tree, so a file that was never stat'd and changed in the recrawled directory looks unchanged. Since #53805, that's every change Windows doesn't name, as well as directories moved on macOS.

The fork also looks up every crawled file's previous mtime in `TreeFS` on a warm start, one lookup per file.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2104 | Lazily stat files in the node crawler | #45378 (`getMtimeByNormalPath` in the crawler, lazy `lstat` in `getOrComputeSha1`), #45391 (`skipFallback`) |

# How

This ports react/metro#2104. Its invariant is that a file without an mtime has nothing that could be stale: the worker stats a file before reading it whenever a SHA-1 or plugin data is first derived from it, and records the mtime and size. The crawler lists regular files without a stat, `getDifference` returns the files that had an mtime before (`filesToStat`), and the crawler stats only those once the difference is known. A recrawl (`subpath` set) stats every regular file. Symlinks are always stat'd, as they already were since #53732.

It replaces the fork's own lazy stat:

- The crawler's per-file `getMtimeByNormalPath` lookup, and with it `skipFallback`, which only that lookup used.
- The `lstat` in `getOrComputeSha1`, which the worker's stat replaces.
- `getDifference`'s "null or 0 on both sides" branch, which becomes upstream's two regular-file branches. `size` is null for a file that hasn't been stat'd, where it was 0, including in the on-demand filesystem's entries.
- The callback-based `find(...)`, which becomes upstream's `find(options)` promise, keeping the fork's VCS and `.cxx` exclusions and directory ignore patterns.

`CACHE_BREAKER` is bumped, so an existing cache doesn't keep files whose data was derived without a stat. Upstream's hunk for lazy plugin workers (react/metro#1950) has no counterpart here and isn't ported.

# Test Plan

- Unit tests (770 passed, 1 skipped).
- Ported upstream's new `TreeFS`, node crawler, `FileProcessor` and worker tests, the last as a new suite, and dropped the fork's `getDifference` tests that upstream's now cover.
- Upstream's `stats-between-starts-test` runs `FileMap` over a real directory, so this adds a memfs version that edits files between two `FileMap` sessions sharing a disk cache. The eager SHA-1 and plugin data cases fail on `main` and pass here, and the recrawl case fails without the recrawl stat.
- On macOS, real `FileMap` sessions over a temporary directory, editing files between sessions: an eager and a lazy SHA-1, dependency data across four starts, a stat'd file deleted or replaced by a symlink, and a file read through the on-demand filesystem.
robhogan added a commit that referenced this pull request Nov 23, 2026
…#2240)

* metro-file-map: Skip the per-file worker pass when no plugin needs it

With lazy SHA-1s (Metro's default), a file only needs a worker at startup if a plugin computes data for it as it's crawled. With Metro's defaults none does, since #2220 leaves out the Haste plugin without Haste config. Even so, `processBatch` goes through every crawled file to find that out, checking its path for `node_modules` and building an array of the plugins to run on it.

This returns straight away when there's no SHA-1 to compute and every plugin's worker is lazy (`lazy: true`, ie it computes data on demand). It doesn't change the cache or any API.

## Benchmark

This takes 5-16% off applying the crawl on a cold start (macOS: 15.4ms -> 13.0ms, Linux: 68ms -> 64ms), and 2-3% off a cold start on Linux and Windows (Linux: 297ms -> 289ms). On macOS, cache writes are 0.4ms slower. The serialise itself takes the same time, so the difference is in the rest of the write.

|  | `main` | #2220 | This PR | vs `main` | vs #2220 |
|---|---|---|---|---|---|
| macOS: Applying the crawl in a cold start | 19.2ms | 15.4ms | 13.0ms | -32.5% (-34.5% to -30.3%) | -15.9% (-19.0% to -12.8%) |
| Linux: Applying the crawl in a cold start | 83ms | 68ms | 64ms | -23.5% (-25.3% to -20.4%) | -6.5% (-8.3% to -4.1%) |
| Windows: Applying the crawl in a cold start | 47ms | 34ms | 32ms | -32.4% (-33.4% to -31.5%) | -5.3% (-7.7% to -2.4%) |
| macOS: Cache write in a cold start | 7.6ms | 4.4ms | 4.8ms | -36.4% (-38.9% to -33.8%) | +10.3% (+7.5% to +12.9%) |
| Linux: Cache write in a cold start | 32ms | 15.0ms | 14.4ms | -55.0% (-56.4% to -52.2%) | -3.7% (-12.9% to +2.5%) |
| Windows: Cache write in a cold start | 20ms | 10.6ms | 9.4ms | -53.7% (-54.6% to -52.7%) | -11.4% (-35.3% to -6.2%) |
| macOS: Cold start | 146ms | 90ms | 92ms | -37.1% (-43.4% to -32.7%) | +1.7% (-9.6% to +13.9%) |
| Linux: Cold start | 647ms | 297ms | 289ms | -55.3% (-55.7% to -54.9%) | -2.6% (-4.2% to -1.3%) |
| Windows: Cold start | 358ms | 162ms | 158ms | -55.9% (-56.5% to -55.6%) | -2.6% (-4.4% to -1.7%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Skip the per-file worker pass when no plugin needs it
```

## Test plan

New unit tests. Benchmark as in #2104 (cold starts only), with 12 rounds on macOS.

* Compute the eager plugin indices once in FileProcessor
robhogan added a commit that referenced this pull request Nov 23, 2026
* metro-file-map: Start watching while the cache is read

Without Watchman, a watch-mode start has to be watching before it crawls, so that a change made during the crawl isn't missed. `NativeWatcher` (macOS) isn't live as soon as it starts, so `Watcher` also confirms each root's watch with a health check before the crawl begins:

https://github.com/react/metro/blob/ef50b94fba3bac242d06b20f03c7149987b3c2d6/packages/metro-file-map/src/Watcher.js#L375-L382

That takes about 13ms on macOS, and `build()` only starts it once the cache has been read, although the watch doesn't need anything from the cache.

This starts the watch, and the Watchman capability check (which spawns `watchman`), while the cache is read. The crawl still waits for the confirmed watch, so nothing can be missed that wasn't before. To allow this, `Watcher` takes the cached state in `crawl()` rather than at construction.

## Benchmark

This takes 13% off a watch-mode start on macOS (104ms -> 90ms). Linux and Windows watch with `FallbackWatcher`, which is live as soon as it starts, so they're unchanged, as are starts outside watch mode. With a cold cache there's no read to overlap with.

|  | `main` | #2240 | This PR | vs `main` | vs #2240 |
|---|---|---|---|---|---|
| macOS: Watch-mode start | 145ms | 104ms | 90ms | -37.7% (-39.9% to -35.9%) | -13.1% (-16.2% to -9.9%) |
| Linux: Watch-mode start | 1,882ms | 489ms | 492ms | -73.9% (-74.2% to -73.5%) | +0.6% (-0.7% to +1.8%) |
| Windows: Watch-mode start | 2,577ms | 523ms | 522ms | -79.7% (-79.8% to -79.6%) | -0.1% (-0.7% to +0.3%) |
| macOS: Warm start | 131ms | 88ms | 88ms | -32.6% (-35.0% to -30.8%) | -0.2% (-4.0% to +2.9%) |
| Linux: Warm start | 641ms | 353ms | 354ms | -44.9% (-45.7% to -44.3%) | +0.0% (-1.8% to +1.3%) |
| Windows: Warm start | 352ms | 200ms | 201ms | -43.0% (-43.3% to -42.8%) | +0.5% (+0.0% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start watching while the cache is read
```

## Test plan

New unit tests. Benchmark as in #2104, with 20 rounds on macOS.

* Shorten the build() comment on preparing the watcher
robhogan added a commit to expo/expo that referenced this pull request Nov 25, 2026
# Why

This ports upstream's lazy stat, which is required to port upstream's
dynamic roots, which is required to unblock updating Metro.

Expo already had lazy statting, which upstream's was inspired by but the
implementation ended up quite different. This fixes a couple of minor
bugs:

- **Recrawls:** A recrawl while watching diffs against the live tree, so
a file that was never stat'd and changed in the recrawled directory
looks unchanged. That would've affected moved directories on macOS and
Windows buffer overruns.
- **Plugin data** (not currently used in practice in an Expo context,
but coming) could go stale if changed while Metro was stopped, because
data was stored against a `null` mtime and not updated on a recrawl.

The fork also looks up every crawled file's previous mtime in `TreeFS`
on a warm start, one lookup per file.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2104 | Lazily stat files in the
node crawler | #45378 (`getMtimeByNormalPath` in the crawler, lazy
`lstat` in `getOrComputeSha1`), #45391 (`skipFallback`) |

# How

This ports react/metro#2104. Its guarantee is
that **a file without an mtime has nothing that could be stale**: the
worker stats a file before reading it whenever a SHA-1 or plugin data is
first derived from it, and records the mtime and size.

The crawler lists regular files without a stat, `getDifference` returns
the files that had an mtime before (`filesToStat`), and the crawler
stats only those once the difference is known. A recrawl (`subpath` set)
stats every regular file. Symlinks are always stat'd, as they already
were since #53732.

It replaces the fork's own lazy stat:

- The crawler's per-file `getMtimeByNormalPath` lookup, and with it
`skipFallback`, which only that lookup used.
- The `lstat` in `getOrComputeSha1`, which the worker's stat replaces.
- `getDifference`'s "null or 0 on both sides" branch, which becomes
upstream's two regular-file branches. `size` is null for a file that
hasn't been stat'd, where it was 0, including in the on-demand
filesystem's entries.
- The callback-based `find(...)`, which becomes upstream's
`find(options)` promise, keeping the fork's VCS and `.cxx` exclusions
and directory ignore patterns.

`CACHE_BREAKER` is bumped, so an existing cache doesn't keep files whose
data was derived without a stat. Upstream's hunk for lazy plugin workers
(react/metro#1950) has no counterpart here and
isn't ported.

## Performance

On Linux and Windows, every start is faster (warm 367ms -> 339ms on
Linux, 241ms -> 221ms on Windows), and the first SHA-1 of a file is
29-49% faster, since the worker stats it as it reads it rather than
`TreeFS` stat-ing it first.

On macOS, warm and watch-mode starts with a cache are 9-12% slower (warm
88ms -> 97ms), all of it in the crawl. The fork's stat-ing during the
walk was the cheaper placement on macOS, where statting itself is
faster, and is the dearer one on Linux and Windows. I'm looking into
optimising that upstream but it may not be worth the complexity. Cold
starts are unaffected.

# Test Plan

- Unit tests (770 passed, 1 skipped).
- Ported upstream's new `TreeFS`, node crawler, `FileProcessor` and
worker tests, the last as a new suite, and dropped the fork's
`getDifference` tests that upstream's now cover.
- Upstream's `stats-between-starts-test` runs `FileMap` over a real
directory, so this adds a memfs version that edits files between two
`FileMap` sessions sharing a disk cache. The eager SHA-1 and plugin data
cases fail on `main` and pass here, and the recrawl case fails without
the recrawl stat.
- On macOS, real `FileMap` sessions over a temporary directory, editing
files between sessions: an eager and a lazy SHA-1, dependency data
across four starts, a stat'd file deleted or replaced by a symlink, and
a file read through the on-demand filesystem.

Benchmark (AI-driven), against `main`: Metro's `DependencyGraph` with
`@expo/metro-file-map` swapped in as Expo CLI does (on-demand filesystem
off, no Watchman), on a React Native 0.87.1 app with 34,488 files in the
file map. Each round runs, per variant: 2 cold starts, a watch-mode
session that computes the SHA-1s of the bundle's 3,125 files and saves
them to the cache, 3 warm starts, 2 watch-mode starts and a watch-mode
cold start, each in a fresh process. 15 interleaved rounds on macOS and
12 on Linux and Windows, with a byte-identical control whose medians are
within 1% of `main` except one RSS cell. Mac mini M4 Pro (macOS, APFS),
Mac mini i5-4278U (Linux Mint 22.3, ext4) and a Ryzen 7 7730U laptop
(Windows 11, NTFS, Defender on), all Node 24.21.0. Heap is unchanged,
and RSS is within noise apart from +4% on macOS.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| macOS | Cold start | 76ms | +6.7% [-4.7, +9.6] |
| macOS | Warm start | 88ms | +11.6% [+10.1, +13.0] |
| macOS | Watch-mode start, warm cache | 88ms | +8.9% [+6.2, +10.2] |
| macOS | Watch-mode start, cold cache | 75ms | +12.5% [+7.1, +14.6] |
| macOS | First SHA-1 of the bundle's files | 97ms | -37.6% [-40.0,
-36.5] |
| Linux | Cold start | 311ms | -3.3% [-4.6, -2.0] |
| Linux | Warm start | 367ms | -7.4% [-8.7, -5.8] |
| Linux | Watch-mode start, warm cache | 703ms | -4.1% [-4.6, -2.7] |
| Linux | Watch-mode start, cold cache | 647ms | -1.8% [-3.4, -1.0] |
| Linux | First SHA-1 of the bundle's files | 262ms | -48.5% [-50.5,
-46.3] |
| Windows | Cold start | 196ms | -6.3% [-8.9, -3.8] |
| Windows | Warm start | 241ms | -8.7% [-9.4, -8.0] |
| Windows | Watch-mode start, warm cache | 816ms | -2.1% [-2.7, -1.4] |
| Windows | Watch-mode start, cold cache | 774ms | -2.1% [-4.2, -1.5] |
| Windows | First SHA-1 of the bundle's files | 469ms | -29.1% [-30.7,
-27.7] |
robhogan added a commit to expo/expo that referenced this pull request Nov 25, 2026
# Why

This ports upstream's lazy stat, which is required to port upstream's
dynamic roots, which is required to unblock updating Metro.

Expo already had lazy statting, which upstream's was inspired by but the
implementation ended up quite different. This fixes a couple of minor
bugs:

- **Recrawls:** A recrawl while watching diffs against the live tree, so
a file that was never stat'd and changed in the recrawled directory
looks unchanged. That would've affected moved directories on macOS and
Windows buffer overruns.
- **Plugin data** (not currently used in practice in an Expo context,
but coming) could go stale if changed while Metro was stopped, because
data was stored against a `null` mtime and not updated on a recrawl.

The fork also looks up every crawled file's previous mtime in `TreeFS`
on a warm start, one lookup per file.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2104 | Lazily stat files in the
node crawler | #45378 (`getMtimeByNormalPath` in the crawler, lazy
`lstat` in `getOrComputeSha1`), #45391 (`skipFallback`) |

# How

This ports react/metro#2104. Its guarantee is
that **a file without an mtime has nothing that could be stale**: the
worker stats a file before reading it whenever a SHA-1 or plugin data is
first derived from it, and records the mtime and size.

The crawler lists regular files without a stat, `getDifference` returns
the files that had an mtime before (`filesToStat`), and the crawler
stats only those once the difference is known. A recrawl (`subpath` set)
stats every regular file. Symlinks are always stat'd, as they already
were since #53732.

It replaces the fork's own lazy stat:

- The crawler's per-file `getMtimeByNormalPath` lookup, and with it
`skipFallback`, which only that lookup used.
- The `lstat` in `getOrComputeSha1`, which the worker's stat replaces.
- `getDifference`'s "null or 0 on both sides" branch, which becomes
upstream's two regular-file branches. `size` is null for a file that
hasn't been stat'd, where it was 0, including in the on-demand
filesystem's entries.
- The callback-based `find(...)`, which becomes upstream's
`find(options)` promise, keeping the fork's VCS and `.cxx` exclusions
and directory ignore patterns.

`CACHE_BREAKER` is bumped, so an existing cache doesn't keep files whose
data was derived without a stat. Upstream's hunk for lazy plugin workers
(react/metro#1950) has no counterpart here and
isn't ported.

## Performance

On Linux and Windows, every start is faster (warm 367ms -> 339ms on
Linux, 241ms -> 221ms on Windows), and the first SHA-1 of a file is
29-49% faster, since the worker stats it as it reads it rather than
`TreeFS` stat-ing it first.

On macOS, warm and watch-mode starts with a cache are 9-12% slower (warm
88ms -> 97ms), all of it in the crawl. The fork's stat-ing during the
walk was the cheaper placement on macOS, where statting itself is
faster, and is the dearer one on Linux and Windows. I'm looking into
optimising that upstream but it may not be worth the complexity. Cold
starts are unaffected.

# Test Plan

- Unit tests (770 passed, 1 skipped).
- Ported upstream's new `TreeFS`, node crawler, `FileProcessor` and
worker tests, the last as a new suite, and dropped the fork's
`getDifference` tests that upstream's now cover.
- Upstream's `stats-between-starts-test` runs `FileMap` over a real
directory, so this adds a memfs version that edits files between two
`FileMap` sessions sharing a disk cache. The eager SHA-1 and plugin data
cases fail on `main` and pass here, and the recrawl case fails without
the recrawl stat.
- On macOS, real `FileMap` sessions over a temporary directory, editing
files between sessions: an eager and a lazy SHA-1, dependency data
across four starts, a stat'd file deleted or replaced by a symlink, and
a file read through the on-demand filesystem.

Benchmark (AI-driven), against `main`: Metro's `DependencyGraph` with
`@expo/metro-file-map` swapped in as Expo CLI does (on-demand filesystem
off, no Watchman), on a React Native 0.87.1 app with 34,488 files in the
file map. Each round runs, per variant: 2 cold starts, a watch-mode
session that computes the SHA-1s of the bundle's 3,125 files and saves
them to the cache, 3 warm starts, 2 watch-mode starts and a watch-mode
cold start, each in a fresh process. 15 interleaved rounds on macOS and
12 on Linux and Windows, with a byte-identical control whose medians are
within 1% of `main` except one RSS cell. Mac mini M4 Pro (macOS, APFS),
Mac mini i5-4278U (Linux Mint 22.3, ext4) and a Ryzen 7 7730U laptop
(Windows 11, NTFS, Defender on), all Node 24.21.0. Heap is unchanged,
and RSS is within noise apart from +4% on macOS.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| macOS | Cold start | 76ms | +6.7% [-4.7, +9.6] |
| macOS | Warm start | 88ms | +11.6% [+10.1, +13.0] |
| macOS | Watch-mode start, warm cache | 88ms | +8.9% [+6.2, +10.2] |
| macOS | Watch-mode start, cold cache | 75ms | +12.5% [+7.1, +14.6] |
| macOS | First SHA-1 of the bundle's files | 97ms | -37.6% [-40.0,
-36.5] |
| Linux | Cold start | 311ms | -3.3% [-4.6, -2.0] |
| Linux | Warm start | 367ms | -7.4% [-8.7, -5.8] |
| Linux | Watch-mode start, warm cache | 703ms | -4.1% [-4.6, -2.7] |
| Linux | Watch-mode start, cold cache | 647ms | -1.8% [-3.4, -1.0] |
| Linux | First SHA-1 of the bundle's files | 262ms | -48.5% [-50.5,
-46.3] |
| Windows | Cold start | 196ms | -6.3% [-8.9, -3.8] |
| Windows | Warm start | 241ms | -8.7% [-9.4, -8.0] |
| Windows | Watch-mode start, warm cache | 816ms | -2.1% [-2.7, -1.4] |
| Windows | Watch-mode start, cold cache | 774ms | -2.1% [-4.2, -1.5] |
| Windows | First SHA-1 of the bundle's files | 469ms | -29.1% [-30.7,
-27.7] |

(cherry picked from commit ebb0f7f)
@robhogan robhogan mentioned this pull request Nov 26, 2026
robhogan added a commit that referenced this pull request Nov 26, 2026
Publish Metro 0.88.1 from `main`. Nothing since 0.88.0 is breaking, so this is a patch release, but one new default is worth reading about before upgrading: transform results are now shared between graphs (see below).

Changelog: Internal

## Draft release notes

```
 - **[Feature]**: metro-cache: `HttpStore` accepts `maxSockets`, to change its connection limit, and `getParams`, to add query parameters (such as a short-lived credential) to every request (#2246 by @GijsWeterings)
 - **[Feature]**: `Transforming file` end log entries report the worker's CPU time for the transform as `cpu_time_us` (#2282 by @vzaidman)
 - **[Fix]**: Keep function bindings that are reassigned when folding constants in production builds (#2197 by @Mwrnk)
 - **[Fix]**: Keep side effects such as `i++` in the leading operands of a sequence when folding constants in production builds (#2278 by @burrows99)
 - **[Fix]**: Keep a `var` declared in a branch that's removed when folding constants in production builds, if code outside the branch uses it, instead of throwing a `ReferenceError` (#2279 by @burrows99)
 - **[Fix]**: Don't replace `void 0` with `undefined` when folding constants in production builds, where `undefined` is shadowed (#2215 by @ZainnQureshii)
 - **[Fix]**: Memoized inline requires: A module first referenced in a skipped optional chain, logical assignment or default value is no longer `undefined` at later references (#2280 by @burrows99)
 - **[Fix]**: `experimentalImportSupport`: `export * from` a module with an enumerable `__esModule` no longer throws in strict mode (#2281 by @burrows99)
 - **[Fix]**: Define a `__proto__` key in JSON modules as an own property, matching `JSON.parse` (#2199 by @Mwrnk)
 - **[Fix]**: `FileStore` writes cache entries atomically, so concurrent reads no longer miss valid entries or see partial writes (#2040 by @frankleng)
 - **[Fix]**: Respect `excludeSource` in inline source maps, and pass it to `serializer.customSerializer` (#2256 by @robhogan)
 - **[Fix]**: Stop leaking a graph's `DeltaCalculator` and file change listener when its first build fails (#2283 by @sensei-woo)
 - **[Fix]**: Resolve a package subpath ending in `/.`, such as `pkg/.`, instead of throwing `Unexpectedly escaped traversal` (#2213 by @kwy404)
 - **[Fix]**: Fix `resolver.blockList: []` hiding every file (8f49d3681cfd)
 - **[Fix]**: Fix an out-of-memory crash on a symlink into itself (#2204 by @robhogan)
 - **[Fix]**: Fix the path of a file added through a symlink to the project root (#2212 by @robhogan)
 - **[Fix]**: Fix `Failed to collapse` on a path through a symlink out of the project root, into a directory whose name ends in `.` (#2237 by @robhogan)
 - **[Fix]**: `require.context` on a directory above the project root follows symlinks inside the project root, and lists each level correctly from two or more levels up (#2229 by @robhogan)
 - **[Fix]**: Use a separate file map cache under Bun, which can't read Node's cache (#2209 by @robhogan)
 - **[Fix]**: Include dependencies required by Metro's published TypeScript declarations (#2149 by @sorinc03)
 - **[Performance]**: Dev server memory is ~95% lower per lazy-loaded route (`import()` or async routes), and lazy bundle requests are ~2x faster (#2239 by @robhogan)
 - **[Performance]**: Without Watchman, the dev server is ready to bundle ~2.5-3x faster on Linux and Windows, and ~15% faster on macOS (#2104, #2105, #2106, #2107, #2108, #2219, #2220, #2221, #2240 by @robhogan)
 - **[Performance]**: Without Watchman, CI builds start bundling ~25-35% sooner on Linux and Windows, and ~15% sooner on macOS (#2104, #2219, #2220, #2240 by @robhogan)
 - **[Performance]**: At least 10x faster `composeSourceMaps` for large bundles (#2250 by @yhpark)
 - **[Types]**: Remove undeclared type-only imports between Metro packages. `bundle_options` moves from `metro-core`'s `LogEntry` to `MetroLogEntry`, exported by `metro` - annotate `Logger.on` handlers that read it as `(entry: MetroLogEntry) => ...` (#2257 by @robhogan)

> NOTE: Experimental features are not covered by semver and can change at any time.

 - **[Experimental]**: Share module outputs between graphs with equal transform options, on by default. Set `transformer.unstable_shareTransformResults: false` to opt out if a transform's output depends on inputs outside its cache key, or if a custom serializer writes to module outputs or dependencies (#2239, #2244 by @robhogan)
 - **[Experimental]**: Add `unstable_hedgeCacheReadsAfterMs`, which transforms a module locally when its cache read is slow and takes whichever finishes first, and `maxInFlightReads` on `HttpGetStore`, which misses rather than queueing once that many reads are outstanding (#2249 by @GijsWeterings)

 **Full Changelog**: v0.88.0...v0.88.1
```

## Shared transform results

`transformer.unstable_shareTransformResults` is on by default. `Transformer` keeps one in-memory copy of each module's `output` and `dependencies` per set of transform options, checked before `cacheStores`, and every graph with equal transform options gets that same object. That's where the lazy bundling memory savings come from. Set it to `false` for the previous behavior. It's left out of the transform cache key, so switching it doesn't invalidate any cache.

Two kinds of code depend on the old behavior and should opt out:

 - A transform whose output depends on inputs outside its cache key, and which relies on a cache store declining its result so that it runs again, now gets its first result back for as long as the key is unchanged. In Expo, this showed up as stale Tailwind CSS in dev.
 - Code that writes to `Module.output` or to dependency data (`TransformResultDependency`), which the types already make read-only, now changes every graph holding the same module. Custom serializers that edit outputs in place are the likely case.

Entries are released when no graph in an `IncrementalBundler` (ie `Server`, `runBuild`) holds the module any more. Code that constructs `Transformer` or `DeltaBundler` directly, without `IncrementalBundler`'s `onModulesReleased` wiring, never releases them, so a long-lived `Transformer` keeps the latest result for every module and transform options it has seen. Set the option to `false` there unless that's intended.

## Migration notes

None of these need a change for apps on Metro's public API, but integrations using `metro*/private/*` or Metro's types may need one:

 - `createFileMap` (`metro/private/node-haste/DependencyGraph/createFileMap`) returns `hasteMap: null` unless `hasteImplModulePath` is set or `enableGlobalPackages` is on, and its type is now `?HasteMap`. Callers that read it need a null check, eg `hasteMap?.getModule(...)`. Configs that set either option get the same `HastePlugin` and cache as before.
 - `CacheManager.write`'s `getSnapshot` now returns the file map's live tree, not a copy. A custom `unstable_fileMapCacheManagerFactory` cache manager must serialize what `getSnapshot` returns before any `await`, or copy it. `TreeFS.getSerializableSnapshot()` still returns a copy.
 - The node crawler no longer stats regular files up front, so `linkStats()` can return a null `modifiedTime` and `size` for a file Metro hasn't read. `CACHE_BREAKER` is bumped (14 -> 15), so file map caches written by 0.88.0 are rebuilt once. Watchman crawls are unaffected.
 - File map plugin names must be unique across all plugins. A plugin's `onChanged` may return a summary, published by plugin name on `ChangeEvent.pluginChanges`, and code that builds `ChangeEvent`s by hand needs a `pluginChanges` map.
 - `TreeFS` gives up on a path after 40 symlink follows, as Linux does, and reports it missing.
 - The file map's `FileSystem` interface has `isMapped`, and a missing `LookupResult` carries `canonicalMissingPath` and `remainingPath`, so a custom `FileSystem` implementation needs the method and code that builds missing results by hand needs the fields. A `TreeFS` constructed without `setRoots` treats every path as mapped, as before.
 - Type-only, from removing undeclared type imports between packages: `bundle_options` moves from `metro-core`'s `LogEntry` to `MetroLogEntry`, exported by `metro`, so annotate `Logger.on('log', (entry: MetroLogEntry) => ...)` handlers that read it. `AmbiguousModuleResolutionError#hasteError` is a structural interface, no longer assignable to `DuplicateHasteCandidatesError`. Custom resolvers can't read other fields of `context.dependency.data` (use `context.isESMImport`). `MinifierResult.map` and `decodedMap` are read-only.
 - `excludeSource` is on `SerializerOptions`, typed `?boolean`, so a custom serializer's options type that narrows it to `boolean` no longer accepts Metro's.

## Test plan

```
node ./scripts/updateVersion 0.88.1
sl diff --stat
```

Only the 17 `packages/*/package.json` manifests change, and every `0.88.0` reference is replaced. Flow, ESLint, Prettier, TypeScript, API snapshots, Markdown lint and Jest pass on this commit.

Benchmark (AI-driven): the `[Performance]` figures are net against 0.88.0 from npm, with a byte-identical copy of 0.88.0 as a noise control. Startup used #2104's benchmark app on macOS, Linux and Windows, over 12 interleaved rounds, timed from process start to the first transform: a dev server (`runServer`, watching) taking an immediate bundle request, and a CI build (`runBuild` with `CI=1` and no file map cache). Lazy chunks used Mattermost's iOS dev server with 20 lazily loaded screens, and `composeSourceMaps` used Mattermost's Android release bundle (7,194 modules) and its `hermesc -O` map. Bundles and composed maps are byte-identical between the two versions. These were measured at 1f9812e. None of the seven later commits changes what they time: startup ends before the first transform, the lazy chunk runs read a warm cache, and #2287 changes the crawler only by an equivalent path join.

Expo `main` was validated against this release at 1f9812e58d8f, before the last seven commits on `main`, packed as 0.88.1 and wrapped as a local `@expo/metro` 58.2.0. Typecheck and unit tests pass across Expo's Metro packages, as do the repo build, React Server Components, native and Tailwind exports, and dev server SSR, bundles, source maps and file watching. Expo needed changes of its own (sharing off, for the Tailwind case above, and catching up with `excludeSource`, `reservedString` and `globalPrefix`), and no Metro changes. Of the seven later commits, Expo's defaults only reach the constant folding fixes #2215 and #2279, and only where `undefined` is shadowed or a dead branch's `var` is used outside it. Expo CLI has its own file map, so #2287 doesn't reach it, and Expo compiles imports and exports with its own plugin, doesn't memoize inline requires or hedge cache reads, and its default cache store writes atomically already.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants