Unverified Commit e0941e65 authored by Marcos Hernanz's avatar Marcos Hernanz Committed by GitHub

[turbopack] Reduce native React Compiler work (#96820)

## What?

- Use the released `swc_ecma_react_compiler::fast_check::is_required`
API to skip React Compiler work for modules that cannot change in native
`infer` mode.
- Keep explicit `annotation` and `all` modes unconditional.
- Delete Next.js's duplicate React Compiler predicates and use the same
upstream check from the native N-API binding.
- Fail open from the N-API check on unreadable files, fatal parse
failures, and recovered parser errors.
- Keep the client-runtime-only compiler out of App SSR, matching the
existing Babel integration.
- Move the workspace to the coherent SWC 76 dependency family, including
the official `mdxjs-rs-turbopack` branch, with no duplicate SWC 75
stack.
- Keep the React Compiler dependency/module native-only; the WASM facade
already deliberately fails open.

This consumes the implementation landed in
[swc-project/swc#12105](https://github.com/swc-project/swc/pull/12105),
released in `swc_ecma_react_compiler` 23.0.0. Its contract is
conservative: false positives add compiler work, while a negative result
guarantees that compilation cannot alter the program.

## Why?

The native compiler currently runs for every parsed module in infer mode
and is configured for both browser and App SSR contexts. This adds
avoidable AST conversion/compiler work and compiles hydrated client
modules in a server context that the existing Turbopack Babel path
excludes.

This matters for large applications such as v0. Their legacy Babel React
Compiler path creates a separate machine-sized Node worker pool in
addition to PostCSS. Moving to the native path removes that pool; this
patch makes the native path cheaper while preserving the established
browser-only scope.

## Performance

### Upstream fast check on the v0 corpus

The final SWC predicate was checked against 1,816 real v0 modules (10.46
MB):

- selected 302 modules
- retained all 257 modules whose output actually changed
- **zero false negatives**
- scanned the full corpus in 20.624 ms
- reduced the measured full compiler pipeline from 2,371.956 ms to
1,906.180 ms: **-19.64%**

### This Next.js patch only

Real v0 homepage, cold `.next`, Chromium interaction gate, 16 vCPU / 32
GB, exact Next canary `79168556`, exact v0 commit `1ab042a47c`, three
samples per arm. Both arms use the native compiler.

| Median | Unpatched native | This PR | Change |
|---|---:|---:|---:|
| Visible interactive UI | 50.737 s | 48.574 s | **-4.26%** |
| Network quiet | 70.125 s | 67.546 s | **-3.68%** |
| Aggregate CPU | 450 s | 418 s | **-7.11%** |
| Peak process-tree RSS | 7,904,772 KB | 7,823,008 KB | -1.03% |

All three paired visible, quiet, and CPU deltas favored the patch. The
RSS difference is inside noise, so this PR makes no patch-only memory
claim.

### v0 legacy Babel path to native + this PR

Both arms use `TURBO_ENGINE_EVICT_AFTER_SNAPSHOT=1` and remain open for
120 seconds after network quiet.

| Median | Legacy Babel | Native + this PR | Change |
|---|---:|---:|---:|
| Visible interactive UI | 52.411 s | 48.372 s | **-7.71%** |
| Peak process-tree RSS | 12.19 GiB | 7.39 GiB | **-39.40%** |
| RSS at +120 s | 5.31 GiB | 3.02 GiB | **-43.07%** |
| Aggregate CPU | 480 s | 429 s | **-10.63%** |
| Processes at +120 s | 16 | 4 | **-75.00%** |

The large memory/process delta comes from switching v0 off the external
Babel pool, not from this source diff alone. v0 still needs to enable
the native compiler configuration separately.

Every accepted browser run required HTTP 200, a visible nonblank UI with
at least ten controls, zero Next overlays/page errors, and a verified
prompt interaction. The complete Next process session was sampled every
100 ms.

## Validation

- `cargo fmt --all -- --check`
- affected Rust `cargo check`
- affected Clippy with warnings denied, including Turbopack tests
- React Compiler unit tests: 2 passed
- React Compiler integration tests: 3 passed
- Turbopack snapshots: 114 passed
- direct native N-API/Node smoke: wrapped JSX and member hooks select;
plain modules skip; recovered parse errors and missing files fail open
- dependency trees: native includes `swc_ecma_react_compiler`; WASI
excludes it; no `swc_core` 75 remains
- official `mdxjs-rs-turbopack` SWC 76 bump: 188 tests passed
- final GPT-5.6 Sol + Claude Opus 5 xhigh autoreview: clean, 0.98
confidence from both reviewers
- real-v0 cold browser matrix, 120-second memory soak, and full
production build: 44 Workflow steps / 10 workflows, 2,722/2,722
prerenders

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
parent 705cb061
This diff is collapsed.
......@@ -209,30 +209,30 @@ turbopack-trace-server = { path = "turbopack/crates/turbopack-trace-server" }
turbopack-trace-utils = { path = "turbopack/crates/turbopack-trace-utils" }
turbopack-wasm = { path = "turbopack/crates/turbopack-wasm" }
swc_ecma_react_compiler = "22.0.0"
swc_ecma_react_compiler = "23.0.0"
react_compiler = { version = "0.2", package = "forked_react_compiler" }
# SWC crates
swc_core = { version = "75.0.0", default-features = false, features = [
swc_core = { version = "76.0.0", default-features = false, features = [
"ecma_loader_lru",
"ecma_loader_parking_lot",
"parallel_rayon",
] }
swc_plugin_backend_wasmtime = "14.0.0"
testing = "25.0.0"
swc_plugin_backend_wasmtime = "15.0.0"
testing = "26.0.0"
# Keep consistent with preset_env_base through swc_core
browserslist-rs = "0.19.0"
browserslist-rs = "0.20.0"
mdxjs = "1.0.4"
modularize_imports = "15.0.0"
styled_components = "15.0.0"
styled_jsx = "15.0.0"
swc_emotion = "15.0.0"
swc_relay = "15.0.0"
react_remove_properties = "15.0.0"
remove_console = "15.0.0"
preset_env_base = "8.0.1"
modularize_imports = "16.0.0"
styled_components = "16.0.0"
styled_jsx = "16.0.0"
swc_emotion = "16.0.0"
swc_relay = "16.0.0"
react_remove_properties = "16.0.0"
remove_console = "16.0.0"
preset_env_base = "9.0.0"
# General Deps
......
......@@ -30,7 +30,7 @@ use turbopack_core::{
use turbopack_css::chunk::CssChunkType;
use turbopack_ecmascript::{
AnalyzeMode, CustomTransformer, TransformPlugin, TypeofWindow, chunk::EcmascriptChunkType,
references::esm::UrlRewriteBehavior, transform::ReactCompilerTarget,
references::esm::UrlRewriteBehavior,
};
use turbopack_ecmascript_plugins::transform::directives::{
client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer,
......@@ -63,10 +63,7 @@ use crate::{
styled_jsx::get_styled_jsx_transform_rule,
swc_ecma_transform_plugins::get_swc_ecma_transform_plugin_rule,
},
webpack_rules::{
WebpackLoaderBuiltinCondition, babel::detect_react_compiler_target,
webpack_loader_options,
},
webpack_rules::{WebpackLoaderBuiltinCondition, webpack_loader_options},
},
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
......@@ -541,16 +538,6 @@ pub async fn get_server_module_options_context(
.flatten()
.collect();
let enable_rust_react_compiler = *next_config.rust_react_compiler().await?;
let rust_react_compiler_target = if enable_rust_react_compiler.is_some() {
match detect_react_compiler_target(&project_path).await? {
Some(ReactCompilerTarget::React18) => ReactCompilerTarget::React18,
_ => ReactCompilerTarget::React19,
}
} else {
ReactCompilerTarget::React19
};
let source_maps = *next_config.server_source_maps().await?;
let module_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
......@@ -721,8 +708,8 @@ pub async fn get_server_module_options_context(
enable_jsx: Some(jsx_runtime_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?),
enable_rust_react_compiler,
rust_react_compiler_target,
// React Compiler only optimizes the React client runtime, so skip it.
enable_rust_react_compiler: None,
..module_options_context.ecmascript
},
enable_webpack_loaders,
......
......@@ -39,7 +39,6 @@ use rustc_hash::FxHasher;
pub mod chain_transforms;
mod linter;
pub mod react_compiler;
pub mod transforms;
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
use swc_core::ecma::{
ast::{
Callee, ExportDefaultDecl, ExportDefaultExpr, Expr, FnDecl, FnExpr, Pat, Program, Stmt,
VarDeclarator,
},
visit::{Visit, VisitWith},
};
pub fn is_required(program: &Program) -> bool {
let mut finder = Finder::default();
finder.visit_program(program);
finder.found
}
#[derive(Default)]
struct Finder {
found: bool,
/// We are in a function that starts with a capital letter or it's a function that starts with
/// `use`
is_interested: bool,
}
impl Visit for Finder {
fn visit_callee(&mut self, node: &Callee) {
if self.is_interested
&& let Callee::Expr(e) = node
&& let Expr::Ident(c) = &**e
&& c.sym.starts_with("use")
{
self.found = true;
return;
}
node.visit_children_with(self);
}
fn visit_export_default_decl(&mut self, node: &ExportDefaultDecl) {
let old = self.is_interested;
self.is_interested = true;
node.visit_children_with(self);
self.is_interested = old;
}
fn visit_export_default_expr(&mut self, node: &ExportDefaultExpr) {
let old = self.is_interested;
self.is_interested = true;
node.visit_children_with(self);
self.is_interested = old;
}
fn visit_expr(&mut self, node: &Expr) {
if self.found {
return;
}
if self.is_interested
&& matches!(
node,
Expr::JSXMember(..)
| Expr::JSXNamespacedName(..)
| Expr::JSXEmpty(..)
| Expr::JSXElement(..)
| Expr::JSXFragment(..)
)
{
self.found = true;
return;
}
node.visit_children_with(self);
}
fn visit_fn_decl(&mut self, node: &FnDecl) {
let old = self.is_interested;
self.is_interested = node.ident.sym.starts_with("use")
|| node.ident.sym.starts_with(|c: char| c.is_ascii_uppercase());
node.visit_children_with(self);
self.is_interested = old;
}
fn visit_fn_expr(&mut self, node: &FnExpr) {
let old = self.is_interested;
self.is_interested |= node.ident.as_ref().is_some_and(|ident| {
ident.sym.starts_with("use") || ident.sym.starts_with(|c: char| c.is_ascii_uppercase())
});
node.visit_children_with(self);
self.is_interested = old;
}
fn visit_stmt(&mut self, node: &Stmt) {
if self.found {
return;
}
node.visit_children_with(self);
}
fn visit_var_declarator(&mut self, node: &VarDeclarator) {
let old = self.is_interested;
if matches!(node.init.as_deref(), Some(Expr::Fn(..) | Expr::Arrow(..))) {
if let Pat::Ident(ident) = &node.name {
self.is_interested = ident.sym.starts_with("use")
|| ident.sym.starts_with(|c: char| c.is_ascii_uppercase());
} else {
self.is_interested = false;
}
}
node.visit_children_with(self);
self.is_interested = old;
}
}
#[cfg(test)]
mod tests {
use swc_core::{
common::FileName,
ecma::parser::{EsSyntax, parse_file_as_program},
};
use testing::run_test2;
use super::*;
fn assert_required(code: &str, required: bool) {
run_test2(false, |cm, _| {
let fm =
cm.new_source_file(FileName::Custom("test.tsx".into()).into(), code.to_string());
let program = parse_file_as_program(
&fm,
swc_core::ecma::parser::Syntax::Es(EsSyntax {
jsx: true,
..Default::default()
}),
Default::default(),
Default::default(),
&mut vec![],
)
.unwrap();
assert_eq!(is_required(&program), required);
Ok(())
})
.unwrap();
}
#[test]
fn lazy_return() {
assert_required(
"
function Foo() {
const a = <div>Hello</div>;
return a
}
",
true,
);
assert_required(
"
function Foo() {
",
false,
);
}
#[test]
fn return_jsx() {
assert_required(
"
function Foo() {
return <div>Hello</div>;
}
",
true,
);
}
#[test]
fn use_hooks() {
assert_required(
"
function Foo(props) {
const [a, b] = useState(0);
return props.children;
}
",
true,
);
}
#[test]
fn arrow_function() {
assert_required(
"
const Foo = () => <div>Hello</div>;
",
true,
);
assert_required(
"
const Foo = () => {
return <div>Hello</div>;
};
",
true,
);
}
#[test]
fn export_const_arrow_function() {
assert_required(
"
export const Foo = () => <div>Hello</div>;
",
true,
);
assert_required(
"
export const Foo = () => {
return <div>Hello</div>;
};
",
true,
);
}
#[test]
fn normal_arrow_function() {
assert_required(
"
const Foo = () => {
const a = 1;
console.log(a);
};
",
false,
);
}
#[test]
fn export_default_arrow_function() {
assert_required(
"
export default () => <div>Hello</div>;
",
true,
);
}
#[test]
fn not_required_arrow_function() {
assert_required(
"
export default () => {
const a = 1;
console.log(a);
};
",
false,
);
}
}
......@@ -88,6 +88,7 @@ swc_core = { workspace = true, features = [
# Dependencies for the native, non-wasm32 build.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
swc_ecma_react_compiler = { workspace = true }
lightningcss-napi = { workspace = true }
lightningcss = { workspace = true }
swc_plugin_backend_wasmtime = { workspace = true }
......
......@@ -51,6 +51,7 @@ pub mod minify;
#[cfg(not(target_arch = "wasm32"))]
pub mod next_api;
pub mod parse;
#[cfg(not(target_arch = "wasm32"))]
pub mod react_compiler;
pub mod rspack;
pub mod transform;
......
......@@ -2,7 +2,6 @@ use std::{path::PathBuf, sync::Arc};
use napi::bindgen_prelude::*;
use napi_derive::napi;
use next_custom_transforms::react_compiler;
use swc_core::{
common::{GLOBALS, SourceMap},
ecma::{
......@@ -41,10 +40,12 @@ impl Task for CheckTask {
return Ok(false);
};
if !errors.is_empty() {
return Ok(false);
// SWC recovered an AST, so the source may still be accepted by Babel. Keep the
// compiler enabled rather than turning a parser difference into a false negative.
return Ok(true);
}
Ok(react_compiler::is_required(&program))
Ok(swc_ecma_react_compiler::fast_check::is_required(&program))
})
}
......
......@@ -8,7 +8,10 @@ use swc_core::{
base::SwcComments,
common::{Mark, SourceMap, comments::Comments},
ecma::{
ast::{ExprStmt, ModuleItem, Pass, Program, Stmt},
ast::{
ArrowExpr, BlockStmtOrExpr, Expr, ExprStmt, Function, Lit, ModuleItem, Pass, Program,
Stmt,
},
preset_env::{self, Feature, FeatureOrModule, Targets},
transforms::{
base::{
......@@ -19,6 +22,7 @@ use swc_core::{
typescript::{Config, typescript},
},
utils::IsDirective,
visit::{Visit, VisitWith},
},
quote,
};
......@@ -447,6 +451,89 @@ impl Issue for ReactCompilerIssue {
}
}
// Keep this in sync with React Compiler's annotation-mode opt-ins. Next.js does not configure
// `dynamic_gating`, so only the standard `use memo` and legacy `use forget` directives enable a
// function.
fn has_react_compiler_opt_in_directive(statements: &[Stmt]) -> bool {
for statement in statements {
if !statement.directive_continue() {
break;
}
let Stmt::Expr(expression) = statement else {
continue;
};
let Expr::Lit(Lit::Str(value)) = &*expression.expr else {
continue;
};
if value
.value
.as_str()
.is_some_and(|value| matches!(value, "use memo" | "use forget"))
{
return true;
}
}
false
}
#[derive(Default)]
struct ReactCompilerAnnotationFinder {
found: bool,
}
impl Visit for ReactCompilerAnnotationFinder {
fn visit_arrow_expr(&mut self, node: &ArrowExpr) {
if self.found {
return;
}
if let BlockStmtOrExpr::BlockStmt(body) = &*node.body
&& has_react_compiler_opt_in_directive(&body.stmts)
{
self.found = true;
return;
}
node.visit_children_with(self);
}
fn visit_function(&mut self, node: &Function) {
if self.found {
return;
}
if node
.body
.as_ref()
.is_some_and(|body| has_react_compiler_opt_in_directive(&body.stmts))
{
self.found = true;
return;
}
node.visit_children_with(self);
}
}
fn has_react_compiler_annotation(program: &Program) -> bool {
let mut finder = ReactCompilerAnnotationFinder::default();
finder.visit_program(program);
finder.found
}
fn should_run_rust_react_compiler(
program: &Program,
compilation_mode: ReactCompilerCompilationMode,
) -> bool {
match compilation_mode {
ReactCompilerCompilationMode::Infer => {
swc_ecma_react_compiler::fast_check::is_required(program)
}
ReactCompilerCompilationMode::Annotation => has_react_compiler_annotation(program),
ReactCompilerCompilationMode::All => true,
}
}
async fn apply_rust_react_compiler(
program: &mut Program,
ctx: &TransformContext<'_>,
......@@ -458,6 +545,13 @@ async fn apply_rust_react_compiler(
return Ok(helpers);
};
// Avoid invoking the compiler when the selected mode cannot change this module. These checks
// run on the SWC AST we already parsed, before converting it to the compiler AST. `All` mode
// remains unconditional because every function is eligible.
if !should_run_rust_react_compiler(program, compilation_mode) {
return Ok(helpers);
}
let single_threaded_comments =
crate::swc_comments::swc_comments_to_single_threaded(ctx.comments);
let result = swc_ecma_react_compiler::transform(
......@@ -583,3 +677,126 @@ pub fn remove_directives(program: &mut Program) {
}
}
}
#[cfg(test)]
mod react_compiler_tests {
use swc_core::{
common::{DUMMY_SP, FileName, GLOBALS, SourceMap},
ecma::{
ast::{EsVersion, Module},
parser::{Syntax, TsSyntax, parse_file_as_program},
},
};
use super::*;
fn parse_program(source: &str) -> Program {
GLOBALS.set(&Default::default(), || {
let cm = SourceMap::default();
let fm = cm.new_source_file(
FileName::Custom("test.tsx".into()).into(),
source.to_owned(),
);
let mut errors = Vec::new();
let program = parse_file_as_program(
&fm,
Syntax::Typescript(TsSyntax {
tsx: true,
..Default::default()
}),
EsVersion::EsNext,
None,
&mut errors,
)
.expect("test fixture should parse");
assert!(errors.is_empty(), "test fixture should not recover errors");
program
})
}
#[test]
fn compilation_modes_use_their_respective_fast_checks() {
let program = Program::Module(Module {
span: DUMMY_SP,
body: Vec::new(),
shebang: None,
});
for mode in [
ReactCompilerCompilationMode::Infer,
ReactCompilerCompilationMode::Annotation,
] {
assert!(!should_run_rust_react_compiler(&program, mode));
}
assert!(should_run_rust_react_compiler(
&program,
ReactCompilerCompilationMode::All,
));
}
#[test]
fn infer_mode_uses_upstream_conservative_fast_check() {
for source in [
"const Button = React.forwardRef((props, ref) => <button ref={ref} />);",
"function useCounter() { return React.useState(0); }",
"function helper() { 'use memo'; return 1; }",
] {
assert!(should_run_rust_react_compiler(
&parse_program(source),
ReactCompilerCompilationMode::Infer
));
}
for source in [
"export const answer = 42;",
"const user = getUser();",
"function helper() { log(); 'use memo'; }",
] {
assert!(!should_run_rust_react_compiler(
&parse_program(source),
ReactCompilerCompilationMode::Infer
));
}
}
#[test]
fn annotation_mode_only_runs_for_function_opt_in_directives() {
for source in [
"function helper() { 'use memo'; return 1; }",
"const helper = () => { 'use forget'; return 1; };",
"function outer() { function inner() { 'use memo'; return 1; } }",
] {
assert!(should_run_rust_react_compiler(
&parse_program(source),
ReactCompilerCompilationMode::Annotation,
));
}
for source in [
"function Component() { return <div />; }",
"function useCounter() { return useState(0); }",
"function helper() { log(); 'use memo'; }",
"'use memo'; export const answer = 42;",
"function helper() { 'use memo if(featureFlag)'; return 1; }",
"function helper() { 'use no memo'; return 1; }",
] {
assert!(!should_run_rust_react_compiler(
&parse_program(source),
ReactCompilerCompilationMode::Annotation,
));
}
}
#[test]
fn all_mode_remains_unconditional() {
for source in [
"export const answer = 42;",
"function helper() { return 1; }",
] {
assert!(should_run_rust_react_compiler(
&parse_program(source),
ReactCompilerCompilationMode::All,
));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment