Skip to content

Add lazy_bundling benchmark for dev server heap and lazy chunk request time - #2242

Merged
vzaidman merged 1 commit into
mainfrom
vzaidman/lazy-bundling-benchmark
Nov 17, 2026
Merged

vzaidman merged 1 commit into
mainfrom
vzaidman/lazy-bundling-benchmark

Conversation

@vzaidman

@vzaidman vzaidman commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds lazy_bundling to scripts/benchmark-patches: a benchmark of what lazy bundling costs the dev server, as the heap its chunk graphs hold and the time chunk requests take.

With lazy bundling, each import() target is served by a graph of its own that holds the chunk's whole transitive closure, so these costs grow with every chunk a session loads. bundling_speed can't see them, because a cold build of one bundle builds one graph. For example, #2239 makes no measurable difference to bundling_speed on an internal Meta app, but cuts the heap that app's chunk graphs hold by 94%, and Mattermost's by 95% (see the examples below).

For each run, lazy_bundling:

  • starts a fresh dev server with --server-command (default: this checkout's metro serve), passing --port with a free port;
  • requests the entry's lazy dev bundle (dev=true&lazy=true, plus --query), then up to --chunks lazy chunks one at a time, taking the async bundle URLs from the bundles' dependency maps breadth first, as an app loads them;
  • reads the server's heap (heapUsed after two forced GCs) after the main bundle and after the chunks, through heap-probe.js. The probe is preloaded into the server's Node.js processes with NODE_OPTIONS=--require, and serves the heap only in the process whose HTTP server listens on the benchmark's port;
  • hashes every response, with the server's origin (which includes the run's port) replaced, and keeps the last run's bundles and chunk URLs in dist/<variant>/<bundle>/.

The table has two columns: Δ memory (heap the chunk graphs add to the server) and Δ time (all chunk requests). Patched rows show the change, the unpatched → patched means and Welch's t; unpatched rows show mean ± sd. A line under the table says whether every run's bundles are byte-identical to unpatched.

Other changes:

  • benchmark-patches.js awaits measure, so a benchmark can be async.
  • stats and delta move from bundling_speed.js to stats.js, shared by both benchmarks.

Example: #2239 on an internal Meta app

share-transform-results.patch holds #2239's source changes. The app was served by its own dev server command, passed with --server-command, with its app-specific URL params in --query:

node scripts/benchmark-patches/benchmark-patches.js lazy_bundling --runs=5 --chunks=20 --server-command='<app dev server command>' --query='<app params>' <entry>:android
Bundle Variant Δ memory Δ time
Internal Meta App, android (20 chunks) unpatched 546.1 ± 2.7 MB 8.99 ± 0.12 s
Internal Meta App, android (20 chunks) share-transform-results -94.14% (546.1 → 32.0 MB, t -407.8) -53.49% (8.99 → 4.18 s, t -84.3)

Δ memory: heap the chunk graphs add to the server. Δ time: all chunk requests. Unpatched rows show mean ± sample sd; patched rows show the change and the unpatched → patched means. t is Welch's t vs unpatched; at 5 runs, |t| < 2.3 is not significant (p > 0.05).
Every run's bundles are byte-identical to unpatched.

Example: #2239 on Mattermost with 20 screens loaded lazily

mattermost-mobile at 3992834c49d2cbc357af954082834c56cdf1fa0f (React Native 0.83.9), served by this checkout's metro serve (the default --server-command), from the Mattermost checkout:

node <metro>/scripts/benchmark-patches/benchmark-patches.js lazy_bundling --runs=5 --chunks=20 index.tsx:ios
Bundle Variant Δ memory Δ time
Mattermost, ios (20 chunks) unpatched 2419.9 ± 0.0 MB 19.39 ± 0.80 s
Mattermost, ios (20 chunks) share-transform-results -94.94% (2419.9 → 122.4 MB, t -105170.4) -51.17% (19.39 → 9.47 s, t -27.0)

Every run's bundles are byte-identical to unpatched. The 20 chunks are the 20 screens, about 48 MB of dev bundle each.

Mattermost setup

benchmark_lazy_screens.ts imports 20 screens with import(), and index.tsx imports it first, so the main bundle names the screens' bundles before any other async dependency. The screens are also imported statically through the app's routes, so each chunk graph is a screen's transitive closure, most of which the main graph also holds.

Mattermost's @babel/preset-env turns import() into a synchronous require, so Metro never sees an async dependency: exclude: ['transform-dynamic-import'] keeps import(). A fresh FileStore directory keeps transforms cached before that change from being reused.

--- a/babel.config.js
+++ b/babel.config.js
-        ['@babel/preset-env', {targets: {node: 'current'}}],
+        ['@babel/preset-env', {targets: {node: 'current'}, exclude: ['transform-dynamic-import']}],
--- a/index.tsx
+++ b/index.tsx
+import './benchmark_lazy_screens';
 import {RUNNING_E2E} from '@env';
--- a/metro.config.js
+++ b/metro.config.js
 const config = {
+    cacheStores: ({FileStore}) => [
+        new FileStore({root: path.join(require('os').tmpdir(), 'metro-cache-lazy-bundling-benchmark')}),
+    ],
     resolver: {
// benchmark_lazy_screens.ts
export const lazyScreens = [
    () => import('./app/screens/channel'),
    () => import('./app/screens/thread'),
    () => import('./app/screens/global_threads'),
    () => import('./app/screens/user_profile'),
    () => import('./app/screens/edit_profile'),
    () => import('./app/screens/settings'),
    () => import('./app/screens/find_channels'),
    () => import('./app/screens/browse_channels'),
    () => import('./app/screens/channel_info'),
    () => import('./app/screens/create_direct_message'),
    () => import('./app/screens/create_or_edit_channel'),
    () => import('./app/screens/emoji_picker'),
    () => import('./app/screens/gallery'),
    () => import('./app/screens/global_drafts'),
    () => import('./app/screens/invite'),
    () => import('./app/screens/login'),
    () => import('./app/screens/permalink'),
    () => import('./app/screens/pinned_messages'),
    () => import('./app/screens/reactions'),
    () => import('./app/screens/channel_files'),
];

Not covered: watch-mode edit rounds, heap in transform workers, and chunks requested in parallel.

Changelog: [Internal]

Test plan

  • yarn jest scripts/benchmark-patches: 8 passed. The new tests cover target parsing and the bundle URL, extracting async bundle URLs from dependency maps, and the table, including byte identity.
  • yarn flow check, ESLint and Prettier on scripts/benchmark-patches: clean.
  • End to end with the default server command on the integration test fixture, with Share transform results between graphs #2239 as a patch, from packages/metro/src/integration_tests:
    node ../../../../scripts/benchmark-patches/benchmark-patches.js lazy_bundling --runs=2 --chunks=3 import-export/index.js:ios
    
    Both variants were measured, the bundles were byte-identical, and the working copy was clean afterwards.
  • The internal Meta app and Mattermost runs above.

…t time

Adds `lazy_bundling` to `scripts/benchmark-patches`. It starts a dev server, requests an entry's lazy dev bundle and then up to N lazy chunks, and reports the heap the chunk graphs add to the server and the time of the chunk requests vs unpatched, and whether every run's bundles are byte-identical to unpatched.

`benchmark-patches.js` awaits `measure`, so a benchmark can be async, and `stats`/`delta` move to `stats.js`, shared by both benchmarks.
@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 17, 2026
@vzaidman
vzaidman marked this pull request as draft November 17, 2026 12:24
@vzaidman
vzaidman marked this pull request as ready for review November 17, 2026 12:44
@vzaidman
vzaidman requested a review from robhogan November 17, 2026 12:47
@vzaidman
vzaidman requested a review from huntie November 17, 2026 12:48
@github-actions

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: d787a85 · 4 commands run

This adds a lazy_bundling benchmark to scripts/benchmark-patches. For each run it starts a dev server, requests the lazy main bundle and then chunks breadth first, and reads the server's heap through a probe preloaded with --require. It also moves the shared stats helpers to stats.js and makes measure async. The change only touches dev scripts. Its tests, Flow and ESLint pass, and I ran the PR's end-to-end test plan on the integration fixture with a no-op patch: it measured both variants, reported identical bundles, and left the working copy clean.

No findings.

Verified
  • ✅ yarn jest --no-watchman scripts/benchmark-patches — 3 suites, 8 tests
  • ✅ npx eslint scripts/benchmark-patches
  • ✅ yarn flow check — No errors
  • ✅ node ../../../../scripts/benchmark-patches/benchmark-patches.js lazy_bundling --runs=2 --chunks=3 import-export/index.js:ios (from packages/metro/src/integration_tests, with a temporary no-op patch in patches/) — Warmup plus 4 runs completed in about 20s. Both variants were measured, chunk URLs were found in the paths maps, the report said bundles were byte-identical, the server processes were stopped, and git status was clean afterwards.

Not run: The internal Meta app and Mattermost example runs weren't reproduced, because they need app checkouts and long runs. They only illustrate the benchmark, which this PR adds without changing Metro runtime code, so no head-versus-base benchmark applies. Windows is untested: stopping the server relies on process.kill(-pid) process groups, and patches are applied with the POSIX patch tool, so this script is effectively POSIX-only, as the existing bundling_speed already is.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

@vzaidman
vzaidman merged commit 0e74495 into main Nov 17, 2026
17 checks passed
@vzaidman
vzaidman deleted the vzaidman/lazy-bundling-benchmark branch November 17, 2026 15:46
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