Skip to content

metro-source-map: Look up source content in indexed maps without querying every section - #2250

Merged
robhogan merged 2 commits into
react:mainfrom
yhpark:ye/sections-consumer-source-content
Nov 19, 2026
Merged

robhogan merged 2 commits into
react:mainfrom
yhpark:ye/sections-consumer-source-content

Conversation

@yhpark

@yhpark yhpark commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Summary

React Native release builds compose Metro's bundle source map with the Hermes compiler's map using composeSourceMaps. Since Metro 0.86.0, the bundle map is an indexed map with one section per module. composeSourceMaps gets each source's content with sourceContentFor, and SectionsConsumer.sourceContentFor asks every section in turn, normalizing the requested path again in each one. The time grows with sources × sections: for an app with 17,110 modules, composing takes about 50 s, nearly all of it in these lookups.

SectionsConsumer now builds a map from normalized source to content on its first sourceContentFor call. When several sections have content for a source, the first one still wins, so the composed map is unchanged; for the app above, composing takes about 2.5 s. Maps with a section that has a sourceRoot keep asking every section.

Changelog: [Performance] Faster composeSourceMaps for bundles with many modules

Test plan

  • New Consumer tests for indexed maps: a section without sourcesContent and a missing source, a source repeated within a section, a requested path that needs normalizing, a map with a sourceRoot section, and a nested section. Removing the sourceRoot fallback, the recursion into nested sections, the first-entry rule or the skip for sections without sourcesContent each fails the matching test.
  • yarn jest packages/metro-source-map, flow check, ESLint and Prettier pass.
  • composeSourceMaps from source, main and this PR, 5 interleaved runs each. Output is byte-identical.
Input main (median) This PR (median)
17,000 one-module sections and a Hermes-style map (script below) 16,956 ms 91 ms
Android release bundle of a 17,110-module app and its hermesc -O map 52,733 ms 2,530 ms
Script for the synthetic maps
// node gen-indexed-maps.js <modules> <outDir>: an indexed map with one section per module, like
// Expo's serializer emits, and a Hermes-style map with one mapping per module on a single line.
const fs = require('fs');
const path = require('path');
const [n, out] = [Number(process.argv[2]), process.argv[3]];
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const vlq = v => {
  let x = v < 0 ? (-v << 1) | 1 : v << 1, s = '';
  do { let d = x & 31; x >>>= 5; if (x) d |= 32; s += B64[d]; } while (x);
  return s;
};
const sections = [];
for (let i = 0; i < n; i++) {
  sections.push({
    offset: {line: i, column: 0},
    map: {version: 3, sources: [`src/module${i}.js`], sourcesContent: [`export default ${i};\n`], names: [], mappings: 'AAAA'},
  });
}
const segments = [];
for (let i = 0; i < n; i++) segments.push(vlq(i === 0 ? 0 : 10) + 'A' + vlq(i === 0 ? 0 : 1) + 'A');
fs.writeFileSync(path.join(out, 'indexed.map'), JSON.stringify({version: 3, sections}));
fs.writeFileSync(path.join(out, 'hermes.map'), JSON.stringify({version: 3, sources: ['bundle.js'], names: [], mappings: segments.join(',')}));

@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 19, 2026
@yhpark yhpark changed the title SectionsConsumer: Look up source content without querying every section metro-source-map: Look up source content in indexed maps without querying every section Nov 19, 2026

@robhogan robhogan 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.

Wow, nice! Thanks

@robhogan
robhogan merged commit 47ba8a5 into react:main Nov 19, 2026
16 checks passed
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