Repository navigation
Add lazy_bundling benchmark for dev server heap and lazy chunk request time - #2242
Conversation
…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.
Metro review: Looks goodRisk: low · Commit: This adds a No findings. Verified
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 Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call. |
Summary
Adds
lazy_bundlingtoscripts/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_speedcan't see them, because a cold build of one bundle builds one graph. For example, #2239 makes no measurable difference tobundling_speedon 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:--server-command(default: this checkout'smetro serve), passing--portwith a free port;dev=true&lazy=true, plus--query), then up to--chunkslazy chunks one at a time, taking the async bundle URLs from the bundles' dependency maps breadth first, as an app loads them;heapUsedafter two forced GCs) after the main bundle and after the chunks, throughheap-probe.js. The probe is preloaded into the server's Node.js processes withNODE_OPTIONS=--require, and serves the heap only in the process whose HTTP server listens on the benchmark's port;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.jsawaitsmeasure, so a benchmark can be async.statsanddeltamove frombundling_speed.jstostats.js, shared by both benchmarks.Example: #2239 on an internal Meta app
share-transform-results.patchholds #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:Δ 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: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.tsimports 20 screens withimport(), andindex.tsximports 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-envturnsimport()into a synchronousrequire, so Metro never sees an async dependency:exclude: ['transform-dynamic-import']keepsimport(). A freshFileStoredirectory keeps transforms cached before that change from being reused.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 onscripts/benchmark-patches: clean.packages/metro/src/integration_tests: