From 82a0a5f88a1bd0f2a0af52e44bbacff1207fc144 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 21 May 2024 13:10:27 +0100 Subject: [PATCH] compiler: fixture for suboptimal jsx sibling memo block merging React Compiler attempts to merge consecutive reactive scopes in order to reduce overhead. The basic idea is that if two consecutive scopes would always invalidate together then we should merge them. It gets more complicated, though, because values produced by the earlier scope may not always invalidate when their inputs do. For example, a scope that produces `fn(x)` may not invalidate on all changes to `x` if the function is `Math.max(x, 10)` (changing x from 8 to 9 won't change the output). Previously we were conservative and only merged if either: * the two scopes had the same dependencies * the second scope's deps exactly matched the previous scope's outputs. You can see this in the new fixture, where the second ` + {/** + * The scope for the + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; + +function Component() { + const $ = _c(8); + const [count, setCount] = useState(0); + let t0; + let t1; + if ($[0] !== count) { + t0 = ; + + t1 = () => setCount(count + 1); + $[0] = count; + $[1] = t0; + $[2] = t1; + } else { + t0 = $[1]; + t1 = $[2]; + } + let t2; + if ($[3] !== t1) { + t2 = ; + $[3] = t1; + $[4] = t2; + } else { + t2 = $[4]; + } + let t3; + if ($[5] !== t0 || $[6] !== t2) { + t3 = ( +
+ {t0} + {t2} +
+ ); + $[5] = t0; + $[6] = t2; + $[7] = t3; + } else { + t3 = $[7]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-deps-subset-of-decls.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-deps-subset-of-decls.js new file mode 100644 index 0000000000..71cea01abc --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-deps-subset-of-decls.js @@ -0,0 +1,21 @@ +import { useState } from "react"; + +function Component() { + const [count, setCount] = useState(0); + return ( +
+ + {/** + * The scope for the +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +};