Skip to content

Commit ae34446

Browse files
committed
[compiler] @enablePreserveExistingMemoizationGuarantees on by default
This enables `@enablePreserveExistingMemoizationGuarantees` by default. As of the previous PR (#34503), this mode now enables the following behaviors: - Treating variables referenced within a `useMemo()` or `useCallback()` as "frozen" (immutable) as of the start of the call. Ie, the compiler will assume that the values you reference are not mutated by the body of the useMemo, not are they mutated later. Directly modifying them (eg `var.property = true`) will be an error. - Similarly, the results of the useMemo/useCallback are treated as frozen (immutable) after the call. These two rules match the behavior for other hooks: this means that developers will see similar behavior to swapping out `useMemo()` for a custom `useMyMemo()` wrapper/alias. Additionally, as of #34503 the compiler uses information from the manual dependencies to know which variables are non-nullable. Even if a useMemo block conditionally accesses a nested property — `if (cond) { log(x.y.z) }` — where the compiler would not usually know that `x` is non-nullable, if the user specifies `x.y.z` as a manual dependency then the compiler knows that `x` and `x.y` are non-nullable and can infer a more precise dependency. Finally, this mode also ensures that we always memoize function calls that return primitives. See #34343 for more details. For now, I've explicitly opted out of this feature in all test fixtures where the behavior changed.
1 parent 57d5a59 commit ae34446

File tree

103 files changed

+138
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+138
-79
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export const EnvironmentConfigSchema = z.object({
210210
* that if a useEffect or useCallback references a function value, that function value will be
211211
* considered frozen, and in turn all of its referenced variables will be considered frozen as well.
212212
*/
213-
enablePreserveExistingMemoizationGuarantees: z.boolean().default(false),
213+
enablePreserveExistingMemoizationGuarantees: z.boolean().default(true),
214214

215215
/**
216216
* Validates that all useMemo/useCallback values are also memoized by Forget. This mode can be

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allocating-primitive-as-dep.expect.md

Lines changed: 3 additions & 1 deletion

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allocating-primitive-as-dep.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @enablePreserveExistingMemoizationGuarantees:false
12
// bar(props.b) is an allocating expression that produces a primitive, which means
23
// that Forget should memoize it.
34
// Correctness:

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-modify-global-in-callback-jsx.expect.md

Lines changed: 2 additions & 1 deletion

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-modify-global-in-callback-jsx.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @enablePreserveExistingMemoizationGuarantees:false
12
import {useMemo} from 'react';
23

34
const someGlobal = {value: 0};

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.expect.md

Lines changed: 2 additions & 1 deletion

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @enablePreserveExistingMemoizationGuarantees:false
12
function Component(props) {
23
let a = foo();
34
// freeze `a` so we know the next line cannot mutate it

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.expect.md

Lines changed: 2 additions & 1 deletion

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @enablePreserveExistingMemoizationGuarantees:false
12
import {Stringify, identity} from 'shared-runtime';
23

34
function foo() {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)