-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[compiler] Inferred effect dependencies now include optional chains #33326
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
Conversation
84347ec
to
53cc92d
Compare
When collecting scope dependencies, mark each dependency with `reactive: true | false`. This prepares for later PRs #33326 and #32099 which rewrite scope dependencies into instructions. Note that some reactive objects may have non-reactive properties, but we do not currently track this. Technically, state[0] is reactive and state[1] is not. Currently, both would be marked as reactive. ```js const state = useState(); ```
ab7d916
to
fe7e4c8
Compare
const builder = new HIRBuilder(env, { | ||
entryBlockKind: 'value', | ||
}); |
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.
Reusing HIRBuilder here since writing optional blocks needs the same reserve
/ enterReserved
logic as buildHIR
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.
Ahh this makes sense now, definitely makes it easier to construct the optionals
@@ -86,6 +92,7 @@ export function inferEffectDependencies(fn: HIRFunction): void { | |||
* reactive(Identifier i) = Union_{reference of i}(reactive(reference)) | |||
*/ | |||
const reactiveIds = inferReactiveIdentifiers(fn); | |||
const rewriteBlocks: Array<BasicBlock> = []; |
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.
We're now inserting new blocks into the function (see rewriteSplices
)
) { | ||
// exclude non-reactive hook results, which will never be in a memo block | ||
continue; | ||
} | ||
|
||
const {place, instructions} = writeDependencyToInstructions( | ||
const dep = truncateDepAtCurrent(maybeDep); |
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.
Moved this check from writeDependencyToInstructions
so that IDE highlighting and etc also receive the correct (truncated) dependency
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.
Nice, thank you!
IIUC, the IDE has access to all of the paths but we'll still need to look inside the effect body for property accesses/optional chains that correspond to deps, is that right?
And #32099 would make this easier because it would hoist the deps into temporaries that would be re-used inside the body.
0dfaec8
to
1a5a050
Compare
fn.env.hasInferredEffect = true; | ||
} | ||
} | ||
|
||
function writeDependencyToInstructions( |
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.
This was moved to ScopeDependencyUtils
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.
Super cool
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.
Wooo! this rocks
@@ -4,6 +4,12 @@ import {print} from 'shared-runtime'; | |||
|
|||
// TODO: take optional chains as dependencies |
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.
You can remove the todo now :D
import {useEffect} from 'react'; | ||
import {print, shallowCopy} from 'shared-runtime'; | ||
|
||
// TODO: take optional chains as dependencies |
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.
You can drop this now!
import {useEffect} from 'react'; | ||
import {print, shallowCopy} from 'shared-runtime'; | ||
|
||
// TODO: take optional chains as dependencies |
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.
And this one!
) { | ||
// exclude non-reactive hook results, which will never be in a memo block | ||
continue; | ||
} | ||
|
||
const {place, instructions} = writeDependencyToInstructions( | ||
const dep = truncateDepAtCurrent(maybeDep); |
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.
Nice, thank you!
IIUC, the IDE has access to all of the paths but we'll still need to look inside the effect body for property accesses/optional chains that correspond to deps, is that right?
And #32099 would make this easier because it would hoist the deps into temporaries that would be re-used inside the body.
When collecting scope dependencies, mark each dependency with `reactive: true | false`. This prepares for later PRs #33326 and #32099 which rewrite scope dependencies into instructions. Note that some reactive objects may have non-reactive properties, but we do not currently track this. Technically, state[0] is reactive and state[1] is not. Currently, both would be marked as reactive. ```js const state = useState(); ```
Inferred effect dependencies now include optional chains. This is a temporary solution while #32099 and its followups are worked on. Ideally, we should model reactive scope dependencies in the IR similarly to `ComputeIR` -- dependencies should be hoisted and all references rewritten to use the hoisted dependencies. `
--- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/32286). * #33326 * #33325 * __->__ #32286
When collecting scope dependencies, mark each dependency with `reactive: true | false`. This prepares for later PRs #33326 and #32099 which rewrite scope dependencies into instructions. Note that some reactive objects may have non-reactive properties, but we do not currently track this. Technically, state[0] is reactive and state[1] is not. Currently, both would be marked as reactive. ```js const state = useState(); ``` --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/33325). * #33326 * __->__ #33325 * #32286
--- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/32286). * #33326 * #33325 * __->__ #32286 DiffTrain build for [13f2004](13f2004)
--- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/32286). * #33326 * #33325 * __->__ #32286 DiffTrain build for [13f2004](13f2004)
…33326) Inferred effect dependencies now include optional chains. This is a temporary solution while #32099 and its followups are worked on. Ideally, we should model reactive scope dependencies in the IR similarly to `ComputeIR` -- dependencies should be hoisted and all references rewritten to use the hoisted dependencies. ` --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/33326). * __->__ #33326 * #33325 * #32286 DiffTrain build for [0d07288](0d07288)
Inferred effect dependencies now include optional chains.
This is a temporary solution while #32099 and its followups are worked on. Ideally, we should model reactive scope dependencies in the IR similarly to
ComputeIR
-- dependencies should be hoisted and all references rewritten to use the hoisted dependencies.`
Stack created with Sapling. Best reviewed with ReviewStack.