Skip to content

Commit a355b48

Browse files
josephsavonamofeiZ
authored andcommitted
[compiler] Fix false positive memo validation (alternative)
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.
1 parent 3d9d22c commit a355b48

7 files changed

+363
-43
lines changed

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

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import {
2929
SourceLocation,
3030
} from '../HIR';
3131
import {printIdentifier, printManualMemoDependency} from '../HIR/PrintHIR';
32-
import {eachInstructionValueOperand} from '../HIR/visitors';
32+
import {
33+
eachInstructionValueLValue,
34+
eachInstructionValueOperand,
35+
} from '../HIR/visitors';
3336
import {collectMaybeMemoDependencies} from '../Inference/DropManualMemoization';
3437
import {
3538
ReactiveFunctionVisitor,
@@ -337,56 +340,53 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
337340
* @returns a @{ManualMemoDependency} representing the variable +
338341
* property reads represented by @value
339342
*/
340-
recordDepsInValue(
341-
value: ReactiveValue,
342-
state: VisitorState,
343-
): ManualMemoDependency | null {
343+
recordDepsInValue(value: ReactiveValue, state: VisitorState): void {
344344
switch (value.kind) {
345345
case 'SequenceExpression': {
346346
for (const instr of value.instructions) {
347347
this.visitInstruction(instr, state);
348348
}
349-
const result = this.recordDepsInValue(value.value, state);
350-
return result;
349+
this.recordDepsInValue(value.value, state);
350+
break;
351351
}
352352
case 'OptionalExpression': {
353-
return this.recordDepsInValue(value.value, state);
353+
this.recordDepsInValue(value.value, state);
354+
break;
354355
}
355356
case 'ConditionalExpression': {
356357
this.recordDepsInValue(value.test, state);
357358
this.recordDepsInValue(value.consequent, state);
358359
this.recordDepsInValue(value.alternate, state);
359-
return null;
360+
break;
360361
}
361362
case 'LogicalExpression': {
362363
this.recordDepsInValue(value.left, state);
363364
this.recordDepsInValue(value.right, state);
364-
return null;
365+
break;
365366
}
366367
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;
368+
collectMaybeMemoDependencies(value, this.temporaries, false);
369+
if (
370+
value.kind === 'StoreLocal' ||
371+
value.kind === 'StoreContext' ||
372+
value.kind === 'Destructure'
373+
) {
374+
for (const storeTarget of eachInstructionValueLValue(value)) {
375+
state.manualMemoState?.decls.add(
376+
storeTarget.identifier.declarationId,
377+
);
378+
if (storeTarget.identifier.name?.kind === 'named') {
379+
this.temporaries.set(storeTarget.identifier.id, {
380+
root: {
381+
kind: 'NamedLocal',
382+
value: storeTarget,
383+
},
384+
path: [],
385+
});
386+
}
387387
}
388388
}
389-
return dep;
389+
break;
390390
}
391391
}
392392
}
@@ -403,19 +403,15 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
403403
state.manualMemoState.decls.add(lvalue.identifier.declarationId);
404404
}
405405

406-
const maybeDep = this.recordDepsInValue(value, state);
406+
this.recordDepsInValue(value, state);
407407
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-
}
408+
temporaries.set(lvalId, {
409+
root: {
410+
kind: 'NamedLocal',
411+
value: {...(instr.lvalue as Place)},
412+
},
413+
path: [],
414+
});
419415
}
420416
}
421417

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+
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+
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)