Skip to content

Cache React Compiler transforms across app builds - #3520

Merged
ymichael merged 4 commits into
mainfrom
bb/app-react-compiler-transform-cache-thr_cehj5ti332
Sep 11, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/app-react-compiler-transform-cache-thr_cehj5ti332

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The app ran the Babel-based React Compiler transform again for every module whenever Vite executed, even when the module input and complete compiler environment were unchanged. Turbo could reuse or restore the whole app task, but it could not reuse the expensive intermediate transforms during a forced build or after a single source edit.

What changed

The existing @rolldown/plugin-babel integration and unchanged reactCompilerPreset() are wrapped with a persistent cacache layer. The content-addressed cache is shared through the Git common directory across isolated worktrees, validates stored code and source maps, recovers from corrupt entries, and rejects results containing raw or escaped absolute worktree paths.

Keys cover source code, relative module identity and query, module type, Vite environment, configuration and dependency contents, actual Node/platform/architecture/options, and relevant Babel/Node environment. Development and compiler diagnostic modes continue through the original uncached transform. Turbo inputs and repository documentation describe the cache contract. This does not change compiler optimizations, minification, compression, native validation, runtime policy, CLI/SDK contracts, or the host daemon protocol.

@slopcop review requested.

How you verified

  • pnpm exec turbo run lint typecheck test --filter=@bb/app --output-logs=full --summarize=false: app lint had zero errors; 511 test files and 4,380 tests passed, with 4 skipped.
  • Focused final cache tests passed after adding dependency/toolchain identity and concurrent-write coverage.
  • Post-rebase pnpm exec turbo run lint typecheck --filter=@bb/app, focused app tests, and a forced @bb/app production build passed through Turbo. The build reported 1,067 cache hits and zero misses.
  • Three matched uncached baseline builds took 18.478–21.370 seconds, median 19.094 seconds. Three forced warm builds took 4.455–4.561 seconds, median 4.467 seconds. The single cold-cache sample took 19.640 seconds, adding 0.546 seconds versus the baseline median.
  • A one-source edit caused exactly one miss. A separate detached worktree with independent dependency links reused all 1,067 transforms.
  • All 1,531 production outputs matched the uncached baseline byte for byte, including 442 gzip and 442 Brotli files. Whole-task hits and missing-output restoration were measured separately from intermediate reuse.

AGENT GENERATED

@bb-slop-cop

bb-slop-cop Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

This PR saves React Compiler results between app builds so unchanged files can skip repeated compiler work. I’m SlopCop, and I’m reviewing security, code quality, performance, and opportunities to reuse existing code, with verification of the build behavior where possible.

AGENT GENERATED

await writeFile(configDependency, 'export default "changed";');
const [concurrentA, concurrentB] = await Promise.all([compile(), compile()]);
expect(concurrentA.stats.misses).toBeGreaterThan(0);
expect(concurrentB.stats.misses).toBeGreaterThan(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — [P3] Allow a concurrent build to reuse the first build’s result

Both builds must currently record a miss, but Promise.all does not guarantee that both cache reads happen before either write. A delayed build can legitimately hit the entry produced by the other, causing this test to fail despite correct behavior. Consider asserting that the combined miss count is positive while retaining output equality and the subsequent fully warm build check; if the intent is specifically to prove overlapping writes, synchronize the initial misses explicitly.

AGENT GENERATED

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 SLOP COP 🚨 · review

This PR remembers React Compiler results so unchanged app files can skip expensive work on later builds, including builds from another worktree of the same repository.

The parallel security, code-quality, and performance reviews found no blocking product issue. The wrapper preserves the existing compiler integration, validates cached output, and recompiles when entries are missing or corrupt. The repository scan found no existing transform-cache implementation worth extracting or reusing here.

One small test reliability finding is posted inline: requiring both concurrent builds to miss assumes a particular scheduling order. A correctly functioning cache can let the second build reuse the first build’s result.

I independently ran the four cache tests through Turbo; all passed. App lint and typecheck also passed, with 191 lint warnings and zero errors. I did not independently repeat the full-app timing/output benchmarks reported in the PR. No browser end-to-end smoke was run: the requested doobie CLI is unavailable here, and development mode bypasses this cache; the focused tests exercised actual Vite production builds.

AGENT GENERATED

@ymichael
ymichael force-pushed the bb/app-react-compiler-transform-cache-thr_cehj5ti332 branch from f50802c to 03c557d Compare September 11, 2026 20:45
@ymichael
ymichael merged commit b72b1f7 into main Sep 11, 2026
16 checks passed
@ymichael
ymichael deleted the bb/app-react-compiler-transform-cache-thr_cehj5ti332 branch September 11, 2026 21:03
ymichael pushed a commit that referenced this pull request Nov 30, 2026
## Human comments

## What was wrong

The React Compiler transform cache from #3520 ran only for `vite build`.
`cachedReactCompiler()` returned from `configResolved` before it set up
the cache whenever `config.command !== "build"`, so the dev server
compiled every module again on each start. `testReactCompiler()`, which
`apps/app/vitest.config.ts` uses since #7603, returned a plain Babel
plugin with no cache. On app shard 3/8 the Babel pass was 80-91 s of
Vitest transform time in a 113-131 s run. I measured the shard and a dev
server start before changing anything (numbers below). #3520 gives no
reason for the build-only limit, and the cache key already separates
build, dev and test: Vite mode, `isProduction`, Vite environment, the
content of each config file dependency (`vite.config.ts` and
`vitest.config.ts` differ), `NODE_ENV`, `BABEL_*`, and the Node version
and flags.

## What changed

- `apps/app/vite-react-compiler.ts`: the cache wrapper takes the Babel
options, and both `cachedReactCompiler()` and `testReactCompiler()` go
through it. The `command !== "build"` early return is gone;
`ENABLE_REACT_COMPILER_TIMINGS=1` and `BABEL_SHOW_CONFIG_FOR` still
bypass the cache. The "React Compiler cache: N hits, M misses" line
still prints for builds only, so test and dev output do not change.
- Docs: `docs/configuration.md` and `docs/debugging-and-qa.md` say that
builds, the dev server and tests use the cache. `docs/ci-performance.md`
records the shard measurement and says that CI app test shards do not
restore or save the cache.

The cache still has no size limit, as for builds today; a dev server
edit adds one entry for the edited module.

No wire, CLI, protocol or CI workflow changes.
`packages/shared-ui/vitest.config.ts` keeps its own inline preset; its
suite is 65 tests.

A cached result cannot change a test or dev result, only skip a Babel
run. The key covers the source, the module id, the module type, the Vite
environment and the compiler namespace above. A result that holds a
worktree path, does not parse, or is corrupt on disk falls back to a
fresh compile, as it does for builds today.

## How you verified

New test in `apps/app/src/test/react-compiler-cache.test.ts`: "reuses
dev server compiler output across servers" (`cachedReactCompiler`) and
"reuses test compiler output across servers" (`testReactCompiler`). Each
starts a Vite dev server twice on the same cache directory, transforms
one component, and expects 1 miss and then 1 hit with identical code.

Before the fix (origin/main `vite-react-compiler.ts`):

```
 FAIL  src/test/react-compiler-cache.test.ts > reuses dev server compiler output across servers
AssertionError: expected { hits: +0, misses: +0 } to deeply equal { hits: +0, misses: 1 }
 FAIL  src/test/react-compiler-cache.test.ts > reuses test compiler output across servers
TypeError: Cannot read properties of undefined (reading 'compilerCache')
      Tests  2 failed | 4 passed (6)
```

After the fix: `Tests  6 passed (6)`.

App shard 3/8, run like the Linux CI shard (`BB_CI_TEST_SHARD=3/8
VITEST_MAX_WORKERS=2`, pinned to four CPUs with `taskset`), Node 26, two
alternating rounds:

| Run | Round 1 wall | Round 2 wall | Vitest transform |
|---|---|---|---|
| origin/main | 131.2 s | 113.2 s | 90.8 s, 80.3 s |
| Fix, empty cache | 113.1 s | 98.5 s | 90.2 s, 70.9 s |
| Fix, warm cache | 64.5 s | 62.9 s | 14.7 s, 14.4 s |

All six runs gave the same 701 test results (file, full name, status),
compared from the Vitest JSON reports: 698 passed, 3 skipped. The
shard's cache directory holds 43 MB.

CI starts each app shard with an empty cache, so the "Fix, empty cache"
row is the CI case: 18.1 s and 14.7 s less per shard (14% and 13%),
because the shared and isolated Vitest projects transform the same
modules in one run and the second project reuses the first one's output.
The "warm cache" row is what a local rerun gets: 66.7 s and 50.3 s less
per shard (51% and 44%).

Dev server, created from `apps/app/vite.dev.config.ts`, transforming
`src/main.tsx`, `AppLayout.tsx`, `ThreadDetailView.tsx` and
`TimelineWindowedItems.tsx` with their pre-transformed imports, two
server starts each:

| Build | First start | Second start | Cache |
|---|---|---|---|
| origin/main | 15.7 s | 14.1 s | 0 hits, 0 misses |
| Fix | 15.3 s | 2.1 s | 1/807, then 794/11 hits/misses |

I did not run CI, so the CI saving is not measured. The tables above are
local measurements on one machine.

## CI option left out

App test shards could restore and save the compiler cache in CI with a
per-shard key (`cache-react-compiler` with a `cache-prefix:
test-<shard>-` input, for the 8 Linux and 4 Windows app shards). That
would give CI the warm-cache row: 48.6 s and 35.6 s less per Linux shard
than the empty-cache row, measured locally. This PR does not do it
because of the cost: each run that tests the app would save 12 new
entries of about 43 MB (about 0.5 GB), keyed on the commit, against the
10 GB Actions cache quota that the Turbo caches also use (GitHub-visible
caches held 7.4 GB on 2026-11-26). `scripts/cleanup-actions-caches.mjs`
deletes only pnpm store entries, not react-compiler entries, so the new
entries could evict Turbo caches, which save more. A maintainer can ask
for this option in a comment.

Checks:

- `pnpm exec turbo run typecheck test lint lint:type-aware --continue
--filter=@bb/app`: all 10 tasks pass; 705 test files, 7485 passed, 20
skipped (run with `LANG=en_US.UTF-8`, because
`UpdatePluginDialog.test.tsx` formats dates by the system locale); lint
and lint:type-aware report 0 warnings and 0 errors

Fixes #8226

> AGENT GENERATED
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…bb#8227)

## Human comments

## What was wrong

The React Compiler transform cache from get-bb#3520 ran only for `vite build`.
`cachedReactCompiler()` returned from `configResolved` before it set up
the cache whenever `config.command !== "build"`, so the dev server
compiled every module again on each start. `testReactCompiler()`, which
`apps/app/vitest.config.ts` uses since get-bb#7603, returned a plain Babel
plugin with no cache. On app shard 3/8 the Babel pass was 80-91 s of
Vitest transform time in a 113-131 s run. I measured the shard and a dev
server start before changing anything (numbers below). get-bb#3520 gives no
reason for the build-only limit, and the cache key already separates
build, dev and test: Vite mode, `isProduction`, Vite environment, the
content of each config file dependency (`vite.config.ts` and
`vitest.config.ts` differ), `NODE_ENV`, `BABEL_*`, and the Node version
and flags.

## What changed

- `apps/app/vite-react-compiler.ts`: the cache wrapper takes the Babel
options, and both `cachedReactCompiler()` and `testReactCompiler()` go
through it. The `command !== "build"` early return is gone;
`ENABLE_REACT_COMPILER_TIMINGS=1` and `BABEL_SHOW_CONFIG_FOR` still
bypass the cache. The "React Compiler cache: N hits, M misses" line
still prints for builds only, so test and dev output do not change.
- Docs: `docs/configuration.md` and `docs/debugging-and-qa.md` say that
builds, the dev server and tests use the cache. `docs/ci-performance.md`
records the shard measurement and says that CI app test shards do not
restore or save the cache.

The cache still has no size limit, as for builds today; a dev server
edit adds one entry for the edited module.

No wire, CLI, protocol or CI workflow changes.
`packages/shared-ui/vitest.config.ts` keeps its own inline preset; its
suite is 65 tests.

A cached result cannot change a test or dev result, only skip a Babel
run. The key covers the source, the module id, the module type, the Vite
environment and the compiler namespace above. A result that holds a
worktree path, does not parse, or is corrupt on disk falls back to a
fresh compile, as it does for builds today.

## How you verified

New test in `apps/app/src/test/react-compiler-cache.test.ts`: "reuses
dev server compiler output across servers" (`cachedReactCompiler`) and
"reuses test compiler output across servers" (`testReactCompiler`). Each
starts a Vite dev server twice on the same cache directory, transforms
one component, and expects 1 miss and then 1 hit with identical code.

Before the fix (origin/main `vite-react-compiler.ts`):

```
 FAIL  src/test/react-compiler-cache.test.ts > reuses dev server compiler output across servers
AssertionError: expected { hits: +0, misses: +0 } to deeply equal { hits: +0, misses: 1 }
 FAIL  src/test/react-compiler-cache.test.ts > reuses test compiler output across servers
TypeError: Cannot read properties of undefined (reading 'compilerCache')
      Tests  2 failed | 4 passed (6)
```

After the fix: `Tests  6 passed (6)`.

App shard 3/8, run like the Linux CI shard (`BB_CI_TEST_SHARD=3/8
VITEST_MAX_WORKERS=2`, pinned to four CPUs with `taskset`), Node 26, two
alternating rounds:

| Run | Round 1 wall | Round 2 wall | Vitest transform |
|---|---|---|---|
| origin/main | 131.2 s | 113.2 s | 90.8 s, 80.3 s |
| Fix, empty cache | 113.1 s | 98.5 s | 90.2 s, 70.9 s |
| Fix, warm cache | 64.5 s | 62.9 s | 14.7 s, 14.4 s |

All six runs gave the same 701 test results (file, full name, status),
compared from the Vitest JSON reports: 698 passed, 3 skipped. The
shard's cache directory holds 43 MB.

CI starts each app shard with an empty cache, so the "Fix, empty cache"
row is the CI case: 18.1 s and 14.7 s less per shard (14% and 13%),
because the shared and isolated Vitest projects transform the same
modules in one run and the second project reuses the first one's output.
The "warm cache" row is what a local rerun gets: 66.7 s and 50.3 s less
per shard (51% and 44%).

Dev server, created from `apps/app/vite.dev.config.ts`, transforming
`src/main.tsx`, `AppLayout.tsx`, `ThreadDetailView.tsx` and
`TimelineWindowedItems.tsx` with their pre-transformed imports, two
server starts each:

| Build | First start | Second start | Cache |
|---|---|---|---|
| origin/main | 15.7 s | 14.1 s | 0 hits, 0 misses |
| Fix | 15.3 s | 2.1 s | 1/807, then 794/11 hits/misses |

I did not run CI, so the CI saving is not measured. The tables above are
local measurements on one machine.

## CI option left out

App test shards could restore and save the compiler cache in CI with a
per-shard key (`cache-react-compiler` with a `cache-prefix:
test-<shard>-` input, for the 8 Linux and 4 Windows app shards). That
would give CI the warm-cache row: 48.6 s and 35.6 s less per Linux shard
than the empty-cache row, measured locally. This PR does not do it
because of the cost: each run that tests the app would save 12 new
entries of about 43 MB (about 0.5 GB), keyed on the commit, against the
10 GB Actions cache quota that the Turbo caches also use (GitHub-visible
caches held 7.4 GB on 2026-11-26). `scripts/cleanup-actions-caches.mjs`
deletes only pnpm store entries, not react-compiler entries, so the new
entries could evict Turbo caches, which save more. A maintainer can ask
for this option in a comment.

Checks:

- `pnpm exec turbo run typecheck test lint lint:type-aware --continue
--filter=@bb/app`: all 10 tasks pass; 705 test files, 7485 passed, 20
skipped (run with `LANG=en_US.UTF-8`, because
`UpdatePluginDialog.test.tsx` formats dates by the system locale); lint
and lint:type-aware report 0 warnings and 0 errors

Fixes get-bb#8226

> AGENT GENERATED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant