Prune declarations not used after merging
This is the optimization mentioned in #2113. When we merge scopes, often the declarations from the first scope become unnecessary, since those values are only consumed by the subsequent, now-merged scope. There's no point emitting those values as outputs of the merged scope since no one can consume them. Thanks to the logic in #2116 we now know the last place each identifier is used. We use that again here, to prune declarations that aren't used past the end of the merged scope. This is a pretty dramatic win on cache slots used.
Joe Savona committed
Sep 27, 2023 at 14:04 UTC
66841aa9ac59d4312f42e41cd8e39158765c5eae
49 files changed
+359
-569
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/MergeConsecutiveScopes.ts
+25
@@ -169,6 +169,9 @@ class Transform extends ReactiveFunctionTransform<void> {
169
areLValuesLastUsedByScope(instr.scope, lvalues, this.lastUsage)
170
) {
171
const intermediateInstructions = block.slice(currentScope.to, i);
172
+ currentScope.scope.scope.range.end = makeInstructionId(
173
+ Math.max(currentScope.scope.scope.range.end, instr.scope.range.end)
174
+ );
175
currentScope.scope.instructions.push(...intermediateInstructions);
176
currentScope.scope.instructions.push(...instr.instructions);
177
for (const [key, value] of instr.scope.declarations) {
@@ -193,12 +196,34 @@ class Transform extends ReactiveFunctionTransform<void> {
196
}
197
198
if (nextInstructions !== null) {
199
+ for (const instr of nextInstructions) {
200
+ if (instr.kind === "scope") {
201
+ updateScopeDeclarations(instr.scope, this.lastUsage);
202
+ }
203
+ }
204
+
205
block.length = 0;
206
block.push(...nextInstructions);
207
}
208
}
209
}
210
211
+/**
212
+ * Updates @param scope's declarations to remove any declarations that are not
213
+ * used after the scope, based on the scope's updated range post-merging.
214
+ */
215
+function updateScopeDeclarations(
216
+ scope: ReactiveScope,
217
+ lastUsage: Map<IdentifierId, InstructionId>
218
+): void {
219
+ for (const [key] of scope.declarations) {
220
+ const lastUsedAt = lastUsage.get(key)!;
221
+ if (lastUsedAt < scope.range.end) {
222
+ scope.declarations.delete(key);
223
+ }
224
+ }
225
+}
226
+
227
/**
228
* Returns whether the given @param scope is the last usage of all
229
* the given @param lvalues. Returns false if any of the lvalues
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver-and-mutate.expect.md
+5
-11
@@ -31,25 +31,19 @@ import { unstable_useMemoCache as useMemoCache } from "react";
31
import { makeObject_Primitives, mutate } from "shared-runtime";
32
33
function Component() {
34
- const $ = useMemoCache(3);
35
- let x;
36
- let a;
34
+ const $ = useMemoCache(1);
35
let t0;
36
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
39
- a = makeObject_Primitives();
37
+ const a = makeObject_Primitives();
38
41
- x = [];
39
+ const x = [];
40
x.push(a);
41
42
mutate(x);
43
t0 = [x, a];
46
- $[0] = x;
47
- $[1] = a;
48
- $[2] = t0;
44
+ $[0] = t0;
45
} else {
50
- x = $[0];
51
- a = $[1];
52
- t0 = $[2];
46
+ t0 = $[0];
47
}
48
return t0;
49
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver.expect.md
+4
-7
@@ -20,7 +20,7 @@ function Component() {
20
```javascript
21
import { unstable_useMemoCache as useMemoCache } from "react";
22
function Component() {
23
- const $ = useMemoCache(3);
23
+ const $ = useMemoCache(2);
24
let t0;
25
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
26
t0 = someObj();
@@ -29,18 +29,15 @@ function Component() {
29
t0 = $[0];
30
}
31
const a = t0;
32
- let x;
32
let t1;
33
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
35
- x = [];
34
+ const x = [];
35
x.push(a);
36
37
t1 = [x, a];
39
- $[1] = x;
40
- $[2] = t1;
38
+ $[1] = t1;
39
} else {
42
- x = $[1];
43
- t1 = $[2];
40
+ t1 = $[1];
41
}
42
return t1;
43
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md
+4
-7
@@ -24,7 +24,7 @@ export const FIXTURE_ENTRYPOINT = {
24
```javascript
25
import { unstable_useMemoCache as useMemoCache } from "react";
26
function component() {
27
- const $ = useMemoCache(3);
27
+ const $ = useMemoCache(2);
28
let t0;
29
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
30
t0 = [];
@@ -33,18 +33,15 @@ function component() {
33
t0 = $[0];
34
}
35
const z = t0;
36
- let y;
36
let x;
37
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
39
- y = {};
38
+ const y = {};
39
y.z = z;
40
x = {};
41
x.y = y;
43
- $[1] = y;
44
- $[2] = x;
42
+ $[1] = x;
43
} else {
46
- y = $[1];
47
- x = $[2];
44
+ x = $[1];
45
}
46
return x;
47
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/allocating-primitive-as-dep-nested-scope.expect.md
+10
-16
@@ -24,37 +24,31 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // bar(props.b) i
24
// Correctness:
25
// - y depends on either bar(props.b) or bar(props.b) + 1
26
function AllocatingPrimitiveAsDepNested(props) {
27
- const $ = useMemoCache(7);
27
+ const $ = useMemoCache(5);
28
const c_0 = $[0] !== props.b;
29
const c_1 = $[1] !== props.a;
30
- let x;
31
- let y;
30
let t2;
31
if (c_0 || c_1) {
34
- x = {};
32
+ const x = {};
33
mutate(x);
34
const t0 = bar(props.b) + 1;
37
- const c_5 = $[5] !== t0;
35
+ const c_3 = $[3] !== t0;
36
let t1;
39
- if (c_5) {
37
+ if (c_3) {
38
t1 = foo(t0);
41
- $[5] = t0;
42
- $[6] = t1;
39
+ $[3] = t0;
40
+ $[4] = t1;
41
} else {
44
- t1 = $[6];
42
+ t1 = $[4];
43
}
46
- y = t1;
44
+ const y = t1;
45
mutate(x, props.a);
46
t2 = [x, y];
47
$[0] = props.b;
48
$[1] = props.a;
51
- $[2] = x;
52
- $[3] = y;
53
- $[4] = t2;
49
+ $[2] = t2;
50
} else {
55
- x = $[2];
56
- y = $[3];
57
- t2 = $[4];
51
+ t2 = $[2];
52
}
53
return t2;
54
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-access-assignment.expect.md
+10
-16
@@ -24,39 +24,33 @@ export const FIXTURE_ENTRYPOINT = {
24
```javascript
25
import { unstable_useMemoCache as useMemoCache } from "react";
26
function foo(a, b, c) {
27
- const $ = useMemoCache(8);
27
+ const $ = useMemoCache(6);
28
const c_0 = $[0] !== a;
29
const c_1 = $[1] !== b;
30
const c_2 = $[2] !== c;
31
- let x;
32
- let z;
31
let t1;
32
if (c_0 || c_1 || c_2) {
35
- x = [a];
36
- const c_6 = $[6] !== b;
33
+ const x = [a];
34
+ const c_4 = $[4] !== b;
35
let t0;
38
- if (c_6) {
36
+ if (c_4) {
37
t0 = [null, b];
40
- $[6] = b;
41
- $[7] = t0;
38
+ $[4] = b;
39
+ $[5] = t0;
40
} else {
43
- t0 = $[7];
41
+ t0 = $[5];
42
}
43
const y = t0;
46
- z = [[], [], [c]];
44
+ const z = [[], [], [c]];
45
x[0] = y[1];
46
z[0][0] = x[0];
47
t1 = [x, z];
48
$[0] = a;
49
$[1] = b;
50
$[2] = c;
53
- $[3] = x;
54
- $[4] = z;
55
- $[5] = t1;
51
+ $[3] = t1;
52
} else {
57
- x = $[3];
58
- z = $[4];
59
- t1 = $[5];
53
+ t1 = $[3];
54
}
55
return t1;
56
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-effect.expect.md
+20
-24
@@ -22,42 +22,38 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // arrayInstance.
22
// - read on receiver
23
// - mutate on lvalue
24
function ArrayAtTest(props) {
25
- const $ = useMemoCache(8);
25
+ const $ = useMemoCache(7);
26
const c_0 = $[0] !== props.x;
27
let t0;
28
- let t1;
28
if (c_0) {
30
- t0 = foo(props.x);
31
- t1 = [t0];
29
+ t0 = [foo(props.x)];
30
$[0] = props.x;
31
$[1] = t0;
34
- $[2] = t1;
32
} else {
33
t0 = $[1];
37
- t1 = $[2];
34
}
39
- const arr = t1;
40
- const c_3 = $[3] !== props.y;
41
- const c_4 = $[4] !== arr;
42
- let t3;
43
- if (c_3 || c_4) {
44
- const c_6 = $[6] !== props.y;
45
- let t2;
46
- if (c_6) {
47
- t2 = bar(props.y);
48
- $[6] = props.y;
49
- $[7] = t2;
35
+ const arr = t0;
36
+ const c_2 = $[2] !== props.y;
37
+ const c_3 = $[3] !== arr;
38
+ let t2;
39
+ if (c_2 || c_3) {
40
+ const c_5 = $[5] !== props.y;
41
+ let t1;
42
+ if (c_5) {
43
+ t1 = bar(props.y);
44
+ $[5] = props.y;
45
+ $[6] = t1;
46
} else {
51
- t2 = $[7];
47
+ t1 = $[6];
48
}
53
- t3 = arr.at(t2);
54
- $[3] = props.y;
55
- $[4] = arr;
56
- $[5] = t3;
49
+ t2 = arr.at(t1);
50
+ $[2] = props.y;
51
+ $[3] = arr;
52
+ $[4] = t2;
53
} else {
58
- t3 = $[5];
54
+ t2 = $[4];
55
}
60
- const result = t3;
56
+ const result = t2;
57
return result;
58
}
59
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-mutate-after-capture.expect.md
+3
-7
@@ -21,19 +21,15 @@ function Component(props) {
21
import { unstable_useMemoCache as useMemoCache } from "react"; // x's mutable range should extend to `mutate(y)`
22
23
function Component(props) {
24
- const $ = useMemoCache(2);
24
+ const $ = useMemoCache(1);
25
let t0;
26
- let t1;
26
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
28
- t0 = {};
29
- t1 = [42, t0];
27
+ t0 = [42, {}];
28
$[0] = t0;
31
- $[1] = t1;
29
} else {
30
t0 = $[0];
34
- t1 = $[1];
31
}
36
- const x = t1;
32
+ const x = t0;
33
const idx = foo(props.b);
34
const y = x.at(idx);
35
mutate(y);
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations-complex-lvalue-array.expect.md
+3
-7
@@ -22,19 +22,15 @@ export const FIXTURE_ENTRYPOINT = {
22
```javascript
23
import { unstable_useMemoCache as useMemoCache } from "react";
24
function foo() {
25
- const $ = useMemoCache(2);
25
+ const $ = useMemoCache(1);
26
let t0;
27
- let t1;
27
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
29
- t0 = [1];
30
- t1 = [t0];
28
+ t0 = [[1]];
29
$[0] = t0;
32
- $[1] = t1;
30
} else {
31
t0 = $[0];
35
- t1 = $[1];
32
}
37
- const a = t1;
33
+ const a = t0;
34
const first = a.at(0);
35
first.set(0, 2);
36
return a;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/builtin-jsx-tag-lowered-between-mutations.expect.md
+3
-7
@@ -14,20 +14,16 @@ function Component(props) {
14
```javascript
15
import { unstable_useMemoCache as useMemoCache } from "react";
16
function Component(props) {
17
- const $ = useMemoCache(2);
17
+ const $ = useMemoCache(1);
18
let t0;
19
- let t1;
19
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
20
const maybeMutable = new MaybeMutable();
22
- t0 = maybeMutate(maybeMutable);
23
- t1 = <div>{t0}</div>;
21
+ t0 = <div>{maybeMutate(maybeMutable)}</div>;
22
$[0] = t0;
25
- $[1] = t1;
23
} else {
24
t0 = $[0];
28
- t1 = $[1];
25
}
30
- return t1;
26
+ return t0;
27
}
28
29
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call-with-independently-memoizable-arg.expect.md
+11
-11
@@ -19,31 +19,31 @@ function Component(props) {
19
```javascript
20
import { unstable_useMemoCache as useMemoCache } from "react";
21
function Component(props) {
22
- const $ = useMemoCache(5);
22
+ const $ = useMemoCache(4);
23
const c_0 = $[0] !== props;
24
- let t2;
24
+ let t1;
25
if (c_0) {
26
const x = makeFunction(props);
27
const c_2 = $[2] !== props.text;
28
let t0;
29
- let t1;
29
if (c_2) {
31
- t0 = <span>{props.text}</span>;
32
- t1 = <div>{t0}</div>;
30
+ t0 = (
31
+ <div>
32
+ <span>{props.text}</span>
33
+ </div>
34
+ );
35
$[2] = props.text;
36
$[3] = t0;
35
- $[4] = t1;
37
} else {
38
t0 = $[3];
38
- t1 = $[4];
39
}
40
- t2 = x(t1);
40
+ t1 = x(t0);
41
$[0] = props;
42
- $[1] = t2;
42
+ $[1] = t1;
43
} else {
44
- t2 = $[1];
44
+ t1 = $[1];
45
}
46
- const y = t2;
46
+ const y = t1;
47
return y;
48
}
49
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call.expect.md
+5
-11
@@ -22,24 +22,18 @@ import { unstable_useMemoCache as useMemoCache } from "react";
22
function foo() {}
23
24
function Component(props) {
25
- const $ = useMemoCache(3);
26
- let a;
27
- let b;
25
+ const $ = useMemoCache(1);
26
let t0;
27
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
30
- a = [];
31
- b = {};
28
+ const a = [];
29
+ const b = {};
30
foo(a, b);
31
32
foo(b);
33
t0 = <div a={a} b={b} />;
36
- $[0] = a;
37
- $[1] = b;
38
- $[2] = t0;
34
+ $[0] = t0;
35
} else {
40
- a = $[0];
41
- b = $[1];
42
- t0 = $[2];
36
+ t0 = $[0];
37
}
38
return t0;
39
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md
+3
-7
@@ -23,21 +23,17 @@ export const FIXTURE_ENTRYPOINT = {
23
```javascript
24
import { unstable_useMemoCache as useMemoCache } from "react";
25
function component(a) {
26
- const $ = useMemoCache(3);
26
+ const $ = useMemoCache(2);
27
const c_0 = $[0] !== a;
28
let t0;
29
- let t1;
29
if (c_0) {
31
- t0 = { a };
32
- t1 = { a: t0 };
30
+ t0 = { a: { a } };
31
$[0] = a;
32
$[1] = t0;
35
- $[2] = t1;
33
} else {
34
t0 = $[1];
38
- t1 = $[2];
35
}
40
- const z = t1;
36
+ const z = t0;
37
return z;
38
}
39
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md
+11
-15
@@ -25,35 +25,31 @@ export const FIXTURE_ENTRYPOINT = {
25
```javascript
26
import { unstable_useMemoCache as useMemoCache } from "react";
27
function component(a) {
28
- const $ = useMemoCache(5);
28
+ const $ = useMemoCache(4);
29
const c_0 = $[0] !== a;
30
let t0;
31
- let t1;
31
if (c_0) {
33
- t0 = { a };
34
- t1 = { a: t0 };
32
+ t0 = { a: { a } };
33
$[0] = a;
34
$[1] = t0;
37
- $[2] = t1;
35
} else {
36
t0 = $[1];
40
- t1 = $[2];
37
}
42
- const z = t1;
43
- const c_3 = $[3] !== z.a.a;
44
- let t2;
45
- if (c_3) {
46
- t2 = function () {
38
+ const z = t0;
39
+ const c_2 = $[2] !== z.a.a;
40
+ let t1;
41
+ if (c_2) {
42
+ t1 = function () {
43
(function () {
44
console.log(z.a.a);
45
})();
46
};
51
- $[3] = z.a.a;
52
- $[4] = t2;
47
+ $[2] = z.a.a;
48
+ $[3] = t1;
49
} else {
54
- t2 = $[4];
50
+ t1 = $[3];
51
}
56
- const x = t2;
52
+ const x = t1;
53
return x;
54
}
55
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md
+11
-15
@@ -23,33 +23,29 @@ export const FIXTURE_ENTRYPOINT = {
23
```javascript
24
import { unstable_useMemoCache as useMemoCache } from "react";
25
function component(a) {
26
- const $ = useMemoCache(5);
26
+ const $ = useMemoCache(4);
27
const c_0 = $[0] !== a;
28
let t0;
29
- let t1;
29
if (c_0) {
31
- t0 = { a };
32
- t1 = { a: t0 };
30
+ t0 = { a: { a } };
31
$[0] = a;
32
$[1] = t0;
35
- $[2] = t1;
33
} else {
34
t0 = $[1];
38
- t1 = $[2];
35
}
40
- const z = t1;
41
- const c_3 = $[3] !== z.a.a;
42
- let t2;
43
- if (c_3) {
44
- t2 = function () {
36
+ const z = t0;
37
+ const c_2 = $[2] !== z.a.a;
38
+ let t1;
39
+ if (c_2) {
40
+ t1 = function () {
41
console.log(z.a.a);
42
};
47
- $[3] = z.a.a;
48
- $[4] = t2;
43
+ $[2] = z.a.a;
44
+ $[3] = t1;
45
} else {
50
- t2 = $[4];
46
+ t1 = $[3];
47
}
52
- const x = t2;
48
+ const x = t1;
49
return x;
50
}
51
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md
+4
-7
@@ -23,13 +23,12 @@ import { makeReadOnly } from "react-forget-runtime";
23
import { unstable_useMemoCache as useMemoCache } from "react"; // @enableEmitFreeze true
24
25
function MyComponentName(props) {
26
- const $ = useMemoCache(4);
26
+ const $ = useMemoCache(3);
27
const c_0 = $[0] !== props.a;
28
const c_1 = $[1] !== props.b;
29
- let x;
29
let y;
30
if (c_0 || c_1) {
32
- x = {};
31
+ const x = {};
32
foo(x, props.a);
33
foo(x, props.b);
34
@@ -37,11 +36,9 @@ function MyComponentName(props) {
36
y.push(x);
37
$[0] = props.a;
38
$[1] = props.b;
40
- $[2] = __DEV__ ? makeReadOnly(x, "MyComponentName") : x;
41
- $[3] = __DEV__ ? makeReadOnly(y, "MyComponentName") : y;
39
+ $[2] = __DEV__ ? makeReadOnly(y, "MyComponentName") : y;
40
} else {
43
- x = $[2];
44
- y = $[3];
41
+ y = $[2];
42
}
43
return y;
44
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md
+4
-8
@@ -30,7 +30,7 @@ export const FIXTURE_ENTRYPOINT = {
30
```javascript
31
import { unstable_useMemoCache as useMemoCache } from "react"; // Should print A, B, arg, original
32
function Component() {
33
- const $ = useMemoCache(3);
33
+ const $ = useMemoCache(2);
34
let t0;
35
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
36
t0 = (o) => {
@@ -41,20 +41,16 @@ function Component() {
41
t0 = $[0];
42
}
43
const changeF = t0;
44
- let t1;
44
let x;
45
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
47
- t1 = () => console.log("original");
48
- x = { f: t1 };
46
+ x = { f: () => console.log("original") };
47
48
(console.log("A"), x)[(console.log("B"), "f")](
49
(changeF(x), console.log("arg"), 1)
50
);
53
- $[1] = t1;
54
- $[2] = x;
51
+ $[1] = x;
52
} else {
56
- t1 = $[1];
57
- x = $[2];
53
+ x = $[1];
54
}
55
return x;
56
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constructor.expect.md
+5
-11
@@ -22,23 +22,17 @@ import { unstable_useMemoCache as useMemoCache } from "react";
22
function Foo() {}
23
24
function Component(props) {
25
- const $ = useMemoCache(3);
26
- let a;
27
- let b;
25
+ const $ = useMemoCache(1);
26
let t0;
27
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
30
- a = [];
31
- b = {};
28
+ const a = [];
29
+ const b = {};
30
new Foo(a, b);
31
new Foo(b);
32
t0 = <div a={a} b={b} />;
35
- $[0] = a;
36
- $[1] = b;
37
- $[2] = t0;
33
+ $[0] = t0;
34
} else {
39
- a = $[0];
40
- b = $[1];
41
- t0 = $[2];
35
+ t0 = $[0];
36
}
37
return t0;
38
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.expect.md
+9
-9
@@ -29,27 +29,27 @@ import { unstable_useMemoCache as useMemoCache } from "react";
29
import fbt from "fbt";
30
31
export function Component(props) {
32
- const $ = useMemoCache(3);
32
+ const $ = useMemoCache(2);
33
let count = 0;
34
if (props.items) {
35
count = props.items.length;
36
}
37
const c_0 = $[0] !== count;
38
let t0;
39
- let t1;
39
if (c_0) {
41
- t0 = fbt._("for {count} experiences", [fbt._param("count", count)], {
42
- hk: "nmYpm",
43
- });
44
- t1 = <View>{t0}</View>;
40
+ t0 = (
41
+ <View>
42
+ {fbt._("for {count} experiences", [fbt._param("count", count)], {
43
+ hk: "nmYpm",
44
+ })}
45
+ </View>
46
+ );
47
$[0] = count;
48
$[1] = t0;
47
- $[2] = t1;
49
} else {
50
t0 = $[1];
50
- t1 = $[2];
51
}
52
- return t1;
52
+ return t0;
53
}
54
55
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-text-must-use-expression-container.expect.md
+7
-7
@@ -25,19 +25,19 @@ import { unstable_useMemoCache as useMemoCache } from "react";
25
import fbt from "fbt";
26
27
function Component(props) {
28
- const $ = useMemoCache(2);
28
+ const $ = useMemoCache(1);
29
let t0;
30
- let t1;
30
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
32
- t0 = fbt._("{value}%", [fbt._param("value", "0")], { hk: "10F5Cc" });
33
- t1 = <Foo value={t0} />;
31
+ t0 = (
32
+ <Foo
33
+ value={fbt._("{value}%", [fbt._param("value", "0")], { hk: "10F5Cc" })}
34
+ />
35
+ );
36
$[0] = t0;
35
- $[1] = t1;
37
} else {
38
t0 = $[0];
38
- t1 = $[1];
39
}
40
- return t1;
40
+ return t0;
41
}
42
43
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-element-content.expect.md
+27
-27
@@ -29,49 +29,49 @@ import { unstable_useMemoCache as useMemoCache } from "react";
29
import fbt from "fbt";
30
31
function Component(t39) {
32
- const $ = useMemoCache(5);
32
+ const $ = useMemoCache(4);
33
const { name, data, icon } = t39;
34
const c_0 = $[0] !== name;
35
const c_1 = $[1] !== icon;
36
const c_2 = $[2] !== data;
37
let t0;
38
- let t1;
38
if (c_0 || c_1 || c_2) {
40
- t0 = fbt._(
41
- "{item author}{icon}{=m2}",
42
- [
43
- fbt._param(
44
- "item author",
39
+ t0 = (
40
+ <Text type="body4">
41
+ {fbt._(
42
+ "{item author}{icon}{=m2}",
43
+ [
44
+ fbt._param(
45
+ "item author",
46
46
- <Text type="h4">{name}</Text>
47
- ),
48
- fbt._param(
49
- "icon",
47
+ <Text type="h4">{name}</Text>
48
+ ),
49
+ fbt._param(
50
+ "icon",
51
51
- icon
52
- ),
53
- fbt._implicitParam(
54
- "=m2",
55
- <Text type="h4">
56
- {fbt._("{item details}", [fbt._param("item details", data)], {
57
- hk: "4jLfVq",
58
- })}
59
- </Text>
60
- ),
61
- ],
62
- { hk: "2HLm2j" }
52
+ icon
53
+ ),
54
+ fbt._implicitParam(
55
+ "=m2",
56
+ <Text type="h4">
57
+ {fbt._("{item details}", [fbt._param("item details", data)], {
58
+ hk: "4jLfVq",
59
+ })}
60
+ </Text>
61
+ ),
62
+ ],
63
+ { hk: "2HLm2j" }
64
+ )}
65
+ </Text>
66
);
64
- t1 = <Text type="body4">{t0}</Text>;
67
$[0] = name;
68
$[1] = icon;
69
$[2] = data;
70
$[3] = t0;
69
- $[4] = t1;
71
} else {
72
t0 = $[3];
72
- t1 = $[4];
73
}
74
- return t1;
74
+ return t0;
75
}
76
77
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-fragment-value.expect.md
+11
-9
@@ -27,23 +27,25 @@ import fbt from "fbt";
27
import { identity } from "shared-runtime";
28
29
function Component(props) {
30
- const $ = useMemoCache(3);
30
+ const $ = useMemoCache(2);
31
const c_0 = $[0] !== props.text;
32
let t0;
33
- let t1;
33
if (c_0) {
35
- t0 = fbt._("{value}%", [fbt._param("value", <>{identity(props.text)}</>)], {
36
- hk: "10F5Cc",
37
- });
38
- t1 = <Foo value={t0} />;
34
+ t0 = (
35
+ <Foo
36
+ value={fbt._(
37
+ "{value}%",
38
+ [fbt._param("value", <>{identity(props.text)}</>)],
39
+ { hk: "10F5Cc" }
40
+ )}
41
+ />
42
+ );
43
$[0] = props.text;
44
$[1] = t0;
41
- $[2] = t1;
45
} else {
46
t0 = $[1];
44
- t1 = $[2];
47
}
46
- return t1;
48
+ return t0;
49
}
50
51
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-in-statement.expect.md
+4
-7
@@ -22,23 +22,20 @@ export const FIXTURE_ENTRYPOINT = {
22
```javascript
23
import { unstable_useMemoCache as useMemoCache } from "react";
24
function Component(props) {
25
- const $ = useMemoCache(3);
25
+ const $ = useMemoCache(2);
26
const c_0 = $[0] !== props;
27
- let items;
27
let t0;
28
if (c_0) {
30
- items = [];
29
+ const items = [];
30
for (const key in props) {
31
items.push(<div key={key}>{key}</div>);
32
}
33
34
t0 = <div>{items}</div>;
35
$[0] = props;
37
- $[1] = items;
38
- $[2] = t0;
36
+ $[1] = t0;
37
} else {
40
- items = $[1];
41
- t0 = $[2];
38
+ t0 = $[1];
39
}
40
return t0;
41
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md
+4
-7
@@ -28,22 +28,19 @@ import { unstable_useMemoCache as useMemoCache } from "react";
28
import { makeObject_Primitives, mutate, Stringify } from "shared-runtime";
29
30
function Component(_props) {
31
- const $ = useMemoCache(2);
32
- let results;
31
+ const $ = useMemoCache(1);
32
let t0;
33
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
34
const collection = [makeObject_Primitives()];
36
- results = [];
35
+ const results = [];
36
for (const item of collection) {
37
results.push(<div key={Stringify(item)}>{Stringify(mutate(item))}</div>);
38
}
39
40
t0 = <div>{results}</div>;
42
- $[0] = results;
43
- $[1] = t0;
41
+ $[0] = t0;
42
} else {
45
- results = $[0];
46
- t0 = $[1];
43
+ t0 = $[0];
44
}
45
return t0;
46
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-reassign.expect.md
+4
-8
@@ -23,21 +23,17 @@ export const FIXTURE_ENTRYPOINT = {
23
```javascript
24
import { unstable_useMemoCache as useMemoCache } from "react";
25
function component() {
26
- const $ = useMemoCache(2);
27
- let t0;
26
+ const $ = useMemoCache(1);
27
let x;
28
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
30
- t0 = function x(a) {
29
+ x = function x(a) {
30
a.foo();
31
};
33
- x = t0;
32
33
x = {};
36
- $[0] = t0;
37
- $[1] = x;
34
+ $[0] = x;
35
} else {
39
- t0 = $[0];
40
- x = $[1];
36
+ x = $[0];
37
}
38
return x;
39
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/global-jsx-tag-lowered-between-mutations.expect.md
+6
-14
@@ -25,25 +25,17 @@ function Component(props) {
25
```javascript
26
import { unstable_useMemoCache as useMemoCache } from "react";
27
function Component(props) {
28
- const $ = useMemoCache(3);
29
- let T0;
30
- let t1;
31
- let t2;
28
+ const $ = useMemoCache(1);
29
+ let t0;
30
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
31
const maybeMutable = new MaybeMutable();
32
35
- T0 = View;
36
- t1 = maybeMutate(maybeMutable);
37
- t2 = <T0>{t1}</T0>;
38
- $[0] = T0;
39
- $[1] = t1;
40
- $[2] = t2;
33
+ t0 = <View>{maybeMutate(maybeMutable)}</View>;
34
+ $[0] = t0;
35
} else {
42
- T0 = $[0];
43
- t1 = $[1];
44
- t2 = $[2];
36
+ t0 = $[0];
37
}
46
- return t2;
38
+ return t0;
39
}
40
41
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md
+4
-7
@@ -24,20 +24,17 @@ export const FIXTURE_ENTRYPOINT = {
24
```javascript
25
import { unstable_useMemoCache as useMemoCache } from "react";
26
function hoisting() {
27
- const $ = useMemoCache(2);
28
- let foo;
27
+ const $ = useMemoCache(1);
28
let t0;
29
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
31
- foo = () => bar + baz;
30
+ const foo = () => bar + baz;
31
32
const bar = 3;
33
const baz = 2;
34
t0 = foo();
36
- $[0] = foo;
37
- $[1] = t0;
35
+ $[0] = t0;
36
} else {
39
- foo = $[0];
40
- t0 = $[1];
37
+ t0 = $[0];
38
}
39
return t0;
40
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inner-memo-value-not-promoted-to-outer-scope-static.expect.md
+13
-53
@@ -21,70 +21,30 @@ function Component(props) {
21
```javascript
22
import { unstable_useMemoCache as useMemoCache } from "react";
23
function Component(props) {
24
- const $ = useMemoCache(11);
24
+ const $ = useMemoCache(2);
25
let t1;
26
- let T2;
27
- let t3;
28
- let t0;
29
- let t4;
30
- let T5;
31
- let t6;
32
- let t7;
33
- let t8;
34
- let t9;
26
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
27
const count = new MaybeMutable();
37
-
38
- T5 = View;
39
- t6 = "\n ";
40
- T2 = View;
41
- t3 = "\n ";
42
- if ($[10] === Symbol.for("react.memo_cache_sentinel")) {
28
+ let t0;
29
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
30
t0 = <span>Text</span>;
44
- $[10] = t0;
31
+ $[1] = t0;
32
} else {
46
- t0 = $[10];
33
+ t0 = $[1];
34
}
48
- t4 = "\n ";
49
- t1 = maybeMutate(count);
50
- t7 = <span>{t1}</span>;
51
- t8 = (
52
- <T2>
53
- {t3}
54
- {t0}
55
- {t4}
56
- {t7}
57
- </T2>
58
- );
59
- t9 = (
60
- <T5>
61
- {t6}
62
- {t8}
63
- </T5>
35
+ t1 = (
36
+ <View>
37
+ <View>
38
+ {t0}
39
+ <span>{maybeMutate(count)}</span>
40
+ </View>
41
+ </View>
42
);
43
$[0] = t1;
66
- $[1] = T2;
67
- $[2] = t3;
68
- $[3] = t0;
69
- $[4] = t4;
70
- $[5] = T5;
71
- $[6] = t6;
72
- $[7] = t7;
73
- $[8] = t8;
74
- $[9] = t9;
44
} else {
45
t1 = $[0];
77
- T2 = $[1];
78
- t3 = $[2];
79
- t0 = $[3];
80
- t4 = $[4];
81
- T5 = $[5];
82
- t6 = $[6];
83
- t7 = $[7];
84
- t8 = $[8];
85
- t9 = $[9];
46
}
87
- return t9;
47
+ return t1;
48
}
49
50
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-fragment.expect.md
+16
-15
@@ -26,34 +26,35 @@ export const FIXTURE_ENTRYPOINT = {
26
```javascript
27
import { unstable_useMemoCache as useMemoCache } from "react";
28
function Foo(props) {
29
- const $ = useMemoCache(4);
29
+ const $ = useMemoCache(3);
30
let t0;
31
- let t1;
31
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
33
- t0 = <>Text</>;
34
- t1 = <div>{t0}</div>;
32
+ t0 = (
33
+ <div>
34
+ <>Text</>
35
+ </div>
36
+ );
37
$[0] = t0;
36
- $[1] = t1;
38
} else {
39
t0 = $[0];
39
- t1 = $[1];
40
}
41
- const c_2 = $[2] !== props.greeting;
42
- let t2;
43
- if (c_2) {
44
- t2 = (
41
+ const c_1 = $[1] !== props.greeting;
42
+ let t1;
43
+ if (c_1) {
44
+ t1 = (
45
<>
46
Hello {props.greeting}
47
- {t1}
47
+ {t0}
48
</>
49
);
50
- $[2] = props.greeting;
51
- $[3] = t2;
50
+ $[1] = props.greeting;
51
+ $[2] = t1;
52
} else {
53
- t2 = $[3];
53
+ t1 = $[2];
54
}
55
- return t2;
55
+ return t1;
56
}
57
+
58
export const FIXTURE_ENTRYPOINT = {
59
fn: Foo,
60
params: ["TodoAdd"],
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression-tag-grouping.expect.md
+6
-14
@@ -14,24 +14,16 @@ function Component(props) {
14
```javascript
15
import { unstable_useMemoCache as useMemoCache } from "react";
16
function Component(props) {
17
- const $ = useMemoCache(3);
18
- let T0;
19
- let t1;
20
- let t2;
17
+ const $ = useMemoCache(1);
18
+ let t0;
19
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
20
const maybeMutable = new MaybeMutable();
23
- T0 = Foo.Bar;
24
- t1 = maybeMutate(maybeMutable);
25
- t2 = <T0>{t1}</T0>;
26
- $[0] = T0;
27
- $[1] = t1;
28
- $[2] = t2;
21
+ t0 = <Foo.Bar>{maybeMutate(maybeMutable)}</Foo.Bar>;
22
+ $[0] = t0;
23
} else {
30
- T0 = $[0];
31
- t1 = $[1];
32
- t2 = $[2];
24
+ t0 = $[0];
25
}
34
- return t2;
26
+ return t0;
27
}
28
29
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression.expect.md
+7
-7
@@ -17,19 +17,19 @@ function Component(props) {
17
```javascript
18
import { unstable_useMemoCache as useMemoCache } from "react";
19
function Component(props) {
20
- const $ = useMemoCache(2);
20
+ const $ = useMemoCache(1);
21
let t0;
22
- let t1;
22
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
24
- t0 = <Foo.Bar.Baz />;
25
- t1 = <Sathya.Codes.Forget>{t0}</Sathya.Codes.Forget>;
23
+ t0 = (
24
+ <Sathya.Codes.Forget>
25
+ <Foo.Bar.Baz />
26
+ </Sathya.Codes.Forget>
27
+ );
28
$[0] = t0;
27
- $[1] = t1;
29
} else {
30
t0 = $[0];
30
- t1 = $[1];
31
}
32
- return t1;
32
+ return t0;
33
}
34
35
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-with-fbt.expect.md
+7
-11
@@ -42,7 +42,7 @@ import { unstable_useMemoCache as useMemoCache } from "react";
42
import { fbt } from "fbt";
43
44
function Component() {
45
- const $ = useMemoCache(4);
45
+ const $ = useMemoCache(2);
46
let t0;
47
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
48
t0 = () => {
@@ -72,21 +72,17 @@ function Component() {
72
}
73
const buttonLabel = t0;
74
let t1;
75
- let t2;
76
- let t3;
75
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
78
- t1 = buttonLabel();
79
- t2 = <Button text={t1} />;
80
- t3 = <View>{t2}</View>;
76
+ t1 = (
77
+ <View>
78
+ <Button text={buttonLabel()} />
79
+ </View>
80
+ );
81
$[1] = t1;
82
- $[2] = t2;
83
- $[3] = t3;
82
} else {
83
t1 = $[1];
86
- t2 = $[2];
87
- t3 = $[3];
84
}
89
- return t3;
85
+ return t1;
86
}
87
88
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/merge-consecutive-scopes.expect.md
+13
-17
@@ -27,7 +27,7 @@ export const FIXTURE_ENTRYPOINT = {
27
```javascript
28
import { unstable_useMemoCache as useMemoCache } from "react";
29
function Component() {
30
- const $ = useMemoCache(9);
30
+ const $ = useMemoCache(8);
31
const [state, setState] = useState(0);
32
let t0;
33
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
@@ -47,39 +47,35 @@ function Component() {
47
}
48
const c_3 = $[3] !== state;
49
let t2;
50
- let t3;
50
if (c_3) {
52
- t2 = () => setState(state + 1);
53
- t3 = (
54
- <button data-testid="button" onClick={t2}>
51
+ t2 = (
52
+ <button data-testid="button" onClick={() => setState(state + 1)}>
53
increment
54
</button>
55
);
56
$[3] = state;
57
$[4] = t2;
60
- $[5] = t3;
58
} else {
59
t2 = $[4];
63
- t3 = $[5];
60
}
65
- const c_6 = $[6] !== t1;
66
- const c_7 = $[7] !== t3;
67
- let t4;
68
- if (c_6 || c_7) {
69
- t4 = (
61
+ const c_5 = $[5] !== t1;
62
+ const c_6 = $[6] !== t2;
63
+ let t3;
64
+ if (c_5 || c_6) {
65
+ t3 = (
66
<div>
67
{t0}
68
{t1}
73
- {t3}
69
+ {t2}
70
</div>
71
);
76
- $[6] = t1;
72
+ $[5] = t1;
73
+ $[6] = t2;
74
$[7] = t3;
78
- $[8] = t4;
75
} else {
80
- t4 = $[8];
76
+ t3 = $[7];
77
}
82
- return t4;
78
+ return t3;
79
}
80
export const FIXTURE_ENTRYPOINT = {
81
fn: Component,
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.expect.md
+4
-8
@@ -37,7 +37,7 @@ const ErrorView = isForgetEnabled_Fixtures()
37
38
export default Renderer = isForgetEnabled_Fixtures()
39
? (props) => {
40
- const $ = useMemoCache(3);
40
+ const $ = useMemoCache(2);
41
let t0;
42
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
43
t0 = <Bar />;
@@ -46,22 +46,18 @@ export default Renderer = isForgetEnabled_Fixtures()
46
t0 = $[0];
47
}
48
let t1;
49
- let t2;
49
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
51
- t1 = <ErrorView />;
52
- t2 = (
50
+ t1 = (
51
<Foo>
52
{t0}
55
- {t1}
53
+ <ErrorView />
54
</Foo>
55
);
56
$[1] = t1;
59
- $[2] = t2;
57
} else {
58
t1 = $[1];
62
- t2 = $[2];
59
}
64
- return t2;
60
+ return t1;
61
}
62
: (props) => (
63
<Foo>
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-gating-test.expect.md
+4
-8
@@ -37,7 +37,7 @@ const ErrorView = isForgetEnabled_Fixtures()
37
38
export const Renderer = isForgetEnabled_Fixtures()
39
? (props) => {
40
- const $ = useMemoCache(3);
40
+ const $ = useMemoCache(2);
41
let t0;
42
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
43
t0 = <Bar />;
@@ -46,22 +46,18 @@ export const Renderer = isForgetEnabled_Fixtures()
46
t0 = $[0];
47
}
48
let t1;
49
- let t2;
49
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
51
- t1 = <ErrorView />;
52
- t2 = (
50
+ t1 = (
51
<Foo>
52
{t0}
55
- {t1}
53
+ <ErrorView />
54
</Foo>
55
);
56
$[1] = t1;
59
- $[2] = t2;
57
} else {
58
t1 = $[1];
62
- t2 = $[2];
59
}
64
- return t2;
60
+ return t1;
61
}
62
: (props) => (
63
<Foo>
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-gating-test.expect.md
+4
-8
@@ -39,7 +39,7 @@ const ErrorView = isForgetEnabled_Fixtures()
39
40
const Renderer = isForgetEnabled_Fixtures()
41
? (props) => {
42
- const $ = useMemoCache(3);
42
+ const $ = useMemoCache(2);
43
let t0;
44
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
45
t0 = <Bar />;
@@ -48,22 +48,18 @@ const Renderer = isForgetEnabled_Fixtures()
48
t0 = $[0];
49
}
50
let t1;
51
- let t2;
51
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
53
- t1 = <ErrorView />;
54
- t2 = (
52
+ t1 = (
53
<Foo>
54
{t0}
57
- {t1}
55
+ <ErrorView />
56
</Foo>
57
);
58
$[1] = t1;
61
- $[2] = t2;
59
} else {
60
t1 = $[1];
64
- t2 = $[2];
61
}
66
- return t2;
62
+ return t1;
63
}
64
: (props) => (
65
<Foo>
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md
+7
-19
@@ -76,17 +76,13 @@ function cond(x) {
76
}
77
78
function testFunction(props) {
79
- const $ = useMemoCache(5);
80
- let a;
81
- let b;
82
- let c;
83
- let d;
79
+ const $ = useMemoCache(1);
80
let t0;
81
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
86
- a = {};
87
- b = {};
88
- c = {};
89
- d = {};
82
+ let a = {};
83
+ let b = {};
84
+ let c = {};
85
+ let d = {};
86
while (true) {
87
const z = a;
88
a = b;
@@ -109,17 +105,9 @@ function testFunction(props) {
105
106
mutate(d, null);
107
t0 = { a, b, c, d };
112
- $[0] = a;
113
- $[1] = b;
114
- $[2] = c;
115
- $[3] = d;
116
- $[4] = t0;
108
+ $[0] = t0;
109
} else {
118
- a = $[0];
119
- b = $[1];
120
- c = $[2];
121
- d = $[3];
122
- t0 = $[4];
110
+ t0 = $[0];
111
}
112
return t0;
113
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md
+4
-8
@@ -24,24 +24,20 @@ export const FIXTURE_ENTRYPOINT = {
24
```javascript
25
import { unstable_useMemoCache as useMemoCache } from "react";
26
function Foo() {
27
- const $ = useMemoCache(2);
27
+ const $ = useMemoCache(1);
28
let t0;
29
- let t1;
29
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
31
- t0 = function t() {
30
+ t0 = (function t() {
31
return function a(t25) {
32
const x_0 = t25 === undefined ? () => {} : t25;
33
return x_0;
34
};
36
- };
37
- t1 = t0();
35
+ })();
36
$[0] = t0;
39
- $[1] = t1;
37
} else {
38
t0 = $[0];
42
- t1 = $[1];
39
}
44
- return t1;
40
+ return t0;
41
}
42
43
export const FIXTURE_ENTRYPOINT = {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md
+5
-11
@@ -32,14 +32,12 @@ export const FIXTURE_ENTRYPOINT = {
32
```javascript
33
import { unstable_useMemoCache as useMemoCache } from "react"; // @debug
34
function Component(props) {
35
- const $ = useMemoCache(4);
35
+ const $ = useMemoCache(2);
36
const c_0 = $[0] !== props;
37
- let x;
38
- let y;
37
let t0;
38
if (c_0) {
41
- x = {};
42
- y = undefined;
39
+ const x = {};
40
+ let y = undefined;
41
if (props.cond) {
42
y = [props.value];
43
} else {
@@ -50,13 +48,9 @@ function Component(props) {
48
49
t0 = [x, y];
50
$[0] = props;
53
- $[1] = x;
54
- $[2] = y;
55
- $[3] = t0;
51
+ $[1] = t0;
52
} else {
57
- x = $[1];
58
- y = $[2];
59
- t0 = $[3];
53
+ t0 = $[1];
54
}
55
return t0;
56
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-as-dep-nested-scope.expect.md
+10
-16
@@ -26,37 +26,31 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // props.b + 1 is
26
// Correctness:
27
// y depends on either props.b or props.b + 1
28
function PrimitiveAsDepNested(props) {
29
- const $ = useMemoCache(7);
29
+ const $ = useMemoCache(5);
30
const c_0 = $[0] !== props.b;
31
const c_1 = $[1] !== props.a;
32
- let x;
33
- let y;
32
let t2;
33
if (c_0 || c_1) {
36
- x = {};
34
+ const x = {};
35
mutate(x);
36
const t0 = props.b + 1;
39
- const c_5 = $[5] !== t0;
37
+ const c_3 = $[3] !== t0;
38
let t1;
41
- if (c_5) {
39
+ if (c_3) {
40
t1 = foo(t0);
43
- $[5] = t0;
44
- $[6] = t1;
41
+ $[3] = t0;
42
+ $[4] = t1;
43
} else {
46
- t1 = $[6];
44
+ t1 = $[4];
45
}
48
- y = t1;
46
+ const y = t1;
47
mutate(x, props.a);
48
t2 = [x, y];
49
$[0] = props.b;
50
$[1] = props.a;
53
- $[2] = x;
54
- $[3] = y;
55
- $[4] = t2;
51
+ $[2] = t2;
52
} else {
57
- x = $[2];
58
- y = $[3];
59
- t2 = $[4];
53
+ t2 = $[2];
54
}
55
return t2;
56
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/property-call-evaluation-order.expect.md
+4
-8
@@ -30,7 +30,7 @@ export const FIXTURE_ENTRYPOINT = {
30
import { unstable_useMemoCache as useMemoCache } from "react"; // Should print A, arg, original
31
32
function Component() {
33
- const $ = useMemoCache(3);
33
+ const $ = useMemoCache(2);
34
let t0;
35
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
36
t0 = (o) => {
@@ -41,18 +41,14 @@ function Component() {
41
t0 = $[0];
42
}
43
const changeF = t0;
44
- let t1;
44
let x;
45
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
47
- t1 = () => console.log("original");
48
- x = { f: t1 };
46
+ x = { f: () => console.log("original") };
47
48
(console.log("A"), x).f((changeF(x), console.log("arg"), 1));
51
- $[1] = t1;
52
- $[2] = x;
49
+ $[1] = x;
50
} else {
54
- t1 = $[1];
55
- x = $[2];
51
+ x = $[1];
52
}
53
return x;
54
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reactive-scopes.expect.md
+4
-7
@@ -26,13 +26,12 @@ export const FIXTURE_ENTRYPOINT = {
26
```javascript
27
import { unstable_useMemoCache as useMemoCache } from "react";
28
function f(a, b) {
29
- const $ = useMemoCache(4);
29
+ const $ = useMemoCache(3);
30
const c_0 = $[0] !== a.length;
31
const c_1 = $[1] !== b;
32
- let x;
32
let t0;
33
if (c_0 || c_1) {
35
- x = [];
34
+ const x = [];
35
if (a.length === 1) {
36
if (b) {
37
x.push(b);
@@ -42,11 +41,9 @@ function f(a, b) {
41
t0 = <div>{x}</div>;
42
$[0] = a.length;
43
$[1] = b;
45
- $[2] = x;
46
- $[3] = t0;
44
+ $[2] = t0;
45
} else {
48
- x = $[2];
49
- t0 = $[3];
46
+ t0 = $[2];
47
}
48
return t0;
49
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps-join-uncond-scopes-cond-deps.expect.md
+10
-15
@@ -52,35 +52,30 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // This tests an
52
// }
53
54
function TestJoinCondDepsInUncondScopes(props) {
55
- const $ = useMemoCache(6);
55
+ const $ = useMemoCache(4);
56
const c_0 = $[0] !== props.a.b;
57
- let x;
58
- let y;
57
let t0;
58
if (c_0) {
61
- y = {};
62
- const c_4 = $[4] !== props;
63
- if (c_4) {
59
+ const y = {};
60
+ const c_2 = $[2] !== props;
61
+ let x;
62
+ if (c_2) {
63
x = {};
64
if (foo) {
65
mutate1(x, props.a.b);
66
}
68
- $[4] = props;
69
- $[5] = x;
67
+ $[2] = props;
68
+ $[3] = x;
69
} else {
71
- x = $[5];
70
+ x = $[3];
71
}
72
73
mutate2(y, props.a.b);
74
t0 = [x, y];
75
$[0] = props.a.b;
77
- $[1] = x;
78
- $[2] = y;
79
- $[3] = t0;
76
+ $[1] = t0;
77
} else {
81
- x = $[1];
82
- y = $[2];
83
- t0 = $[3];
78
+ t0 = $[1];
79
}
80
return t0;
81
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx-2.expect.md
+5
-11
@@ -25,26 +25,20 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // @Pass runMutab
25
function foo() {}
26
27
function Component(props) {
28
- const $ = useMemoCache(3);
29
- let a;
30
- let b;
28
+ const $ = useMemoCache(1);
29
let t0;
30
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
33
- a = [];
34
- b = {};
31
+ const a = [];
32
+ const b = {};
33
foo(a, b);
34
if (foo()) {
35
}
36
37
foo(a, b);
38
t0 = <div a={a} b={b} />;
41
- $[0] = a;
42
- $[1] = b;
43
- $[2] = t0;
39
+ $[0] = t0;
40
} else {
45
- a = $[0];
46
- b = $[1];
47
- t0 = $[2];
41
+ t0 = $[0];
42
}
43
return t0;
44
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx.expect.md
+5
-11
@@ -22,24 +22,18 @@ import { unstable_useMemoCache as useMemoCache } from "react";
22
function foo() {}
23
24
function Component(props) {
25
- const $ = useMemoCache(3);
26
- let a;
27
- let b;
25
+ const $ = useMemoCache(1);
26
let t0;
27
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
30
- a = [];
31
- b = {};
28
+ const a = [];
29
+ const b = {};
30
foo(a, b);
31
32
foo(a, b);
33
t0 = <div a={a} b={b} />;
36
- $[0] = a;
37
- $[1] = b;
38
- $[2] = t0;
34
+ $[0] = t0;
35
} else {
40
- a = $[0];
41
- b = $[1];
42
- t0 = $[2];
36
+ t0 = $[0];
37
}
38
return t0;
39
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-accessed-outside-scope.expect.md
+3
-11
@@ -15,27 +15,19 @@ function Component(props) {
15
```javascript
16
import { unstable_useMemoCache as useMemoCache } from "react";
17
function Component(props) {
18
- const $ = useMemoCache(4);
18
+ const $ = useMemoCache(2);
19
const c_0 = $[0] !== props;
20
let t0;
21
- let t1;
22
- let t2;
21
if (c_0) {
22
const maybeMutable = new MaybeMutable();
23
const x = props;
26
- t0 = x;
27
- t1 = maybeMutate(maybeMutable);
28
- t2 = [t0, t1];
24
+ t0 = [x, maybeMutate(maybeMutable)];
25
$[0] = props;
26
$[1] = t0;
31
- $[2] = t1;
32
- $[3] = t2;
27
} else {
28
t0 = $[1];
35
- t1 = $[2];
36
- t2 = $[3];
29
}
38
- return t2;
30
+ return t0;
31
}
32
33
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-property-load-accessed-outside-scope.expect.md
+3
-11
@@ -15,27 +15,19 @@ function Component(props) {
15
```javascript
16
import { unstable_useMemoCache as useMemoCache } from "react";
17
function Component(props) {
18
- const $ = useMemoCache(4);
18
+ const $ = useMemoCache(2);
19
const c_0 = $[0] !== props.value;
20
let t0;
21
- let t1;
22
- let t2;
21
if (c_0) {
22
const maybeMutable = new MaybeMutable();
23
const x = props.value;
26
- t0 = x;
27
- t1 = maybeMutate(maybeMutable);
28
- t2 = [t0, t1];
24
+ t0 = [x, maybeMutate(maybeMutable)];
25
$[0] = props.value;
26
$[1] = t0;
31
- $[2] = t1;
32
- $[3] = t2;
27
} else {
28
t0 = $[1];
35
- t1 = $[2];
36
- t2 = $[3];
29
}
38
- return t2;
30
+ return t0;
31
}
32
33
```
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-test-field-load-binary-op.expect.md
+3
-7
@@ -21,7 +21,7 @@ function component() {
21
```javascript
22
import { unstable_useMemoCache as useMemoCache } from "react";
23
function component() {
24
- const $ = useMemoCache(3);
24
+ const $ = useMemoCache(2);
25
let t0;
26
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
27
t0 = makeSomePrimitive();
@@ -30,17 +30,13 @@ function component() {
30
t0 = $[0];
31
}
32
let t1;
33
- let t2;
33
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
35
- t1 = makeSomePrimitive();
36
- t2 = { u: t0, v: t1 };
34
+ t1 = { u: t0, v: makeSomePrimitive() };
35
$[1] = t1;
38
- $[2] = t2;
36
} else {
37
t1 = $[1];
41
- t2 = $[2];
38
}
43
- const x = t2;
39
+ const x = t1;
40
const u = x.u;
41
const v = x.v;
42
if (u > v) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md
+5
-11
@@ -16,22 +16,16 @@ function Component(props) {
16
```javascript
17
import { unstable_useMemoCache as useMemoCache } from "react";
18
function Component(props) {
19
- const $ = useMemoCache(3);
20
- let y;
21
- let x;
19
+ const $ = useMemoCache(1);
20
let t0;
21
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
24
- x = mutate();
25
-
22
+ const x = mutate();
23
+ let y;
24
foo(x);
25
t0 = [y, x];
28
- $[0] = y;
29
- $[1] = x;
30
- $[2] = t0;
26
+ $[0] = t0;
27
} else {
32
- y = $[0];
33
- x = $[1];
34
- t0 = $[2];
28
+ t0 = $[0];
29
}
30
return t0;
31
}