Repository navigation
metro-file-map: Give plugins getOrComputeFileData, record observed content - #1951
Conversation
79d24cb to
f249690
Compare
71974ca to
94702e2
Compare
94702e2 to
9e9232f
Compare
9e9232f to
2204ff8
Compare
2204ff8 to
1f33985
Compare
1f33985 to
b559e11
Compare
b559e11 to
4f7c473
Compare
a8afbec to
3837704
Compare
…ntent #1944 lets `FileSystem.lookup` record what it observed, but a file found only goes in `existence`. A plugin's per-file data is derived from the file's contents, so reading it should be recorded in `content`, which is also invalidated by modification. Plugins currently read their data from `lookup()`'s `pluginData`, and a lazy plugin calls `processFile` when that's `undefined`. Recording on either wouldn't be enough - on a warm start, or any read after the first, the data is already stored and `processFile` never runs. This replaces both with `getOrComputeFileData(path, observations?)`, on plugin init options and `FileDataPlugin`. It returns the stored data, or for a lazy plugin runs the worker in-band on a miss, and records the file in `content` either way. A missing path or directory is a result rather than an error, so a caller falling back to something else has still recorded it. Plugin `lookup` no longer returns data, so `getOrComputeFileData` is the only per-file read. The motivation is `package.json`, which Metro currently reads with `fs` outside the file map, leaving nothing to record: https://github.com/react/metro/blob/32ff3c9993384580ca40310f3c4024e5f250837b/packages/metro/src/node-haste/PackageCache.js#L24-L25 Changelog: Internal Test plan: New unit tests. Nothing passes observations to this yet, so it's only reachable from tests until the `package.json` plugin follows.
3837704 to
e8ab103
Compare
|
What if the observations created via both shouldn't we then move the file path from "existence" to "content"? |
7d694df to
e8ab103
Compare
Compute whether a plugin is lazy once per plugin at initialization, rather than on every miss in getOrComputeFileData. A non-lazy plugin misses on every read of a file its filter excludes. Correct the TreeFS comment that said a path belongs in only one of existence and content, and document on Observations.content that a path may be in both without changing what invalidates the result.
Yeah good question - there's an argument for that, but I think it's easiest + most performant if we treat all of these sets as write-only during file map access. I've change a comment that suggested they were mutually exclusive. |
TreeFS.lookup now returns the canonicalPath it found, and getOrComputeFileData adds it to observations.content after a plain lookup. This removes lookupContent, leaving TreeFS with one lookup method. A file read is now recorded in existence as well as content, which is redundant but doesn't change what invalidates the result.
|
Modified this a bit further - |
## Why #45378 made file processing async. That blocks porting upstream's lazy plugin workers (react/metro#1950, react/metro#1951), which run a plugin's worker in-band from synchronous callers such as resolution: https://github.com/react/metro/blob/3c56803664df86a91b7b4b4c2004247c4fb4a32c/packages/metro-file-map/src/lib/FileProcessor.js#L157-L180 ## How This reverts the async part of #45378, so the worker, `FileProcessor` and plugin workers match upstream again, and `getContent()` returns a `Buffer`. It's also faster. With the SHA-1 requests from a real cold iOS build, lazy SHA-1 is ~24% faster, and eager crawls in worker threads are ~46% faster. The cost is that an in-band batch (`maxWorkers: 1`) blocks the event loop rather than yielding between files, as upstream's does. Expo's defaults never do that. ### Benchmark (AI-driven) | Workload | `main` | This PR [95% CI] | Control [95% CI] | |---|---|---|---| | Cold build, default options | 99.9 ms | +0.2% [-2.8, +4.3] | +1.2% [+0.1, +2.1] | | Warm start, default options | 93.9 ms | +0.3% [-1.4, +1.9] | +2.4% [-1.5, +3.5] | | Lazy SHA-1, a cold build's bursts | 108.5 ms | -24.2% [-24.8, -23.7] | +0.2% [-0.4, +0.6] | | Lazy SHA-1, a cold build's bursts, main thread busy | 102.7 ms | -19.9% [-20.7, -19.5] | +0.2% [-0.7, +0.5] | | Lazy SHA-1, one at a time | 201.0 ms | -51.1% [-51.4, -50.7] | +0.1% [-0.2, +0.7] | | Lazy SHA-1, all 3,062 at once | 67.6 ms | +7.2% [+5.5, +9.4] | -0.8% [-1.6, +0.9] | | Eager SHA-1 + dependencies, `maxWorkers: 1` | 722.9 ms | -11.5% [-12.3, -9.8] | +0.0% [-0.9, +0.5] | | Eager SHA-1 + dependencies, 8 worker threads | 787.9 ms | -45.7% [-45.9, -45.3] | +0.3% [-0.2, +0.5] | Fixture: a pnpm React Native app with 27,459 files and 3,030 symlinks, on macOS (Mac mini, 12 cores), Node 24.21. 20 rounds, a fresh process per arm and workload, in random order each round, paired against `main`, with a byte-identical control build of `main`. Medians of paired changes, with bootstrap 95% CIs. File metadata, SHA-1s, returned contents and dependency data are identical across all arms. Not measured: a cold OS page cache, slow or network disks, or Windows, which is where async reads could plausibly help lazy SHA-1. ## Test Plan Unit tests updated for the synchronous `processFile` signature. CI covers the CLI E2E suite for this package. ## Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
## Why #45378 made file processing async. That blocks porting upstream's lazy plugin workers (react/metro#1950, react/metro#1951), which run a plugin's worker in-band from synchronous callers such as resolution: https://github.com/react/metro/blob/3c56803664df86a91b7b4b4c2004247c4fb4a32c/packages/metro-file-map/src/lib/FileProcessor.js#L157-L180 ## How This reverts the async part of #45378, so the worker, `FileProcessor` and plugin workers match upstream again, and `getContent()` returns a `Buffer`. It's also faster. With the SHA-1 requests from a real cold iOS build, lazy SHA-1 is ~24% faster, and eager crawls in worker threads are ~46% faster. The cost is that an in-band batch (`maxWorkers: 1`) blocks the event loop rather than yielding between files, as upstream's does. Expo's defaults never do that. ### Benchmark (AI-driven) | Workload | `main` | This PR [95% CI] | Control [95% CI] | |---|---|---|---| | Cold build, default options | 99.9 ms | +0.2% [-2.8, +4.3] | +1.2% [+0.1, +2.1] | | Warm start, default options | 93.9 ms | +0.3% [-1.4, +1.9] | +2.4% [-1.5, +3.5] | | Lazy SHA-1, a cold build's bursts | 108.5 ms | -24.2% [-24.8, -23.7] | +0.2% [-0.4, +0.6] | | Lazy SHA-1, a cold build's bursts, main thread busy | 102.7 ms | -19.9% [-20.7, -19.5] | +0.2% [-0.7, +0.5] | | Lazy SHA-1, one at a time | 201.0 ms | -51.1% [-51.4, -50.7] | +0.1% [-0.2, +0.7] | | Lazy SHA-1, all 3,062 at once | 67.6 ms | +7.2% [+5.5, +9.4] | -0.8% [-1.6, +0.9] | | Eager SHA-1 + dependencies, `maxWorkers: 1` | 722.9 ms | -11.5% [-12.3, -9.8] | +0.0% [-0.9, +0.5] | | Eager SHA-1 + dependencies, 8 worker threads | 787.9 ms | -45.7% [-45.9, -45.3] | +0.3% [-0.2, +0.5] | Fixture: a pnpm React Native app with 27,459 files and 3,030 symlinks, on macOS (Mac mini, 12 cores), Node 24.21. 20 rounds, a fresh process per arm and workload, in random order each round, paired against `main`, with a byte-identical control build of `main`. Medians of paired changes, with bootstrap 95% CIs. File metadata, SHA-1s, returned contents and dependency data are identical across all arms. Not measured: a cold OS page cache, slow or network disks, or Windows, which is where async reads could plausibly help lazy SHA-1. ## Test Plan Unit tests updated for the synchronous `processFile` signature. CI covers the CLI E2E suite for this package. ## Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) (cherry picked from commit d031acc)
#1944 lets
FileSystem.lookuprecord what it observed, but a file found only goes inexistence. A plugin's per-file data is derived from the file's contents, so reading it should be recorded incontent, which is also invalidated by modification.Plugins currently read their data from
lookup()'spluginData, and a lazy plugin callsprocessFilewhen that'sundefined. Recording on either wouldn't be enough - on a warm start, or any read after the first, the data is already stored andprocessFilenever runs.This replaces both with
getOrComputeFileData(path, observations?), on plugin init options andFileDataPlugin. It returns the stored data, or for a lazy plugin runs the worker in-band on a miss, and records the file incontenteither way. A missing path or directory is a result rather than an error, so a caller falling back to something else has still recorded it. Pluginlookupno longer returns data, sogetOrComputeFileDatais the only per-file read.The motivation is
package.json, which Metro currently reads withfsoutside the file map, leaving nothing to record:metro/packages/metro/src/node-haste/PackageCache.js
Lines 24 to 25 in 32ff3c9
Changelog: Internal
Test plan:
New unit tests. Nothing passes observations to this yet, so it's only reachable from tests until the
package.jsonplugin follows.