Skip to content

Cache React Compiler output in app tests and the app dev server - #8227

Merged
ymichael merged 1 commit into
get-bb:mainfrom
lanjak:perf-test-react-compiler-cache
Nov 30, 2026
Merged

ymichael merged 1 commit into
get-bb:mainfrom
lanjak:perf-test-react-compiler-cache

Conversation

@lanjak

@lanjak lanjak commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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

The compiler transform cache ran only for vite build. cachedReactCompiler
skipped it for every other command, and testReactCompiler returned an
uncached Babel plugin, so each app test run and each dev server start
compiled every module again. On app shard 3/8 that was 80-91 s of
transform time in a 113-131 s run.

Route both presets through the cache wrapper and remove the build-only
return. The timing and BABEL_SHOW_CONFIG_FOR bypasses stay, and the hit
count still prints for builds only.
@ymichael
ymichael merged commit 1dc151b into get-bb:main Nov 30, 2026
50 checks passed
ymichael added a commit that referenced this pull request Nov 30, 2026
## Human comments

## What was wrong

The React Compiler transform cache at
`<git-common-dir>/bb-cache/react-compiler` never removes anything.
Builds alone had grown it to 4.1 GB (702k index files, 2.7 GB of which
is index) since Sep 10. #8227 now also caches app tests and the app dev
server. A full app test run adds about 38 MB whenever the lockfile, a
`package.json` or a Vite/Vitest config changes (38 commits on main
touched one in the last 30 days), and the dev server adds a similar set
of entries. Deleting the directory once only resets the clock for that
one clone.

## What changed

- `apps/app/react-compiler-cache-prune.ts` (new):
- `pruneCompilerCache` deletes index buckets whose newest entry is older
than 30 days.
- It then deletes stored content that no remaining entry references and
that is itself older than 30 days, so content a concurrent writer has
just stored, before it appends the index entry, is kept. One race
remains: cacache does not rewrite content that already exists, so a new
entry whose output is byte-identical to old unreferenced content can
lose that content to a prune that already scanned the index. That read,
like any read racing a deletion, fails and falls back to a fresh compile
that stores the content again, the same path a corrupt entry takes
today.
- Files that disappear mid-run (a second prune, or `cacache.rm.content`
on a corrupt entry) are skipped instead of aborting the run.
- Run as a script (Node ≥22.19 strips the types), it prunes the
directory given as its argument.
- `schedulePrune` checks a `pruned-at` marker in the cache directory.
When the marker is more than a day old, it touches the marker and starts
the script as a detached, unref'd process, so no build, test run or dev
server waits on it.
- `apps/app/vite-react-compiler.ts` calls `schedulePrune` once the cache
is set up in `configResolved`. Failures only log a warning.
- `docs/configuration.md` and `docs/debugging-and-qa.md` describe the
pruning.

Entries are dated by when they were written, so a cache hit does not
refresh an entry. A module unchanged for more than 30 days recompiles
once after a prune. `cacache.verify` was not used: it loads the whole
index into memory, re-checksums all content and clears `tmp/` while
other worktrees may be writing.

CI is effectively unaffected. Its cache key includes the lockfile and
`apps/app/vite*.ts` hashes, so it starts over whenever those change, and
the prune finds little to delete there.

## How you verified

- New `apps/app/src/test/react-compiler-cache-prune.test.ts`:
- The first test runs the real script in a child process over expired,
shared and recent entries. It checks that expired entries and their
unreferenced content are removed, and that content a recent entry still
uses survives. Breaking the liveness check makes it fail with `ENOENT`
on the shared content. It also stores content with no index entry and
checks that it survives; dropping the 30-day content age check makes
that fail.
  - The second test checks that pruning is scheduled at most once a day.
- On a real shared cache, the first prune took it from 4.1 GB to 1.4 GB:
index files from 702k to 234k, content files from 22k to 9.7k. It took
about 15 minutes, with a large copy competing for the disk. A
steady-state run with nothing to delete took 3 min 54 s of wall time and
59 s of CPU.
- A backdated marker made `pnpm exec turbo run build --filter=@bb/app
--force` update the marker and leave a detached prune running after the
build exited. The build itself reported `React Compiler cache: 1476
hits, 0 misses`. The Vitest config path also schedules it.
- `pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/app`
passes; the 8 React Compiler cache tests pass.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 30, 2026
…ests (#8420)

## Human comments

## What was wrong

The two cross-server React Compiler cache tests (from #8227) compared
the full `server.transformRequest` result, which includes the rewrites
from Vite's import analysis. That step resolves a module's imports in
parallel inside `Promise.all` (`vite/dist/node/chunks/node.js`,
`interopNamedImports`). For each CJS-interop dependency it calls
`str.appendLeft(fileStart, hoistedAssignments)` when that dependency's
resolution finishes. On Windows the `react/compiler-runtime` and
`react/jsx-dev-runtime` resolutions sometimes finish in a different
order, so the prepended lines come out swapped. The cached compiler
output is the same in both runs. From the failed Windows (app-4) job:

```
- ...component.tsx");const _jsxDEV = __vite__cjsImport1_react_jsxDevRuntime["jsxDEV"];const _c = __vite__cjsImport0_react_compilerRuntime["c"];import ...
+ ...component.tsx");const _c = __vite__cjsImport0_react_compilerRuntime["c"];const _jsxDEV = __vite__cjsImport1_react_jsxDevRuntime["jsxDEV"];import ...
```

The two lines just swap places. Their import indices stay the same, so
this is ordering in Vite, not a cache or path-separator problem.

## What changed

`apps/app/src/test/react-compiler-cache.test.ts`: the cross-server tests
add a `pre` probe plugin right after the compiler plugin. The probe
records the code the compiler (or the cache) returned for the input
module, and the tests compare that code instead of Vite's final output.
The probe matches on `normalizePath(input)` so it also works with
Windows ids. Production code is unchanged.

## How you verified

- `pnpm exec turbo run test --filter=@bb/app --
src/test/react-compiler-cache.test.ts`: 6/6 pass.
- To check the probe still catches cache bugs, I temporarily made cache
hits return `code + ';'`. Both cross-server tests failed, and passed
again once the change was reverted.
- `pnpm exec turbo run typecheck --filter=@bb/app` passes.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
## Human comments

## What was wrong

The React Compiler transform cache at
`<git-common-dir>/bb-cache/react-compiler` never removes anything.
Builds alone had grown it to 4.1 GB (702k index files, 2.7 GB of which
is index) since Sep 10. get-bb#8227 now also caches app tests and the app dev
server. A full app test run adds about 38 MB whenever the lockfile, a
`package.json` or a Vite/Vitest config changes (38 commits on main
touched one in the last 30 days), and the dev server adds a similar set
of entries. Deleting the directory once only resets the clock for that
one clone.

## What changed

- `apps/app/react-compiler-cache-prune.ts` (new):
- `pruneCompilerCache` deletes index buckets whose newest entry is older
than 30 days.
- It then deletes stored content that no remaining entry references and
that is itself older than 30 days, so content a concurrent writer has
just stored, before it appends the index entry, is kept. One race
remains: cacache does not rewrite content that already exists, so a new
entry whose output is byte-identical to old unreferenced content can
lose that content to a prune that already scanned the index. That read,
like any read racing a deletion, fails and falls back to a fresh compile
that stores the content again, the same path a corrupt entry takes
today.
- Files that disappear mid-run (a second prune, or `cacache.rm.content`
on a corrupt entry) are skipped instead of aborting the run.
- Run as a script (Node ≥22.19 strips the types), it prunes the
directory given as its argument.
- `schedulePrune` checks a `pruned-at` marker in the cache directory.
When the marker is more than a day old, it touches the marker and starts
the script as a detached, unref'd process, so no build, test run or dev
server waits on it.
- `apps/app/vite-react-compiler.ts` calls `schedulePrune` once the cache
is set up in `configResolved`. Failures only log a warning.
- `docs/configuration.md` and `docs/debugging-and-qa.md` describe the
pruning.

Entries are dated by when they were written, so a cache hit does not
refresh an entry. A module unchanged for more than 30 days recompiles
once after a prune. `cacache.verify` was not used: it loads the whole
index into memory, re-checksums all content and clears `tmp/` while
other worktrees may be writing.

CI is effectively unaffected. Its cache key includes the lockfile and
`apps/app/vite*.ts` hashes, so it starts over whenever those change, and
the prune finds little to delete there.

## How you verified

- New `apps/app/src/test/react-compiler-cache-prune.test.ts`:
- The first test runs the real script in a child process over expired,
shared and recent entries. It checks that expired entries and their
unreferenced content are removed, and that content a recent entry still
uses survives. Breaking the liveness check makes it fail with `ENOENT`
on the shared content. It also stores content with no index entry and
checks that it survives; dropping the 30-day content age check makes
that fail.
  - The second test checks that pruning is scheduled at most once a day.
- On a real shared cache, the first prune took it from 4.1 GB to 1.4 GB:
index files from 702k to 234k, content files from 22k to 9.7k. It took
about 15 minutes, with a large copy competing for the disk. A
steady-state run with nothing to delete took 3 min 54 s of wall time and
59 s of CPU.
- A backdated marker made `pnpm exec turbo run build --filter=@bb/app
--force` update the marker and leave a detached prune running after the
build exited. The build itself reported `React Compiler cache: 1476
hits, 0 misses`. The Vitest config path also schedules it.
- `pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/app`
passes; the 8 React Compiler cache tests pass.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…ests (get-bb#8420)

## Human comments

## What was wrong

The two cross-server React Compiler cache tests (from get-bb#8227) compared
the full `server.transformRequest` result, which includes the rewrites
from Vite's import analysis. That step resolves a module's imports in
parallel inside `Promise.all` (`vite/dist/node/chunks/node.js`,
`interopNamedImports`). For each CJS-interop dependency it calls
`str.appendLeft(fileStart, hoistedAssignments)` when that dependency's
resolution finishes. On Windows the `react/compiler-runtime` and
`react/jsx-dev-runtime` resolutions sometimes finish in a different
order, so the prepended lines come out swapped. The cached compiler
output is the same in both runs. From the failed Windows (app-4) job:

```
- ...component.tsx");const _jsxDEV = __vite__cjsImport1_react_jsxDevRuntime["jsxDEV"];const _c = __vite__cjsImport0_react_compilerRuntime["c"];import ...
+ ...component.tsx");const _c = __vite__cjsImport0_react_compilerRuntime["c"];const _jsxDEV = __vite__cjsImport1_react_jsxDevRuntime["jsxDEV"];import ...
```

The two lines just swap places. Their import indices stay the same, so
this is ordering in Vite, not a cache or path-separator problem.

## What changed

`apps/app/src/test/react-compiler-cache.test.ts`: the cross-server tests
add a `pre` probe plugin right after the compiler plugin. The probe
records the code the compiler (or the cache) returned for the input
module, and the tests compare that code instead of Vite's final output.
The probe matches on `normalizePath(input)` so it also works with
Windows ids. Production code is unchanged.

## How you verified

- `pnpm exec turbo run test --filter=@bb/app --
src/test/react-compiler-cache.test.ts`: 6/6 pass.
- To check the probe still catches cache bugs, I temporarily made cache
hits return `code + ';'`. Both cross-server tests failed, and passed
again once the change was reverted.
- `pnpm exec turbo run typecheck --filter=@bb/app` passes.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

App tests and the app dev server run the React Compiler with no transform cache

2 participants