-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler] Remove transitive memo check in validatePreserveMemo #30630
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
import {useCallback} from 'react'; | ||
import {identity, useIdentity} from 'shared-runtime'; | ||
|
||
/** | ||
* Repro showing a manual memo whose declaration (useCallback's 1st argument) | ||
* is memoized, but not its dependency (x). In this case, `x`'s scope is pruned | ||
* due to hook-call flattening. | ||
*/ | ||
function useFoo(a) { | ||
const x = identity(a); | ||
useIdentity(2); | ||
mutate(x); | ||
|
||
return useCallback(() => [x, []], [x]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [3], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees | ||
import { useCallback } from "react"; | ||
import { identity, useIdentity } from "shared-runtime"; | ||
|
||
/** | ||
* Repro showing a manual memo whose declaration (useCallback's 1st argument) | ||
* is memoized, but not its dependency (x). In this case, `x`'s scope is pruned | ||
* due to hook-call flattening. | ||
*/ | ||
function useFoo(a) { | ||
const $ = _c(2); | ||
const x = identity(a); | ||
useIdentity(2); | ||
mutate(x); | ||
let t0; | ||
if ($[0] !== x) { | ||
t0 = () => [x, []]; | ||
$[0] = x; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this output correctly preserves manual memoization |
||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [3], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: exception) mutate is not defined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :-) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees:true | ||
|
||
import {useCallback} from 'react'; | ||
import {Stringify, useIdentity} from 'shared-runtime'; | ||
|
||
/** | ||
* Here, the *inferred* dependencies of cb are `a` and `t1 = LoadContext capture x_@1`. | ||
* - t1 does not have a scope as it captures `x` after x's mutable range | ||
* - `x` is a context variable, which means its mutable range extends to all | ||
* references / aliases. | ||
* - `a`, `b`, and `x` get the same mutable range due to potential aliasing. | ||
* | ||
* We currently bail out because `a` has a scope and is not transitively memoized | ||
* (as its scope is pruned due to a hook call) | ||
*/ | ||
function useBar({a, b}, cond) { | ||
let x = useIdentity({val: 3}); | ||
if (cond) { | ||
x = b; | ||
} | ||
|
||
const cb = useCallback(() => { | ||
return [a, x]; | ||
}, [a, x]); | ||
|
||
return <Stringify cb={cb} shouldInvoke={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useBar, | ||
params: [{a: 1, b: 2}, true], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees:true | ||
|
||
import { useCallback } from "react"; | ||
import { Stringify, useIdentity } from "shared-runtime"; | ||
|
||
/** | ||
* Here, the *inferred* dependencies of cb are `a` and `t1 = LoadContext capture x_@1`. | ||
* - t1 does not have a scope as it captures `x` after x's mutable range | ||
* - `x` is a context variable, which means its mutable range extends to all | ||
* references / aliases. | ||
* - `a`, `b`, and `x` get the same mutable range due to potential aliasing. | ||
* | ||
* We currently bail out because `a` has a scope and is not transitively memoized | ||
* (as its scope is pruned due to a hook call) | ||
*/ | ||
function useBar(t0, cond) { | ||
const $ = _c(6); | ||
const { a, b } = t0; | ||
let t1; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t1 = { val: 3 }; | ||
$[0] = t1; | ||
} else { | ||
t1 = $[0]; | ||
} | ||
let x; | ||
x = useIdentity(t1); | ||
if (cond) { | ||
x = b; | ||
} | ||
|
||
const t2 = x; | ||
let t3; | ||
if ($[1] !== a || $[2] !== t2) { | ||
t3 = () => [a, x]; | ||
$[1] = a; | ||
$[2] = t2; | ||
$[3] = t3; | ||
} else { | ||
t3 = $[3]; | ||
} | ||
Comment on lines
+76
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This inferred memoization also preserves manual memo |
||
x; | ||
const cb = t3; | ||
let t4; | ||
if ($[4] !== cb) { | ||
t4 = <Stringify cb={cb} shouldInvoke={true} />; | ||
$[4] = cb; | ||
$[5] = t4; | ||
} else { | ||
t4 = $[5]; | ||
} | ||
return t4; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useBar, | ||
params: [{ a: 1, b: 2 }, true], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) <div>{"cb":"[[ function params=0 ]]","shouldInvoke":true}</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(drive by dead code deletion)