mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
d5c3fb87e6
See context from #2187 for background about control dependencies. Our current `PruneNonReactiveIdentifiers` pass runs on ReactiveFunction, after scope construction, and removes scope dependencies that aren't reactive. It works by first building up a set of reactive identifiers in `InferReactiveIdentifiers`, then walking the ReactiveFunction and pruning any scope dependencies that aren't in that set. The challenge is control variables, as demonstrated by the test cases in #2184. `InferReactiveIdentifiers` runs against ReactiveFunction, and when we initially wrote it we didn't consider control variables. To handle control variables we really need to use precise control- & data-flow analysis, which is much easier with HIR. This PR adds the start of `InferReactivePlaces`, which annotates each `Place` with whether it is reactive or not. This allows the annotation to survive LeaveSSA, which swaps out the identifiers of places but leaves other properties as-is. This version does _not_ yet handle control variables, but it's already more precise than our existing inference. In our current inference, if `x` is ever assigned a reactive value, then all `x`s are marked reactive. In our new inference, each instance of `x` (each Place) gets a separate flag based on whether x can actually be reactive at that point in the program. There are two main next steps (in follow-up PRs): * Update the mechanism by which we prune non-reactive dependencies from scopes. * Handle control variables. I think we may be able to use dominator trees to figure out the set of basic blocks whose reachability is gated by the control variables. This should clearly work for if/else and switch, as for loops i'm not sure but intuitively it seems right.