The React AOT Compiler
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- AGENTS.md
- packages/farm-react/package.json
- packages/farm-react/src/tests/compiler-edge-cases.test.ts
- examples/react-compiler/README.md
- packages/farm-react/src/tests/compiler-component-islands.test.ts
- packages/farm-react/src/tests/compiler-mixed-ranges.test.ts
- packages/farm-react/src/tests/compiler-keyed-ranges.test.ts
The React AOT (Ahead-of-Time) Compiler is an experimental optimization tool in Farm.js that converts eligible React components into direct DOM updates. It bypasses standard React reconciliation for local state changes while maintaining a safe fallback to standard React behavior for unsupported component structures. By moving state-to-DOM binding analysis to the build phase, the compiler reduces CPU overhead and component execution counts during runtime updates.
Sources: README.md:46, examples/react-compiler/README.md:4, examples/react-compiler/README.md:120
Core Objectives and Architecture
The compiler implements a hybrid reactivity model. React retains ownership of the initial render, hydration, events, and lifecycle boundaries, while the compiler manages high-frequency local updates through precomputed bindings.
- Trace Dependencies: The build process identifies which
useStatecells control specific text nodes or DOM attributes. - Direct Patching: Local updates apply changes only to identified targets, skipping the component body execution and virtual DOM diffing.
- Safe Fallback: Any component containing unproven structures, such as
refs,effects, or complex dynamic expressions, automatically uses the standard React rendering path.
Sources: examples/react-compiler/README.md:8, examples/react-compiler/README.md:124, examples/react-compiler/README.md:129
Compilation Workflow
The following diagram illustrates how the compiler identifies and handles components during the build and runtime phases:
The compiler analyzes components during the build phase to generate direct DOM bindings for runtime state updates. Sources: examples/react-compiler/README.md:8, examples/react-compiler/README.md:124
Boundary and Safety Rules
The compiler enforces strict boundaries to ensure application correctness. It falls back to React reconciliation when it cannot prove the safety of an optimization.
Supported Features
- Local State: Updates via
useStatethat affect text, classes, data attributes, and specific style properties. - Keyed Maps: Dedicated host containers like
<ul>{items.map(...)}</ul>that use stable keys. - Conditional Blocks: Logical (
&&) and ternary (? :) operators that control host-only branches. - Static Siblings: Stable DOM elements that remain constant across updates.
Unsupported Fallbacks (React-Owned)
- Hooks and Lifecycle:
useEffect,useRef, anduseMemocalls within the component body. - Non-Inline Handlers: Event handlers that are not defined inline or that read dynamic derived values.
- Spread Attributes: Objects spread into JSX props (e.g.,
<div {...props} />). - Complex Components: Custom React components used as branches in a conditional or rows in a list.
Sources: packages/farm-react/src/tests/compiler-edge-cases.test.ts:161-228, examples/react-compiler/README.md:162-180
Performance and Benchmarks
The compiler significantly reduces update latency by eliminating redundant rendering work. Reference benchmarks on Apple M1 hardware show major improvements in keyed list operations and local state updates.
| Metric | Base React | Compiler AOT | Improvement |
|---|---|---|---|
| Keyed Swaps | Baseline | 6.8x Faster | +580% |
| CPU Usage | Baseline | 2x Less | -50% |
| Update Latency (Median) | 0.175 ms | 0.015 ms | 91.4% Reduction |
| Component Executions | 1 per update | 0 per update | 100% Reduction |
Sources: README.md:46, examples/react-compiler/README.md:126, examples/react-compiler/README.md:130
Configuration and Usage
The compiler is enabled through the React renderer configuration in farm.config.ts. It considers all components by default but allows for explicit opt-outs.
- Global Enablement: Set
experimental.compiler: truein the React renderer options. - Manual Opt-out: Use the
"use no compiler"directive at the top of a component function to force standard React rendering. - Environment Override: The variable
FARM_REACT_COMPILER=falsecan be used to disable the compiler during build time for comparison purposes.
Sources: packages/create-farm-app/templates/react-compiler/README.md:19-35, packages/farm-react/src/tests/compiler-edge-cases.test.ts:316-324
Keyed Range and List Optimization
The compiler implements a specialized runtime for keyed collections. It uses a Longest Increasing Subsequence (LIS) algorithm to minimize DOM moves during list reordering.
- Map Analysis: The compiler traces state dependencies through inline
filter,sort,slice, andmapoperations. - Snapshot Scoping: Farm scopes a prepared branch snapshot to each row key.
- LIS Reordering: During reorders, the runtime identifies rows already in valid relative positions to avoid unnecessary DOM insertions or removals.
- Keyed List Fallback: If duplicate keys are discovered at runtime, the container remounts through standard React to ensure consistency.
Sources: examples/react-compiler/README.md:144-158, packages/farm-react/src/tests/compiler-keyed-ranges.test.ts:1-20
The AOT runtime coordinates with React Fiber when structural changes occur, but handles local patches independently. Sources: examples/react-compiler/README.md:158, examples/react-compiler/README.md:180
The React AOT Compiler provides a significant performance boost for products with high-frequency local interactions or large keyed lists. Its design prioritizes safety by falling back to standard React semantics whenever optimization eligibility cannot be mathematically proven during the build.