-
Notifications
You must be signed in to change notification settings - Fork 47.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false positive on preserving memo of non-escaping values
Fixes the false positive in the previous PR. When we prune a scope because it's values are non-escaping, we now also remove any `Memoize` instructions for that scope. The intuition being that we're actively removing unnecessary memoization, so we don't need to check that the memoization occurred anymore.
- Loading branch information
1 parent
8e4d2fb
commit 0c86667
Showing
4 changed files
with
112 additions
and
30 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
28 changes: 0 additions & 28 deletions
28
...ler/error.repro-false-positive-preserve-memoization-nonescaping-value.expect.md
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
.../compiler/repro-false-positive-preserve-memoization-nonescaping-value.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,71 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions | ||
import { useCallback } from "react"; | ||
|
||
function Component({ entity, children }) { | ||
// showMessage doesn't escape so we don't memoize it. | ||
// However, validatePreserveExistingMemoizationGuarantees only sees that the scope | ||
// doesn't exist, and thinks the memoization was missed instead of being intentionally dropped. | ||
const showMessage = useCallback(() => entity != null); | ||
|
||
if (!showMessage()) { | ||
return children; | ||
} | ||
|
||
return <div>{children}</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [ | ||
{ | ||
entity: { name: "Sathya" }, | ||
children: [<div key="gsathya">Hi Sathya!</div>], | ||
}, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions | ||
import { useCallback, unstable_useMemoCache as useMemoCache } from "react"; | ||
|
||
function Component(t23) { | ||
const $ = useMemoCache(2); | ||
const { entity, children } = t23; | ||
|
||
const showMessage = () => entity != null; | ||
if (!showMessage()) { | ||
return children; | ||
} | ||
let t0; | ||
if ($[0] !== children) { | ||
t0 = <div>{children}</div>; | ||
$[0] = children; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [ | ||
{ | ||
entity: { name: "Sathya" }, | ||
children: [<div key="gsathya">Hi Sathya!</div>], | ||
}, | ||
], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div><div>Hi Sathya!</div></div> |
16 changes: 14 additions & 2 deletions
16
...preserve-memoization-nonescaping-value.js → ...preserve-memoization-nonescaping-value.js
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 |
---|---|---|
@@ -1,13 +1,25 @@ | ||
// @validatePreserveExistingMemoizationGuarantees @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions | ||
import { useCallback } from "react"; | ||
|
||
function Component({ entity, children }) { | ||
// showMessage doesn't escape so we don't memoize it. | ||
// However, validatePreserveExistingMemoizationGuarantees only sees that the scope | ||
// doesn't exist, and thinks the memoization was missed instead of being intentionally dropped. | ||
const showMessage = useCallback(() => entity != null); | ||
|
||
if (!showMessage) { | ||
if (!showMessage()) { | ||
return children; | ||
} | ||
|
||
return <Message>{children}</Message>; | ||
return <div>{children}</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [ | ||
{ | ||
entity: { name: "Sathya" }, | ||
children: [<div key="gsathya">Hi Sathya!</div>], | ||
}, | ||
], | ||
}; |