Repository navigation
Resolver: Collect file system observations per resolution under unstable_incrementalResolution - #1949
Conversation
b2faf45 to
4e24402
Compare
4e24402 to
04896ea
Compare
Stacked on #1949. ## Summary #1949 records what a resolution looks up in the file map. It doesn't record Haste lookups, which aren't paths - the resolver asks the Haste map for a module or package by name, for every bare specifier when `allowHaste` is set. A module of that name appearing later changes the result, so the name needs observing whether or not it was found. This adds `haste` to `ResolutionObservations`, the set of Haste names a resolution looked up. They're recorded in the two Haste closures that `ModuleResolver` already creates per resolution, so nothing else changes and the resolution context is untouched. It's the bare name that's recorded, not the platform - `HastePlugin` falls back from the platform to `native` and then to the generic module of that name, so a change to any of those bindings can change the answer. Haste packages share the bucket, since they share the namespace. `metro-file-map` doesn't change. Its `Observations` is an interface, and Metro's record is a wider type that satisfies it, which is why that type wasn't exported. Two things keep this cheap: - Nothing is observed when no file can be given a Haste name - no `hasteImplModulePath` and `enableGlobalPackages: false`, which is Metro's default. The Haste map is empty then and always will be, so every lookup is a guaranteed miss, and recording one per bare specifier would be paying to observe something that can't change. `createModuleResolver` works this out from config, by the same rule `HastePlugin`'s filter uses. - The set is created on the first name recorded, and `haste` is `null` until then. Most resolutions are relative imports that never consult Haste. Allocating a set for each when Haste is enabled cost ~0.9MB on the benchmark app for ~0.2MB of entries. Nothing reads these yet. Invalidating from them needs `HastePlugin` to report which names changed binding in a batch - it's the only thing that knows, since a removed file's name is gone from the map by the time anyone else sees the change event. That's to follow, alongside invalidation generally. The empty module is resolved with `allowHaste: false`, so it observes no names and there's nothing to merge for it. ## Benchmark Resolution time in isolation, flag on in both columns, through the whole-resolution cache (see the test plan). No measurable cost either way - everything is within about half a percent, inside the run-to-run spread. | scenario | #1949 | this diff | #1949, Haste enabled | this diff, Haste enabled | |---|---|---|---|---| | first build (ms) | 112.4 | 111.8 | 111.2 | 110.6 | | rebuild, nothing changed (ms) | 7.75 | 7.78 | 7.81 | 7.79 | | after a source edit, the edited module's dependencies (µs) | 177 | 168 | 170 | 172 | | after a `package.json` change, re-resolving everything (ms) | 93.2 | 93.2 | 93.6 | 94.1 | | heap retained after resolving (MB) | 18.8 | 18.8 | | 19.1 | With Haste enabled, 27% of distinct resolutions look up a name (1.9k of 6.9k), one name each, over 72 distinct names. With it inert, no resolution carries a set. ## Test plan ``` yarn jest packages/metro-file-map packages/metro-resolver packages/metro/src/node-haste packages/metro/src/DeltaBundler packages/metro/src/integration_tests yarn flow check yarn typecheck-ts yarn verify-api-snapshots ``` New tests in `resolver-test.js`: nothing is observed when no file can be given a Haste name, a name is recorded when it's found and when it isn't, a deep import records only its package name, a relative import records nothing, and a global package name is recorded. The existing exact-set expectations gain `haste: null`. **Benchmark app** - a synthetic mid-size RN 0.87 app: 23 common dependencies (Reanimated, React Navigation, TanStack Query, lodash, etc.) and ~1,000 generated first-party modules, 60% of them in a workspace package outside `projectRoot` with its own `node_modules`. The iOS dev bundle is 2.8k modules, from a file map of 34k files. Timings replay the `resolveDependency` calls a real build of it makes against a fresh `DependencyGraph` per process, in interleaved rounds, with source and `package.json` edits made on disk and picked up by the watcher. "Haste enabled" is the same app with `enableGlobalPackages: true`, which gives its first-party packages Haste names. These timings isolate resolution - no transformation, serialisation or file reads are included. A real build of this app takes around 4s even with a warm transform cache, so resolution is about 2% of it. Changelog: Internal
97d1a02 to
476f5ab
Compare
476f5ab to
526109e
Compare
526109e to
8226d83
Compare
8226d83 to
4437f0b
Compare
4437f0b to
6ff51cb
Compare
6ff51cb to
69e67ba
Compare
69e67ba to
c5e27e6
Compare
c5e27e6 to
d83ae43
Compare
4a700b3 to
33ce98c
Compare
33ce98c to
6b94381
Compare
Metro review: Looks goodRisk: low · Commit: With Findings
Verified
Benchmarks (base vs this PR)
Method
Not run: No heap measurement, so the description's 2.1x retained-heap figure is unchecked. The benchmark fixture is Metro's own packages, not an RN app: it has a flat node_modules-free layout and no assets. Windows paths are not checked: the observations come from TreeFS canonical paths and the tests build expectations with system separators, but nothing was run on Windows. Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call. |
| }); | ||
| }); | ||
|
|
||
| test('origins sharing a cached resolution share its observations', async () => { |
There was a problem hiding this comment.
nit · No test covers a custom resolveRequest's lookups being recorded
The description gives one property of the design: the capabilities are bound to the record in the context, so a custom resolveRequest gets whatever it looks up through context recorded without knowing about it. The new tests only cover the default resolver. A later refactor could rebuild the context the custom resolver receives, or pass it the unbound this._options.fileSystemLookup. Observations would then quietly stop covering custom resolvers, and no test would fail.
A test like this one pins it, and it passes on this head:
test('a custom resolveRequest records lookups made through context', async () => {
setMockFileSystem({'index.js': '', 'a.js': ''});
resolver = await createResolver({resolver: {
unstable_incrementalResolution: true,
resolveRequest: (context, name, platform) => {
context.fileSystemLookup(p('/root/probe.js'));
return context.resolveRequest(context, name, platform);
},
}}, 'ios');
const res = resolver.resolve(p('/root/index.js'), dep('./a'));
expect(res.unstable_observations?.existence).toContain('probe.js');
});Evidence: I added the test above to the head checkout's resolver-test.js, in the unstable_incrementalResolution describe block, and it passes. None of the PR's tests set resolveRequest. · id a61da185
…ble_incrementalResolution The first consumer of the `Observations` API from #1944. With `resolver.unstable_incrementalResolution` on, each resolution records everything it looks up in the file map, and the record is returned on the `BundlerResolution` as `unstable_observations`. Invalidating from it is the next step, so resolution results are unaffected, and with the flag off nothing is allocated. `ModuleResolver` already builds a resolution context per call, so with the flag on it binds the file system capabilities in that context to the resolution's record: https://github.com/react/metro/blob/8f49d3681cfd567c821aaadc958b99fa1d58b4ca/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js#L144-L166 A custom `resolveRequest` therefore records whatever it looks up through the context without knowing about any of this, and `DependencyGraph`'s whole-resolution cache carries the record along with the cached resolution. Still to follow: `package.json` contents, which are still read outside the file map, Haste lookups (#1952), and failed resolutions. With the flag on this costs ~13% of resolution time (~2µs per resolution) and 2.1x the retained heap (5.3MB -> 11.1MB on our benchmark app). Nearly all of that is extension and platform candidates that missed (`a.ios.js`, `a.native.js`, ...), which is what to go after before this is worth turning on, eg by recording the directory once. With the flag off, everything is within noise. ## Benchmark AI-driven. Every `resolveDependency` call of one iOS dev build replayed through the whole-resolution cache, 7 interleaved rounds. Heap is measured in a fresh process per configuration. Resolutions are identical with the flag on and off. | scenario | `main` | this PR, flag off | this PR, flag on | |---|---|---|---| | first build (ms) | 129.0 | 131.0 (+1.6%) | 137.4 (+6.6%) | | rebuild, nothing changed (ms) | 7.98 | 7.96 (-0.3%) | 8.04 (+0.8%) | | after a source edit, re-resolving the edited module's dependencies (µs) | 229 | 219 (-4.3%) | 256 (+11.8%) | | after a `package.json` change, re-resolving everything (ms) | 102.5 | 101.9 (-0.6%) | 116.2 (+13.3%) | | heap retained after resolving (MB) | 5.3 | 5.3 | 11.1 (2.1x) | Changelog: Internal ## Test plan New tests assert the exact sets recorded for a relative import, a package walk, a symlink, asset variants and the empty module, and that nothing is present by default. Benchmark app: the synthetic RN 0.87 app described in #1945 (2.8k-module bundle, 34k-file file map).
1daf793 to
5c6352a
Compare
…1952) ## Context #1949 records what a resolution looks up in the file map, but Haste lookups aren't paths. With `allowHaste`, every bare specifier is looked up by name in the Haste map, and a module of that name appearing later changes the result. So the name needs observing whether or not it was found. ## This change This adds `haste` to `ResolutionObservations`. It's the bare name, not the platform, because `HastePlugin` falls back from platform to `native` to generic and a change to any of those can change the answer: https://github.com/react/metro/blob/6dbe97956af0f66f7a1d88014ba0b784a4a4d893/packages/metro-file-map/src/plugins/HastePlugin.js#L194-L213 It's free in the default config, where nothing can have a Haste name and the map is empty. Otherwise the set is created on the first name recorded, since most resolutions are relative imports that never consult Haste. No measurable cost either way. Invalidating from these needs `HastePlugin` to report which names a change rebound, which is #1953. ## Benchmark AI-driven, flag on in all columns, 7 interleaved rounds. Everything is within half a percent, inside the run-to-run spread. | scenario | #1949 | this PR | #1949, Haste enabled | this PR, Haste enabled | |---|---|---|---|---| | first build (ms) | 112.4 | 111.8 | 111.2 | 110.6 | | rebuild, nothing changed (ms) | 7.75 | 7.78 | 7.81 | 7.79 | | after a source edit, the edited module's dependencies (µs) | 177 | 168 | 170 | 172 | | after a `package.json` change, re-resolving everything (ms) | 93.2 | 93.2 | 93.6 | 94.1 | | heap retained after resolving (MB) | 18.8 | 18.8 | | 19.1 | Changelog: Internal ## Test plan New tests cover Haste inert, a name found and not found, a deep import, and a relative import. Benchmark app: the synthetic RN 0.87 app described in #1945 (2.8k-module bundle, 34k-file file map). "Haste enabled" sets `enableGlobalPackages: true`, which gives its first-party packages Haste names.
#1949 records what a successful resolution observes of the file system, but a failed resolution throws and its record is discarded. That record is what's needed to explain a failure - eg whether a candidate was missing or a directory, or missing only because it isn't watched - or to know when a retry might succeed. This attaches it to `UnableToResolveError` as `unstable_observations`, with what the file map held at each path when the resolution failed: `'f'`, `'d'`, `'l'` or `null`, without following a symlink there. Paths are relative to `rootDir`, and everything is read from the file map rather than the disk, so it's accurate for a virtual file system too. With `unstable_incrementalResolution` on, the record comes from the failed attempt. Otherwise Metro replays the failure once with recording (one extra resolution per failure that breaks the build), which is sound because resolution is synchronous, so the replay sees the same file map. Optional dependencies aren't replayed, since their failures are expected. If a replay succeeds, a custom resolver is reading something outside its resolution context, so Metro warns and throws the original error. A symlink only appears in `existence` where `TreeFS` gives up following a cycle or a long chain: https://github.com/react/metro/blob/dd15cf3728c4a193f80b6b8ad8e048ecbb106b60/packages/metro-file-map/src/lib/TreeFS.js#L960-L967 Note that `package.json` contents aren't recorded as read yet, so a `"main"` pointing at a missing file shows the `package.json` and the missing file, but not that one led to the other. I'd like to settle the shape before anything depends on it, hence draft and `unstable_`. Changelog: ``` - **[Experimental]**: `UnableToResolveError.unstable_observations` describes what a failed resolution observed of the file map ``` Test plan: New unit tests.
…o resolver-observed paths With `resolver.unstable_incrementalResolution` on, resolutions have recorded what they observed since #1949, with the plan to consume those observations to invalidate resolution. This adds the consumer. `Graph` keeps what each module's resolutions observed (carried on `ModuleData`), and `DeltaCalculator` turns each batch of changes into the modules to revisit, before it filters to modules already in the graph. Modules holding an unresolved optional dependency are revisited on any addition or removal. ## Performance The priority here is correct, end-to-end behaviour - it's neither fast nor memory efficient, but that's not a concern while it's flagged off. We'll optimise as follow-ups before stabilising the flag, but I'm leaving optimisations out at this stage to avoid bloating these already large PRs. That said, with this first version, enabling incremental resolution adds ~35% to pure resolution time (~20% of that is recording observations, as since #1949) and ~6-7x the resolution cache's heap. The scan on each change isn't measured yet. I'd expect both to be small in the context of a real build, though I haven't measured that. (For the record: reducing the ingested paths so that we don't store every extension candidate, and deferring ingestion until after a build, are planned) ## Changelog ``` - **[Experimental]**: `resolver.unstable_incrementalResolution` is now implemented end-to-end ``` ## Test plan New unit tests for the index, `Graph` and `DeltaCalculator`, and a watch-mode integration test that creates a more specific platform file, installs a nearer package, creates the file a failed optional `require` wanted, and changes a package's `main`, against a real file map, checking the next delta with the flag on and off (off stays stale, as today).
…o resolver-observed paths With `resolver.unstable_incrementalResolution` on, resolutions have recorded what they observed since #1949, with the plan to consume those observations to invalidate resolution. This adds the consumer. `Graph` keeps what each module's resolutions observed (carried on `ModuleData`), and `DeltaCalculator` turns each batch of changes into the modules to revisit, before it filters to modules already in the graph. Modules holding an unresolved optional dependency are revisited on any addition or removal, or `package.json` change. Graph resets on symlink and (with package exports on) `package.json` changes, and clearing the resolution cache on every change, are unchanged. ## Performance The priority here is correct, end-to-end behaviour - it's neither fast nor memory efficient, but that's not a concern while it's flagged off. We'll optimise as follow-ups before stabilising the flag, but I'm leaving optimisations out at this stage to avoid bloating these already large PRs. That said, with this first version, enabling incremental resolution adds ~35% to pure resolution time (recording alone, as since #1949, adds ~20%) and ~6-7x the resolution cache's heap. I'd expect both to be small in the context of a real build, but haven't measured that, or the scan on each change. (For the record: reducing the ingested paths so that we don't store every extension candidate, and deferring ingestion until after a build, are planned.) ## Changelog ``` - **[Experimental]**: `resolver.unstable_incrementalResolution` is now implemented end-to-end ``` ## Test plan New unit tests for the index, `Graph` and `DeltaCalculator`, and watch-mode integration tests against a real file map that check the next delta after creating a more specific platform file, installing a nearer package, creating the file a failed optional `require` wanted, and changing a package's `main` (directly, through a symlinked workspace, and to fix a failed optional `require`).
#1949 records what a successful resolution observes of the file system, but a failed resolution throws and its record is discarded. That record is what's needed to explain a failure - eg whether a candidate was missing or a directory, or missing only because it isn't watched - or to know when a retry might succeed. This attaches it to `UnableToResolveError` as `unstable_observations`, with what the file map held at each path when the resolution failed: `'f'`, `'d'`, `'l'` or `null`, without following a symlink there. Paths are relative to `rootDir`, and everything is read from the file map rather than the disk, so it's accurate for a virtual file system too. With `unstable_incrementalResolution` on, the record comes from the failed attempt. Otherwise Metro replays the failure once with recording (one extra resolution per failure that breaks the build), which is sound because resolution is synchronous, so the replay sees the same file map. Optional dependencies aren't replayed, since their failures are expected, and their errors skip the code frame, which reads the origin file from disk only for `buildSubgraph` to discard it. If a replay succeeds, a custom resolver is reading something outside its resolution context, so Metro warns and throws the original error. A symlink only appears in `existence` where `TreeFS` gives up following a cycle or a long chain: https://github.com/react/metro/blob/dd15cf3728c4a193f80b6b8ad8e048ecbb106b60/packages/metro-file-map/src/lib/TreeFS.js#L960-L967 Note that `package.json` contents aren't recorded as read yet, so a `"main"` pointing at a missing file shows the `package.json` and the missing file, but not that one led to the other. I'd like to settle the shape before anything depends on it, hence draft and `unstable_`. Changelog: ``` - **[Experimental]**: `UnableToResolveError.unstable_observations` describes what a failed resolution observed of the file map ``` Test plan: New unit tests.
…o resolver-observed paths With `resolver.unstable_incrementalResolution` on, resolutions have recorded what they observed since #1949, with the plan to consume those observations to invalidate resolution. This adds the consumer. `Graph` keeps what each module's resolutions observed (carried on `ModuleData`), and `DeltaCalculator` turns each batch of changes into the modules to revisit, before it filters to modules already in the graph. Modules holding an unresolved optional dependency are revisited on any addition or removal, or `package.json` change. Graph resets on symlink and (with package exports on) `package.json` changes, and clearing the resolution cache on every change, are unchanged. ## Performance The priority here is correct, end-to-end behaviour - it's neither fast nor memory efficient, but that's not a concern while it's flagged off. We'll optimise as follow-ups before stabilising the flag, but I'm leaving optimisations out at this stage to avoid bloating these already large PRs. That said, with this first version, enabling incremental resolution adds ~35% to pure resolution time (recording alone, as since #1949, adds ~20%) and ~6-7x the resolution cache's heap. I'd expect both to be small in the context of a real build, but haven't measured that, or the scan on each change. (For the record: reducing the ingested paths so that we don't store every extension candidate, and deferring ingestion until after a build, are planned.) ## Changelog ``` - **[Experimental]**: `resolver.unstable_incrementalResolution` is now implemented end-to-end ``` ## Test plan New unit tests for the index, `Graph` and `DeltaCalculator`, and watch-mode integration tests against a real file map that check the next delta after creating a more specific platform file, installing a nearer package, creating the file a failed optional `require` wanted, and changing a package's `main` (directly, through a symlinked workspace, and to fix a failed optional `require`).
#1949 records what a successful resolution observes of the file system, but a failed resolution throws and its record is discarded. That record is what's needed to explain a failure - eg whether a candidate was missing or a directory, or missing only because it isn't watched - or to know when a retry might succeed. This attaches it to `UnableToResolveError` as `observations`, with what the file map held at each path when the resolution failed: `'f'`, `'d'`, `'l'` or `null`, without following a symlink there. Paths are relative to `rootDir`, and everything is read from the file map rather than the disk, so it's accurate for a virtual file system too. With `unstable_incrementalResolution` on, the record comes from the failed attempt. Otherwise Metro replays the failure once with recording (one extra resolution per failure that breaks the build), which is sound because resolution is synchronous, so the replay sees the same file map. Optional dependencies aren't replayed, since their failures are expected, and their errors skip the code frame, which reads the origin file from disk only for `buildSubgraph` to discard it. If a replay succeeds, a custom resolver is reading something outside its resolution context, so Metro warns and throws the original error. A symlink only appears in `existence` where `TreeFS` gives up following a cycle or a long chain: https://github.com/react/metro/blob/dd15cf3728c4a193f80b6b8ad8e048ecbb106b60/packages/metro-file-map/src/lib/TreeFS.js#L960-L967 Note that `package.json` contents aren't recorded as read yet, so a `"main"` pointing at a missing file shows the `package.json` and the missing file, but not that one led to the other. I'd like to settle the shape before anything depends on it, hence draft. Changelog: ``` - **[Experimental]**: `UnableToResolveError.observations` describes what a failed resolution observed of the file map ``` Test plan: New unit tests.
…o resolver-observed paths With `resolver.unstable_incrementalResolution` on, resolutions have recorded what they observed since #1949, with the plan to consume those observations to invalidate resolution. This adds the consumer. `Graph` keeps what each module's resolutions observed (carried on `ModuleData`), and `DeltaCalculator` turns each batch of changes into the modules to revisit, before it filters to modules already in the graph. Modules holding an unresolved optional dependency are revisited on any addition or removal, or `package.json` change. Graph resets on symlink and (with package exports on) `package.json` changes, and clearing the resolution cache on every change, are unchanged. ## Performance The priority here is correct, end-to-end behaviour - it's neither fast nor memory efficient, but that's not a concern while it's flagged off. We'll optimise as follow-ups before stabilising the flag, but I'm leaving optimisations out at this stage to avoid bloating these already large PRs. That said, with this first version, enabling incremental resolution adds ~35% to pure resolution time (recording alone, as since #1949, adds ~20%) and ~6-7x the resolution cache's heap. I'd expect both to be small in the context of a real build, but haven't measured that, or the scan on each change. (For the record: reducing the ingested paths so that we don't store every extension candidate, and deferring ingestion until after a build, are planned.) ## Changelog ``` - **[Experimental]**: `resolver.unstable_incrementalResolution` is now implemented end-to-end ``` ## Test plan New unit tests for the index, `Graph` and `DeltaCalculator`, and watch-mode integration tests against a real file map that check the next delta after creating a more specific platform file, installing a nearer package, creating the file a failed optional `require` wanted, and changing a package's `main` (directly, through a symlinked workspace, and to fix a failed optional `require`).
The first consumer of the
ObservationsAPI from #1944. Withresolver.unstable_incrementalResolutionon, each resolution records everything it looks up in the file map, and the record is returned on theBundlerResolutionasunstable_observations. Invalidating from it is the next step, so resolution results are unaffected, and with the flag off nothing is allocated.This unblocks a few related goals:
require.contextas a standard virtual module - currently has bespoke DeltaCalculator handling, but if it used a resolver-driven virtual module defined by matchFiles we could invalidate it as a resolution.ModuleResolveralready builds a resolution context per call, so with the flag on it binds the file system capabilities in that context to the resolution's record:metro/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js
Lines 144 to 166 in 8f49d36
A custom
resolveRequesttherefore records whatever it looks up through the context without knowing about any of this, andDependencyGraph's whole-resolution cache carries the record along with the cached resolution.Still to follow:
package.jsoncontents, which are still read outside the file map, Haste lookups (#1952), and failed resolutions.With the flag on this costs ~13% of resolution time (~2µs per resolution) and 2.1x the retained heap (5.3MB -> 11.1MB on our benchmark app). Nearly all of that is extension and platform candidates that missed (
a.ios.js,a.native.js, ...), which is what to go after before this is worth turning on, eg by recording the directory once. With the flag off, everything is within noise.No plans to turn the flag on for all resolution just yet.
Benchmark
AI-driven. Every
resolveDependencycall of one iOS dev build replayed through the whole-resolution cache, 7 interleaved rounds. Heap is measured in a fresh process per configuration. Resolutions are identical with the flag on and off.mainpackage.jsonchange, re-resolving everything (ms)Changelog: Internal
Test plan
New tests assert the exact sets recorded for a relative import, a package walk, a symlink, asset variants and the empty module, and that nothing is present by default.
Benchmark app: the synthetic RN 0.87 app described in #1945 (2.8k-module bundle, 34k-file file map).