Skip to content

Resolver: Collect file system observations per resolution under unstable_incrementalResolution - #2231

Closed
robhogan wants to merge 1 commit into
pr2230from
pr2231
Closed

robhogan wants to merge 1 commit into
pr2230from
pr2231

Conversation

@robhogan

Copy link
Copy Markdown
Collaborator

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:

try {
const result = Resolver.resolve(
{
allowHaste,
assetExts,
customResolverOptions: resolverOptions.customResolverOptions ?? {},
dependency,
dev: resolverOptions.dev,
disableHierarchicalLookup,
extraNodeModules,
fileSystemLookup,
getPackage,
getPackageForModule,
isESMImport: dependency.data.isESMImport,
mainFields,
nodeModulesPaths,
originModulePath,
preferNativePlatform,
resolveAsset,
resolveHasteModule: (name: string) =>
this._options.getHasteModulePath(name, platform),
resolveHastePackage: (name: string) =>
this._options.getHastePackagePath(name, platform),

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).

…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).
@robhogan
robhogan added this pull request to stack #2234 November 16, 2026 16:17
@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 Nov 16, 2026
@robhogan
robhogan removed this pull request from stack #2234 November 16, 2026 16:18
@robhogan

Copy link
Copy Markdown
Collaborator Author

Opened by mistake, duplicate of #1949

@robhogan robhogan closed this Nov 16, 2026
@robhogan
robhogan deleted the pr2231 branch November 16, 2026 16:18
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.

1 participant