Skip to content

ENG-12682 Add opt-in React Compiler support - #7234

Merged
masenf merged 5 commits into
mainfrom
codex/react-compiler-opt-in
Oct 14, 2026
Merged

masenf merged 5 commits into
mainfrom
codex/react-compiler-opt-in

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Add experimental, opt-in React Compiler support for generated components. Existing apps retain the current build and memoization behavior by default.

config = rx.Config(app_name="my_app", react_compiler=True)

REFLEX_REACT_COMPILER=true provides the equivalent environment setting. The compiler runs before React Router in development and production, targeting React 19 with normal inference and correctness bailouts. Scope is limited to generated app/, app_components/, and utils/components/ JavaScript/JSX; runtime state providers, event utilities, dependencies, and custom public assets are excluded.

The flag installs pinned Babel/compiler development dependencies through the existing dependency cache. Disabling it removes the transform and optional dependencies when no longer needed. The Vite plugin prebundles react/compiler-runtime, which is injected after dependency scanning, to prevent first-load HTTP 504 errors.

Validation:

  • Compiler off/on × development/production browser coverage: 40 passed, 2 skipped. Covers handler arguments, controlled inputs, recursive components, keyed row identity, filtered arrays, and hot reload of an optimized component without losing state. The two skips inspect development-only source modules.
  • Unit suite: 9,722 passed, 18 skipped, 1 deselected; 74.02% coverage. The deselected test is test_run_granian_backend_refuses_requests_while_the_app_is_broken: it also fails on clean upstream 7cac2faf9 (recovery assertion there, refusal assertion on this run). The backend implementation and that test are unchanged by this PR.
  • Repository Ruff checks/formatting, Pyright, and every applicable pre-commit hook passed, including stub generation, ty, and Biome formatting.
  • Disabled Vite output matches the previous template byte-for-byte across 32 existing flag combinations.

Runtime gains depend on the workload; list-heavy updates can regress and compilation adds build time/memory. Documentation makes these tradeoffs explicit. This PR preserves existing Reflex memoization and does not include the fixture-specific filter-callback rewrite from the performance experiment.

<source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg">

@greptile-apps

greptile-apps Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no outstanding previous findings or accepted new issues.

Summary

Adds experimental, opt-in React Compiler support for generated frontend components while preserving existing behavior by default.

  • Adds the react_compiler configuration option and environment-variable support.
  • Installs pinned compiler dependencies and injects a pre-router Vite transform for generated component modules.
  • Documents performance tradeoffs and expands unit, development, production, and HMR coverage.
  • The changes made since the previous review introduce no additional code changes, and both previous findings are resolved.

Reviews (7) · Last reviewed commit: "Merge branch 'main' into codex/react-com..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 15 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 148 untouched benchmarks


Comparing codex/react-compiler-opt-in (269734d) with main (dd41524)

Open in CodSpeed

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-22T20:06:20.624073Z 258d66c Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 14 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread tests/units/test_prerequisites.py Resolved
@FarhanAliRaza FarhanAliRaza changed the title Add opt-in React Compiler support ENG-12682 Add opt-in React Compiler support Sep 22, 2026
@linear-code

linear-code Bot commented Sep 22, 2026

Copy link
Copy Markdown

ENG-12682

@masenf masenf added the perf Performance-improving changes label Sep 29, 2026
FarhanAliRaza and others added 4 commits September 30, 2026 23:13
Evaluate the generated vite.config.js with both flags in Node, apply the adapter's config hook with Vite's merge semantics, and check that optimizeDeps keeps the compiler-runtime include and the production-React rolldownOptions.

Claude-Session: https://claude.ai/code/session_01PXcFKAgnzhgdnSvhuCmxWE
The dependency toggle test hardcoded the current pins, and the cache test patched in 1.0.1, which a real pin bump to 1.0.1 would make a no-op. Build both from constants.PackageJson.REACT_COMPILER_DEV_DEPENDENCIES.

Claude-Session: https://claude.ai/code/session_01PXcFKAgnzhgdnSvhuCmxWE
@masenf masenf added the on deck PRs lined up to review / merge next label Oct 5, 2026

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

this would be the perfect thing to implement as a config Plugin... except the framework provides no "nice" mechanism for updating the vite config from a plugin.

implementation is fine for now, but we probably would want to move it into a plugin later, once the necessary interfaces are in place.

Copy link
Copy Markdown
Contributor Author

The integration failures on 269734d come from a timing problem on npm, not from this PR. In run 37386208888, every failing integration-app-harness and -playwright job broke while installing frontend packages:

error: No version matching "1.3.8" found for specifier "@radix-ui/react-popper" (but package exists)

radix-ui@1.7.0 was published at 23:09:03 UTC, but some of the exact @radix-ui/* versions it requires were published up to 23:10:51. For example, @radix-ui/react-popper@1.3.8 appeared at 23:10:34. The installs ran at 23:09:17–20, so bun resolved 1.7.0 before those packages existed. That's why all the jobs failed in the same second. The AssertionErrors in the summaries come from pytest re-running the fixtures that had already broken.

None of the failing fixtures turn on react_compiler. The compiler-on test_memo tests passed in both dev and prod with main's refreshed dependencies.

All of those packages are published now, so "Re-run failed jobs" on run 37386208888 should pass with no code change. I can't trigger re-runs from here (403).

CodSpeed's two test_state_access regressions aren't from this PR either: its diff against main changes no Python state code.


Generated by Claude Code

@masenf masenf added the documentation Improvements or additions to documentation label Oct 6, 2026

FarhanAliRaza commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@masenf agreed. I briefly tried moving this into a plugin with a new Vite-config hook, but the framework doesn't have a settled interface for plugins to update the Vite config yet, so I dropped that attempt. The PR keeps the react_compiler config flag for now, and it can move into a plugin once those interfaces exist.


Generated by Claude Code

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 21 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread tests/units/test_config.py Resolved
@FarhanAliRaza
FarhanAliRaza force-pushed the codex/react-compiler-opt-in branch from 93b2bc6 to 269734d Compare October 12, 2026 17:30
@masenf
masenf merged commit 9a92690 into main Oct 14, 2026
318 of 328 checks passed
@masenf
masenf deleted the codex/react-compiler-opt-in branch October 14, 2026 07:48
FarhanAliRaza added a commit that referenced this pull request Nov 10, 2026
* Let React Compiler optimize generated components

Replays #7272 onto main. That PR auto-closed when its base branch was
deleted after #7234 merged; its own changes were the diff between the
base-branch head it last merged (07aa53c) and its head (4f3e5db),
covering commits c01f4d4, 755ddba and 4f3e5db.

Change the patterns Reflex emits that made React Compiler bail out:
register refs through a useRegisteredRef hook instead of writing to
refs during render, wrap generateUUID in an inline useMemo callback,
list only referenced bindings as useCallback dependencies, disable
function outlining, and describe Emotion's jsx() to the compiler as
JSX. Pin the experimental compiler, which accepts refs passed to jsx().

Add an auto_memoize config option (default True). False removes
MemoizeStatefulPlugin from page compilation so the compiler memoizes
whole pages; app-root memoization is unchanged.

Conflicts with main: #7586 removed hook positions, so the ref hook no
longer passes VarData(position=INTERNAL); #7435 removed
backend_state_mismatch from state.js, so it stays removed.

Claude-Session: https://claude.ai/code/session_01LwJUCTv3FVWzi1UoUpyMLT

* Tie the callback prune pattern to Python names, tidy tests and fragment

Export GENERATED_EVENT_CALLBACK from the React Compiler adapter and test it
against the names get_memoized_event_triggers produces, so a rename on either
side fails a test instead of silently restoring compiler bailouts.

Drop the Unicode stale-callback claim from the reflex-base fragment: main never
had that bug. Move inline test imports to module level.

* Run the auto_memoize compile_app test as a dry run

Without dry_run, REFLEX_SKIP_COMPILE or a leftover .web/nocompile file made
compile_app return before building the page plugins, and the test wrote into
the working directory's .web.

* Derive the client counter test's JS from the var's real identifier (#7741)

test_memo_client_counter hard-coded the JavaScript name a client state
var binds its value to ("Δcounter + 1"). Merging main brought in #6936,
which drops the name of a legacy ClientStateVar created with
global_ref=False and suffixes the emitted identifier with RxClientState,
so the hook now reads `const [cs$0RxClientState, setCs$0] = ...` while
the click handler still read `Δcounter`. That bare identifier resolved
to the button carrying id="Δcounter" through the browser's named
element access, and the counter rendered "[object HTMLButtonElement]1".

Build the increment expression from str(counter.value) instead, with a
{value} placeholder for the direct read and a {source} placeholder for
the eval'd reads, so the test follows whatever identifier Reflex emits.
Use rx.client_state(0) and .set(...) in place of the deprecated
ClientStateVar.create / set_value entry points. rx.client_state rejects
non-ASCII names, so the Unicode case becomes a plain scoped counter;
test_react_compiler_callback_dependencies still covers Unicode bindings
in the Babel dependency prune.


Claude-Session: https://claude.ai/code/session_01ESN8Dfnc4fUrUVcPsqaLGk

Co-authored-by: Claude <noreply@anthropic.com>

* Keep lifecycle events in their branch when auto_memoize is off

Without memo wrappers, on_mount and on_unmount hooks were hoisted to the
page function. A hidden rx.cond branch fired on_mount on page load, and an
rx.foreach item hook read the loop variable outside the loop.

Render the events as a MountEffect element beside the component instead.

* Warn once that react_compiler is experimental

* Render lifecycle events as elements in every compile mode

An on_mount inside rx.foreach was hoisted to the Foreach memo body, where
the loop variable does not exist, so the page crashed. Run
InlineLifecyclePlugin with auto-memoization on as well, and rewrite a
snapshot subtree before the memoize plugin seals it.

* Limit the lifecycle element plugin to the experimental options

Default apps keep the generated code they have on main. The plugin runs
with react_compiler=True or auto_memoize=False.

* Keep scope wrappers when auto_memoize is off; remove MountEffect

The MountEffect element kept one instance across an rx.cond branch switch,
so no lifecycle event fired, and its fragment hid the child from an
as_child parent.

With auto_memoize=False, still wrap a component that has a lifecycle event
or hooks of its own in a memo component. A wrapper has its own identity and
forwards props, and it keeps the hooks in their rx.cond branch.

* Test rx.foreach item lifecycle with auto_memoize off; complete the docstring

- An item's lifecycle hook compiles into a wrapper inside the loop body and
  receives the loop variable when auto_memoize is off.
- default_page_plugins names the snapshot boundary and ALWAYS exceptions.

Claude-Session: https://claude.ai/code/session_01LwJUCVweoEhh1JsZA7emsa

* Keep lifecycle of prop components in a wrapper when auto_memoize is off

With auto_memoize off, a lifecycle event of a component held in a prop moved
into the page function, so it fired on page load even inside a hidden
rx.cond branch. The scope-only rule now uses _has_instance_hooks, which also
checks components held in props.

Claude-Session: https://claude.ai/code/session_01LwJUCVweoEhh1JsZA7emsa

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation on deck PRs lined up to review / merge next perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants