Skip to content

Commit d876c7d

Browse files
committed
compiler: Infer dependencies for pruned scopes
[ghstack-poisoned]
1 parent d9b212d commit d876c7d

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PropagateScopeDependencies.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isUseRefType,
1919
makeInstructionId,
2020
Place,
21+
PrunedReactiveScopeBlock,
2122
ReactiveFunction,
2223
ReactiveInstruction,
2324
ReactiveScope,
@@ -687,6 +688,16 @@ class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
687688
scope.scope.dependencies = scopeDependencies;
688689
}
689690

691+
override visitPrunedScope(
692+
scopeBlock: PrunedReactiveScopeBlock,
693+
context: Context
694+
): void {
695+
const scopeDependencies = context.enter(scopeBlock.scope, () => {
696+
this.visitBlock(scopeBlock.instructions, context);
697+
});
698+
scopeBlock.scope.dependencies = scopeDependencies;
699+
}
700+
690701
override visitInstruction(
691702
instruction: ReactiveInstruction,
692703
context: Context

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonReactiveDependencies.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
import {
99
IdentifierId,
10+
PrunedReactiveScopeBlock,
1011
ReactiveFunction,
1112
ReactiveInstruction,
13+
ReactiveScope,
1214
ReactiveScopeBlock,
1315
isSetStateType,
1416
} from "../HIR";
@@ -95,23 +97,34 @@ class Visitor extends ReactiveFunctionVisitor<ReactiveIdentifiers> {
9597
state: ReactiveIdentifiers
9698
): void {
9799
this.traverseScope(scopeBlock, state);
98-
for (const dep of scopeBlock.scope.dependencies) {
100+
this.visitScopeImpl(scopeBlock.scope, state);
101+
}
102+
103+
override visitPrunedScope(
104+
scopeBlock: PrunedReactiveScopeBlock,
105+
state: ReactiveIdentifiers
106+
): void {
107+
this.traversePrunedScope(scopeBlock, state);
108+
}
109+
110+
visitScopeImpl(scope: ReactiveScope, state: ReactiveIdentifiers): void {
111+
for (const dep of scope.dependencies) {
99112
const isReactive = state.has(dep.identifier.id);
100113
if (!isReactive) {
101-
scopeBlock.scope.dependencies.delete(dep);
114+
scope.dependencies.delete(dep);
102115
}
103116
}
104-
if (scopeBlock.scope.dependencies.size !== 0) {
117+
if (scope.dependencies.size !== 0) {
105118
/**
106119
* If any of a scope's dependencies are reactive, then all of its
107120
* outputs will re-evaluate whenever those dependencies change.
108121
* Mark all of the outputs as reactive to reflect the fact that
109122
* they may change in practice based on a reactive input.
110123
*/
111-
for (const [, declaration] of scopeBlock.scope.declarations) {
124+
for (const [, declaration] of scope.declarations) {
112125
state.add(declaration.identifier.id);
113126
}
114-
for (const reassignment of scopeBlock.scope.reassignments) {
127+
for (const reassignment of scope.reassignments) {
115128
state.add(reassignment.id);
116129
}
117130
}

0 commit comments

Comments
 (0)