Skip to content

Source map consumer: Search only the target line in originalPositionFor - #1980

Open
robhogan wants to merge 1 commit into
pr1979from
pr1980
Open

robhogan wants to merge 1 commit into
pr1979from
pr1980

Conversation

@robhogan

@robhogan robhogan commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

MappingsConsumer#originalPositionFor binary-searched every mapping in the map:

originalPositionFor(
generatedPosition: GeneratedPositionLookup,
): SourcePosition {
const {line, column} = generatedPosition;
if (line == null || column == null) {
return {...EMPTY_POSITION};
}
if (generatedPosition.bias != null) {
invariant(
generatedPosition.bias === GREATEST_LOWER_BOUND,
`Unimplemented lookup bias: ${lookupBiasToString(
// $FlowFixMe[incompatible-type]
generatedPosition.bias,
)}`,
);
}
const mappings = this._decodeAndCacheMappings();
const index = greatestLowerBound(
mappings,
{line, column},
(position, mapping) => {
if (position.line === mapping.generatedLine) {
return get0(sub(position.column, mapping.generatedColumn));
}
return get0(sub(position.line, mapping.generatedLine));
},
);
if (
index != null &&
mappings[index].generatedLine === generatedPosition.line
) {
const mapping = mappings[index];
return {
source: mapping.source,
name: mapping.name,
line: mapping.originalLine,
column: mapping.originalColumn,
};
}
return {...EMPTY_POSITION};
}

This records the index of the first mapping on each generated line while decoding, and searches only that line. Results are unchanged, including a column before a line's first mapping (unmapped) and repeated generated columns (the last wins).

composeSourceMaps is 20.5% faster than #1979 (95% CI 20.1-21.1%) and 2.38x faster than main (1,107ms -> 878ms on our benchmark app), with identical output. Memory is up 1.2% (+11MB), most of it the per-line index over 1.18M generated lines.

Benchmark

AI-driven.

Time (median) vs previous vs main Composition memory vs previous vs main Peak RSS
main 2,088ms (2,082 to 2,094) 1,070MB (1,070 to 1,070) 1,664MB
#1979 1,107ms (1,099 to 1,111) -47.1% (-47.4 to -46.7) 898MB (897 to 899) -15.9% (-16.1 to -15.6) 1,492MB
This PR 878ms (872 to 883) -20.5% (-21.1 to -20.1) -58.0% (-58.1 to -57.8) 908MB (908 to 909) +1.2% (+1.0 to +1.3) -15.1% (-15.2 to -15.0) 1,502MB

Changelog

 - **[Performance]**: Faster position lookups in `metro-source-map`'s `Consumer`

Test plan

New tests for a column before a line's first mapping and for repeated generated columns. They pass on main too.

Benchmark (AI-driven): Mattermost Mobile 2.45.0 (React Native 0.83.9, Metro 0.83.7), iOS release bundle, unminified: 7,086 sources, 52.6MB bundle, 82.4MB flat source map. Composed with its hermesc -O -output-source-map map (hermes-compiler 0.14.1, 10.7MB, 1.84M segments). Timings are composeSourceMaps alone, excluding JSON parsing, over 105 rounds on Node 22.13.1 on an M5 Pro. Each round runs main, every PR in this stack and a memory baseline, in a shuffled order. Figures are medians with 95% bootstrap confidence intervals, for changes of the per-round paired ratio. Composition memory is peak RSS less that of the same process parsing both maps without composing them (594MB).

@robhogan
robhogan added this pull request to stack #1982 September 26, 2026 07:54
@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 Sep 26, 2026
@robhogan robhogan changed the title Consumer: Search only the target line in originalPositionFor Source map consumer: Search only the target line in originalPositionFor Sep 28, 2026
@robhogan
robhogan marked this pull request as ready for review September 28, 2026 12:13
@robhogan
robhogan requested a lite review from Copilot September 28, 2026 12:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

馃煝 Approval recommended

No unresolved review issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Improves metro-source-map lookup performance by restricting searches to the requested generated line while preserving behavior.

Changes:

  • Adds per-line mapping indexes during decoding.
  • Uses line-scoped binary searches.
  • Adds regression tests for lookup edge cases.
File Description
packages/鈥媘etro-source-map/鈥媠rc/鈥婥onsumer/鈥婱appingsConsumer.js Implements line-indexed position lookups.
packages/鈥媘etro-source-map/鈥媠rc/鈥媉_tests__/鈥婥onsumer-test.js Adds coverage for unmapped leading columns and duplicate generated columns.

馃挕 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

`MappingsConsumer#originalPositionFor` binary-searched every mapping in the map, comparing line and then column through a closure. This records the index of the first mapping on each generated line while decoding, and searches only the target line's mappings by column.

Results are unchanged, including for a column before the first mapping on its line (unmapped) and for several mappings at the same generated column (the last wins).

`composeSourceMaps` with a bundle map and its Hermes map is 20.5% faster than the previous diff (95% CI 20.1-21.1%) and 2.38x faster than `main` (2.37-2.39x; 1,107ms -> 878ms on our benchmark app*), with byte-identical output. Composition's memory rises by 1.2% (1.0-1.3%, +11MB*), about 9MB of which is the per-line index (1.18M generated lines in our benchmark app's bundle map).

| | Time (median) | vs previous | vs `main` | Composition memory* | vs previous | vs `main` | Peak RSS |
|---|---|---|---|---|---|---|---|
| `main` | 2,088ms (2,082 to 2,094) | | | 1,070MB (1,070 to 1,070) | | | 1,664MB |
| Previous diff | 1,107ms (1,099 to 1,111) |  | -47.1% (-47.4 to -46.7) | 898MB (897 to 899) |  | -15.9% (-16.1 to -15.6) | 1,492MB |
| This diff | 878ms (872 to 883) | -20.5% (-21.1 to -20.1) | -58.0% (-58.1 to -57.8) | 908MB (908 to 909) | +1.2% (+1.0 to +1.3) | -15.1% (-15.2 to -15.0) | 1,502MB |

## Changelog

```
 - **[Performance]**: Faster position lookups in `metro-source-map`'s `Consumer`
```

## Test plan

New tests cover a column before a line's first mapping and repeated generated columns. They pass on `main` and on this diff.

```
yarn jest packages/metro-source-map packages/metro-symbolicate
yarn flow check
yarn lint
```

\* Benchmark: Mattermost Mobile 2.45.0 (React Native 0.83.9, Metro 0.83.7), iOS release bundle, unminified: 7,086 sources, 52.6MB bundle, 82.4MB flat source map. Composed with its `hermesc -O -output-source-map` map (hermes-compiler 0.14.1; 10.7MB, 1.84M segments). Timings are `composeSourceMaps` alone, excluding JSON parsing, over 105 rounds on Node 22.13.1 on an M5 Pro; each round runs `main`, every diff in this stack and a memory baseline, in a shuffled order. Figures are medians with 95% bootstrap confidence intervals - for changes, of the per-round paired ratio. Composition memory is peak RSS less that of the same process parsing both maps without composing them (594MB).

This branch has not been deployed

No deployments
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