Skip to content

[compiler] Add reactive flag on scope dependencies #33325

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

Merged
merged 2 commits into from
May 22, 2025
Merged

[compiler] Add reactive flag on scope dependencies #33325

merged 2 commits into from
May 22, 2025

Conversation

mofeiZ
Copy link
Contributor

@mofeiZ mofeiZ commented May 21, 2025

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.

const state = useState();

Stack created with Sapling. Best reviewed with ReviewStack.

Copy link
Contributor

@jbrown215 jbrown215 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! How effective was TS at pointing you to where you needed to propagate this flag?

@mofeiZ mofeiZ marked this pull request as ready for review May 21, 2025 18:52
@mofeiZ
Copy link
Contributor Author

mofeiZ commented May 21, 2025

Looks good! How effective was TS at pointing you to where you needed to propagate this flag?

It was pretty simple to thread this flag through! I've noticed that TS is generally pretty good on giving few errors (not many false positives from inferred types) + fairly accurate source locations, even when I have a file that doesn't parse completely (e.g. mid-edit for a block / expression)

Copy link
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, just one question

@@ -381,7 +393,7 @@ function collectNonNullsInBlocks(
) {
const identifier = fn.params[0].identifier;
knownNonNullIdentifiers.add(
context.registry.getOrCreateIdentifier(identifier),
context.registry.getOrCreateIdentifier(identifier, true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, true bc this is the props object

@@ -375,6 +376,41 @@ export function inferReactivePlaces(fn: HIRFunction): void {
}
}
} while (reactiveIdentifiers.snapshot());

function propagateReactivityToInnerFunctions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hoist this to a top-level function?

@@ -456,6 +456,7 @@ function canMergeScopes(
new Set(
[...current.scope.declarations.values()].map(declaration => ({
identifier: declaration.identifier,
reactive: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should areEqualDependencies() update to look at reactivity?

Copy link

@Hardanish-Singh Hardanish-Singh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

mofeiZ added 2 commits May 22, 2025 15:44
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();
```
mofeiZ added a commit that referenced this pull request May 22, 2025
---
[//]: # (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
@mofeiZ mofeiZ merged commit abf9fd5 into main May 22, 2025
23 of 29 checks passed
mofeiZ added a commit that referenced this pull request May 22, 2025
…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
github-actions bot pushed a commit that referenced this pull request May 22, 2025
---
[//]: # (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)
github-actions bot pushed a commit that referenced this pull request May 22, 2025
---
[//]: # (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)
github-actions bot pushed a commit that referenced this pull request May 22, 2025
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants