Skip to content

Commit eda778b

Browse files
mofeiZjosephsavona
andauthored
[compiler] Fix false positive memo validation (alternative) (#34319)
Alternative to #34276 --- (Summary taken from @josephsavona 's #34276) Partial fix for #34262. Consider this example: ```js function useInputValue(input) { const object = React.useMemo(() => { const {value} = transform(input); return {value}; }, [input]); return object; } ``` React Compiler breaks this code into two reactive scopes: * One for `transform(input)` * One for `{value}` When we run ValidatePreserveExistingMemo, we see that the scope for `{value}` has the dependency `value`, whereas the original memoization had the dependency `input`, and throw an error that the dependencies didn't match. In other words, we're flagging the fact that memoized _better than the user_ as a problem. The more complete solution would be to validate that there is a subgraph of reactive scopes with a single input and output node, where the input node has the same dependencies as the original useMemo, and the output has the same outputs. That is true in this case, with the subgraph being the two consecutive scopes mentioned above. But that's complicated. As a shortcut, this PR checks for any dependencies that are defined after the start of the original useMemo. If we find one, we know that it's a case where we were able to memoize more precisely than the original, and we don't report an error on the dependency. We still check that the original _output_ value is able to be memoized, though. So if the scope of `object` were extended, eg with a call to `mutate(object)`, then we'd still correctly report an error that we couldn't preserve memoization. Co-authored-by: Joe Savona <[email protected]>
1 parent 1836b46 commit eda778b

7 files changed

+364
-45
lines changed

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
IdentifierId,
1919
InstructionValue,
2020
ManualMemoDependency,
21-
Place,
2221
PrunedReactiveScopeBlock,
2322
ReactiveFunction,
2423
ReactiveInstruction,
@@ -29,7 +28,10 @@ import {
2928
SourceLocation,
3029
} from '../HIR';
3130
import {printIdentifier, printManualMemoDependency} from '../HIR/PrintHIR';
32-
import {eachInstructionValueOperand} from '../HIR/visitors';
31+
import {
32+
eachInstructionValueLValue,
33+
eachInstructionValueOperand,
34+
} from '../HIR/visitors';
3335
import {collectMaybeMemoDependencies} from '../Inference/DropManualMemoization';
3436
import {
3537
ReactiveFunctionVisitor,
@@ -337,56 +339,53 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
337339
* @returns a @{ManualMemoDependency} representing the variable +
338340
* property reads represented by @value
339341
*/
340-
recordDepsInValue(
341-
value: ReactiveValue,
342-
state: VisitorState,
343-
): ManualMemoDependency | null {
342+
recordDepsInValue(value: ReactiveValue, state: VisitorState): void {
344343
switch (value.kind) {
345344
case 'SequenceExpression': {
346345
for (const instr of value.instructions) {
347346
this.visitInstruction(instr, state);
348347
}
349-
const result = this.recordDepsInValue(value.value, state);
350-
return result;
348+
this.recordDepsInValue(value.value, state);
349+
break;
351350
}
352351
case 'OptionalExpression': {
353-
return this.recordDepsInValue(value.value, state);
352+
this.recordDepsInValue(value.value, state);
353+
break;
354354
}
355355
case 'ConditionalExpression': {
356356
this.recordDepsInValue(value.test, state);
357357
this.recordDepsInValue(value.consequent, state);
358358
this.recordDepsInValue(value.alternate, state);
359-
return null;
359+
break;
360360
}
361361
case 'LogicalExpression': {
362362
this.recordDepsInValue(value.left, state);
363363
this.recordDepsInValue(value.right, state);
364-
return null;
364+
break;
365365
}
366366
default: {
367-
const dep = collectMaybeMemoDependencies(
368-
value,
369-
this.temporaries,
370-
false,
371-
);
372-
if (value.kind === 'StoreLocal' || value.kind === 'StoreContext') {
373-
const storeTarget = value.lvalue.place;
374-
state.manualMemoState?.decls.add(
375-
storeTarget.identifier.declarationId,
376-
);
377-
if (storeTarget.identifier.name?.kind === 'named' && dep == null) {
378-
const dep: ManualMemoDependency = {
379-
root: {
380-
kind: 'NamedLocal',
381-
value: storeTarget,
382-
},
383-
path: [],
384-
};
385-
this.temporaries.set(storeTarget.identifier.id, dep);
386-
return dep;
367+
collectMaybeMemoDependencies(value, this.temporaries, false);
368+
if (
369+
value.kind === 'StoreLocal' ||
370+
value.kind === 'StoreContext' ||
371+
value.kind === 'Destructure'
372+
) {
373+
for (const storeTarget of eachInstructionValueLValue(value)) {
374+
state.manualMemoState?.decls.add(
375+
storeTarget.identifier.declarationId,
376+
);
377+
if (storeTarget.identifier.name?.kind === 'named') {
378+
this.temporaries.set(storeTarget.identifier.id, {
379+
root: {
380+
kind: 'NamedLocal',
381+
value: storeTarget,
382+
},
383+
path: [],
384+
});
385+
}
387386
}
388387
}
389-
return dep;
388+
break;
390389
}
391390
}
392391
}
@@ -403,19 +402,15 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
403402
state.manualMemoState.decls.add(lvalue.identifier.declarationId);
404403
}
405404

406-
const maybeDep = this.recordDepsInValue(value, state);
407-
if (lvalId != null) {
408-
if (maybeDep != null) {
409-
temporaries.set(lvalId, maybeDep);
410-
} else if (isNamedLocal) {
411-
temporaries.set(lvalId, {
412-
root: {
413-
kind: 'NamedLocal',
414-
value: {...(instr.lvalue as Place)},
415-
},
416-
path: [],
417-
});
418-
}
405+
this.recordDepsInValue(value, state);
406+
if (lvalue != null) {
407+
temporaries.set(lvalue.identifier.id, {
408+
root: {
409+
kind: 'NamedLocal',
410+
value: {...lvalue},
411+
},
412+
path: [],
413+
});
419414
}
420415
}
421416

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validatePreserveExistingMemoizationGuarantees
6+
7+
/**
8+
* Repro from https://github.com/facebook/react/issues/34262
9+
*
10+
* The compiler memoizes more precisely than the original code, with two reactive scopes:
11+
* - One for `transform(input)` with `input` as dep
12+
* - One for `{value}` with `value` as dep
13+
*
14+
* When we validate preserving manual memoization we incorrectly reject this, because
15+
* the original memoization had `object` depending on `input` but our scope depends on
16+
* `value`.
17+
*
18+
* This fixture adds a later potential mutation, which extends the scope and should
19+
* fail validation. This confirms that even though we allow the dependency to diverge,
20+
* we still check that the output value is memoized.
21+
*/
22+
function useInputValue(input) {
23+
const object = React.useMemo(() => {
24+
const {value} = transform(input);
25+
return {value};
26+
}, [input]);
27+
mutate(object);
28+
return object;
29+
}
30+
31+
```
32+
33+
34+
## Error
35+
36+
```
37+
Found 1 error:
38+
39+
Compilation Skipped: Existing memoization could not be preserved
40+
41+
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output.
42+
43+
error.repro-preserve-memoization-inner-destructured-value-mistaken-as-dependency-later-mutation.ts:19:17
44+
17 | */
45+
18 | function useInputValue(input) {
46+
> 19 | const object = React.useMemo(() => {
47+
| ^^^^^^^^^^^^^^^^^^^^^
48+
> 20 | const {value} = transform(input);
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
> 21 | return {value};
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
> 22 | }, [input]);
53+
| ^^^^^^^^^^^^^^ Could not preserve existing memoization
54+
23 | mutate(object);
55+
24 | return object;
56+
25 | }
57+
```
58+
59+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @validatePreserveExistingMemoizationGuarantees
2+
3+
/**
4+
* Repro from https://github.com/facebook/react/issues/34262
5+
*
6+
* The compiler memoizes more precisely than the original code, with two reactive scopes:
7+
* - One for `transform(input)` with `input` as dep
8+
* - One for `{value}` with `value` as dep
9+
*
10+
* When we validate preserving manual memoization we incorrectly reject this, because
11+
* the original memoization had `object` depending on `input` but our scope depends on
12+
* `value`.
13+
*
14+
* This fixture adds a later potential mutation, which extends the scope and should
15+
* fail validation. This confirms that even though we allow the dependency to diverge,
16+
* we still check that the output value is memoized.
17+
*/
18+
function useInputValue(input) {
19+
const object = React.useMemo(() => {
20+
const {value} = transform(input);
21+
return {value};
22+
}, [input]);
23+
mutate(object);
24+
return object;
25+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validatePreserveExistingMemoizationGuarantees
6+
7+
import {identity, Stringify, useHook} from 'shared-runtime';
8+
9+
/**
10+
* Repro from https://github.com/facebook/react/issues/34262
11+
*
12+
* The compiler memoizes more precisely than the original code, with two reactive scopes:
13+
* - One for `transform(input)` with `input` as dep
14+
* - One for `{value}` with `value` as dep
15+
*
16+
* When we validate preserving manual memoization we incorrectly reject this, because
17+
* the original memoization had `object` depending on `input` but our scope depends on
18+
* `value`.
19+
*/
20+
function useInputValue(input) {
21+
// Conflate the `identity(input, x)` call with something outside the useMemo,
22+
// to try and break memoization of `value`. This gets correctly flagged since
23+
// the dependency is being mutated
24+
let x = {};
25+
useHook();
26+
const object = React.useMemo(() => {
27+
const {value} = identity(input, x);
28+
return {value};
29+
}, [input, x]);
30+
return object;
31+
}
32+
33+
function Component() {
34+
return <Stringify value={useInputValue({value: 42}).value} />;
35+
}
36+
37+
export const FIXTURE_ENTRYPOINT = {
38+
fn: Component,
39+
params: [{}],
40+
};
41+
42+
```
43+
44+
45+
## Error
46+
47+
```
48+
Found 1 error:
49+
50+
Compilation Skipped: Existing memoization could not be preserved
51+
52+
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This dependency may be mutated later, which could cause the value to change unexpectedly.
53+
54+
error.repro-preserve-memoization-inner-destructured-value-mistaken-as-dependency-mutated-dep.ts:25:13
55+
23 | const {value} = identity(input, x);
56+
24 | return {value};
57+
> 25 | }, [input, x]);
58+
| ^ This dependency may be modified later
59+
26 | return object;
60+
27 | }
61+
28 |
62+
```
63+
64+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @validatePreserveExistingMemoizationGuarantees
2+
3+
import {identity, Stringify, useHook} from 'shared-runtime';
4+
5+
/**
6+
* Repro from https://github.com/facebook/react/issues/34262
7+
*
8+
* The compiler memoizes more precisely than the original code, with two reactive scopes:
9+
* - One for `transform(input)` with `input` as dep
10+
* - One for `{value}` with `value` as dep
11+
*
12+
* When we validate preserving manual memoization we incorrectly reject this, because
13+
* the original memoization had `object` depending on `input` but our scope depends on
14+
* `value`.
15+
*/
16+
function useInputValue(input) {
17+
// Conflate the `identity(input, x)` call with something outside the useMemo,
18+
// to try and break memoization of `value`. This gets correctly flagged since
19+
// the dependency is being mutated
20+
let x = {};
21+
useHook();
22+
const object = React.useMemo(() => {
23+
const {value} = identity(input, x);
24+
return {value};
25+
}, [input, x]);
26+
return object;
27+
}
28+
29+
function Component() {
30+
return <Stringify value={useInputValue({value: 42}).value} />;
31+
}
32+
33+
export const FIXTURE_ENTRYPOINT = {
34+
fn: Component,
35+
params: [{}],
36+
};

0 commit comments

Comments
 (0)