-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Stop using function
dependencies
in propagateScopeDeps (#…
…31200) Recursively visit inner function instructions to extract dependencies instead of using `LoweredFunction.dependencies` directly. This is currently gated by enableFunctionDependencyRewrite, which needs to be removed before we delete `LoweredFunction.dependencies` altogether (#31204). Some nice side effects - optional-chaining deps for inner functions - full DCE and outlining for inner functions (see #31202) - fewer extraneous instructions (see #31204) - --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31200). * #31202 * #31203 * #31201 * __->__ #31200 * #31521
- Loading branch information
Showing
12 changed files
with
159 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...alidation/error.todo-useCallback-captures-reassigned-context-property.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
import {useCallback} from 'react'; | ||
import {Stringify} from 'shared-runtime'; | ||
|
||
/** | ||
* TODO: we're currently bailing out because `contextVar` is a context variable | ||
* and not recorded into the PropagateScopeDeps LoadLocal / PropertyLoad | ||
* sidemap. Previously, we were able to avoid this as `BuildHIR` hoisted | ||
* `LoadContext` and `PropertyLoad` instructions into the outer function, which | ||
* we took as eligible dependencies. | ||
* | ||
* One solution is to simply record `LoadContext` identifiers into the | ||
* temporaries sidemap when the instruction occurs *after* the context | ||
* variable's mutable range. | ||
*/ | ||
function Foo(props) { | ||
let contextVar; | ||
if (props.cond) { | ||
contextVar = {val: 2}; | ||
} else { | ||
contextVar = {}; | ||
} | ||
|
||
const cb = useCallback(() => [contextVar.val], [contextVar.val]); | ||
|
||
return <Stringify cb={cb} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [{cond: true}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
22 | } | ||
23 | | ||
> 24 | const cb = useCallback(() => [contextVar.val], [contextVar.val]); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected (24:24) | ||
25 | | ||
26 | return <Stringify cb={cb} shouldInvokeFns={true} />; | ||
27 | } | ||
``` | ||
32 changes: 32 additions & 0 deletions
32
.../preserve-memo-validation/error.todo-useCallback-captures-reassigned-context-property.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @validatePreserveExistingMemoizationGuarantees | ||
import {useCallback} from 'react'; | ||
import {Stringify} from 'shared-runtime'; | ||
|
||
/** | ||
* TODO: we're currently bailing out because `contextVar` is a context variable | ||
* and not recorded into the PropagateScopeDeps LoadLocal / PropertyLoad | ||
* sidemap. Previously, we were able to avoid this as `BuildHIR` hoisted | ||
* `LoadContext` and `PropertyLoad` instructions into the outer function, which | ||
* we took as eligible dependencies. | ||
* | ||
* One solution is to simply record `LoadContext` identifiers into the | ||
* temporaries sidemap when the instruction occurs *after* the context | ||
* variable's mutable range. | ||
*/ | ||
function Foo(props) { | ||
let contextVar; | ||
if (props.cond) { | ||
contextVar = {val: 2}; | ||
} else { | ||
contextVar = {}; | ||
} | ||
|
||
const cb = useCallback(() => [contextVar.val], [contextVar.val]); | ||
|
||
return <Stringify cb={cb} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [{cond: true}], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 0 additions & 81 deletions
81
...erve-memo-validation/useCallback-captures-reassigned-context-property.expect.md
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
...es/compiler/preserve-memo-validation/useCallback-captures-reassigned-context-property.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.