@samitouri / QOS-React-1 / commits / b34172c11e

Reactivity inference is single-pass when no loops

We can complete in a single-pass if there are no loops, as a performance optimization.

Joe Savona committed Nov 1, 2023 at 17:13 UTC b34172c11e29cae21950e0f9851e9893c9b8f3d8
3 files changed +12 -12
compiler/packages/babel-plugin-react-forget/src/Inference/InferReactivePlaces.ts
+3 -1
@@ -22,6 +22,7 @@ import {
22 eachInstructionValueOperand,
23 eachTerminalOperand,
24 } from "../HIR/visitors";
25 +import { hasBackEdge } from "../Optimization/DeadCodeElimination";
26 import { assertExhaustive } from "../Utils/utils";
27
28 /**
@@ -94,6 +95,7 @@ export function inferReactivePlaces(fn: HIRFunction): void {
95 includeThrowsAsExitNode: false,
96 });
97 const postDominatorFrontierCache = new Map<BlockId, Set<BlockId>>();
98 + const hasLoop = hasBackEdge(fn);
99 do {
100 const identifierMapping = new Map<Identifier, Identifier>();
101 for (const [, block] of fn.body.blocks) {
@@ -252,7 +254,7 @@ export function inferReactivePlaces(fn: HIRFunction): void {
254 reactiveIdentifiers.isReactive(operand);
255 }
256 }
255 - } while (reactiveIdentifiers.snapshot());
257 + } while (reactiveIdentifiers.snapshot() && hasLoop);
258 }
259
260 /**
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.expect.md
+4 -5
@@ -14,7 +14,7 @@ function Component(props) {
14 ```javascript
15 import { unstable_useMemoCache as useMemoCache } from "react";
16 function Component(props) {
17 - const $ = useMemoCache(4);
17 + const $ = useMemoCache(3);
18 let t0;
19 if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
20 t0 = makeObject();
@@ -24,13 +24,12 @@ function Component(props) {
24 }
25 const x = t0;
26 let t1;
27 - if ($[1] !== props || $[2] !== x) {
27 + if ($[1] !== props) {
28 t1 = x?.[foo(props.value)];
29 $[1] = props;
30 - $[2] = x;
31 - $[3] = t1;
30 + $[2] = t1;
31 } else {
33 - t1 = $[3];
32 + t1 = $[2];
33 }
34 return t1;
35 }
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.expect.md
+5 -6
@@ -14,7 +14,7 @@ function Component(props) {
14 ```javascript
15 import { unstable_useMemoCache as useMemoCache } from "react";
16 function Component(props) {
17 - const $ = useMemoCache(4);
17 + const $ = useMemoCache(3);
18 let t0;
19 if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
20 t0 = makeObject();
@@ -24,13 +24,12 @@ function Component(props) {
24 }
25 const x = t0;
26 let t1;
27 - if ($[1] !== x.y || $[2] !== props) {
27 + if ($[1] !== props) {
28 t1 = x.y?.[props.a?.[props.b?.[props.c]]];
29 - $[1] = x.y;
30 - $[2] = props;
31 - $[3] = t1;
29 + $[1] = props;
30 + $[2] = t1;
31 } else {
33 - t1 = $[3];
32 + t1 = $[2];
33 }
34 return t1;
35 }