From 66841aa9ac59d4312f42e41cd8e39158765c5eae Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 27 Sep 2023 14:04:00 -0400 Subject: [PATCH] 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. --- .../ReactiveScopes/MergeConsecutiveScopes.ts | 25 +++++++ ...re-in-method-receiver-and-mutate.expect.md | 16 ++--- ...alias-capture-in-method-receiver.expect.md | 11 ++-- .../alias-nested-member-path.expect.md | 11 ++-- ...ng-primitive-as-dep-nested-scope.expect.md | 26 +++----- .../array-access-assignment.expect.md | 26 +++----- .../compiler/array-at-effect.expect.md | 44 ++++++------- .../array-at-mutate-after-capture.expect.md | 10 +-- ...-variations-complex-lvalue-array.expect.md | 10 +-- ...sx-tag-lowered-between-mutations.expect.md | 10 +-- ...ith-independently-memoizable-arg.expect.md | 22 +++---- .../fixtures/compiler/call.expect.md | 16 ++--- .../capturing-nested-member-call.expect.md | 10 +-- ...ested-member-expr-in-nested-func.expect.md | 26 ++++---- .../capturing-nested-member-expr.expect.md | 26 ++++---- .../codegen-emit-make-read-only.expect.md | 11 ++-- .../computed-call-evaluation-order.expect.md | 12 ++-- .../fixtures/compiler/constructor.expect.md | 16 ++--- .../fbt-template-string-same-scope.expect.md | 18 ++--- ...xt-must-use-expression-container.expect.md | 14 ++-- ...btparam-with-jsx-element-content.expect.md | 54 +++++++-------- ...fbtparam-with-jsx-fragment-value.expect.md | 20 +++--- .../compiler/for-in-statement.expect.md | 11 ++-- .../fixtures/compiler/for-of-mutate.expect.md | 11 ++-- .../function-declaration-reassign.expect.md | 12 ++-- ...sx-tag-lowered-between-mutations.expect.md | 20 ++---- ...oisting-simple-const-declaration.expect.md | 11 ++-- ...t-promoted-to-outer-scope-static.expect.md | 66 ++++--------------- .../fixtures/compiler/jsx-fragment.expect.md | 31 ++++----- ...x-member-expression-tag-grouping.expect.md | 20 ++---- .../compiler/jsx-member-expression.expect.md | 14 ++-- .../compiler/lambda-with-fbt.expect.md | 18 ++--- .../merge-consecutive-scopes.expect.md | 30 ++++----- ...-expr-export-default-gating-test.expect.md | 12 ++-- ...ti-arrow-expr-export-gating-test.expect.md | 12 ++-- .../multi-arrow-expr-gating-test.expect.md | 12 ++-- .../compiler/mutable-lifetime-loops.expect.md | 26 ++------ ...ction-with-param-as-captured-dep.expect.md | 12 ++-- .../phi-type-inference-array-push.expect.md | 16 ++--- .../primitive-as-dep-nested-scope.expect.md | 26 +++----- .../property-call-evaluation-order.expect.md | 12 ++-- .../compiler/reactive-scopes.expect.md | 11 ++-- ...eps-join-uncond-scopes-cond-deps.expect.md | 25 +++---- .../compiler/ssa-call-jsx-2.expect.md | 16 ++--- .../fixtures/compiler/ssa-call-jsx.expect.md | 16 ++--- ...temporary-accessed-outside-scope.expect.md | 14 +--- ...erty-load-accessed-outside-scope.expect.md | 14 +--- .../type-test-field-load-binary-op.expect.md | 10 +-- ...ed-declaration-in-reactive-scope.expect.md | 16 ++--- 49 files changed, 359 insertions(+), 569 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/MergeConsecutiveScopes.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/MergeConsecutiveScopes.ts index 0df8d3c59c..7454557d4a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/MergeConsecutiveScopes.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/MergeConsecutiveScopes.ts @@ -169,6 +169,9 @@ class Transform extends ReactiveFunctionTransform { areLValuesLastUsedByScope(instr.scope, lvalues, this.lastUsage) ) { const intermediateInstructions = block.slice(currentScope.to, i); + currentScope.scope.scope.range.end = makeInstructionId( + Math.max(currentScope.scope.scope.range.end, instr.scope.range.end) + ); currentScope.scope.instructions.push(...intermediateInstructions); currentScope.scope.instructions.push(...instr.instructions); for (const [key, value] of instr.scope.declarations) { @@ -193,12 +196,34 @@ class Transform extends ReactiveFunctionTransform { } if (nextInstructions !== null) { + for (const instr of nextInstructions) { + if (instr.kind === "scope") { + updateScopeDeclarations(instr.scope, this.lastUsage); + } + } + block.length = 0; block.push(...nextInstructions); } } } +/** + * Updates @param scope's declarations to remove any declarations that are not + * used after the scope, based on the scope's updated range post-merging. + */ +function updateScopeDeclarations( + scope: ReactiveScope, + lastUsage: Map +): void { + for (const [key] of scope.declarations) { + const lastUsedAt = lastUsage.get(key)!; + if (lastUsedAt < scope.range.end) { + scope.declarations.delete(key); + } + } +} + /** * Returns whether the given @param scope is the last usage of all * the given @param lvalues. Returns false if any of the lvalues diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver-and-mutate.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver-and-mutate.expect.md index f850762bf7..e6513b4f23 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver-and-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver-and-mutate.expect.md @@ -31,25 +31,19 @@ import { unstable_useMemoCache as useMemoCache } from "react"; import { makeObject_Primitives, mutate } from "shared-runtime"; function Component() { - const $ = useMemoCache(3); - let x; - let a; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - a = makeObject_Primitives(); + const a = makeObject_Primitives(); - x = []; + const x = []; x.push(a); mutate(x); t0 = [x, a]; - $[0] = x; - $[1] = a; - $[2] = t0; + $[0] = t0; } else { - x = $[0]; - a = $[1]; - t0 = $[2]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver.expect.md index acf02bdff8..1628bb84be 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver.expect.md @@ -20,7 +20,7 @@ function Component() { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component() { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = someObj(); @@ -29,18 +29,15 @@ function Component() { t0 = $[0]; } const a = t0; - let x; let t1; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - x = []; + const x = []; x.push(a); t1 = [x, a]; - $[1] = x; - $[2] = t1; + $[1] = t1; } else { - x = $[1]; - t1 = $[2]; + t1 = $[1]; } return t1; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md index ef339261e4..d61306d424 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md @@ -24,7 +24,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function component() { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = []; @@ -33,18 +33,15 @@ function component() { t0 = $[0]; } const z = t0; - let y; let x; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - y = {}; + const y = {}; y.z = z; x = {}; x.y = y; - $[1] = y; - $[2] = x; + $[1] = x; } else { - y = $[1]; - x = $[2]; + x = $[1]; } return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/allocating-primitive-as-dep-nested-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/allocating-primitive-as-dep-nested-scope.expect.md index 0e57c16178..77896b8c75 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/allocating-primitive-as-dep-nested-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/allocating-primitive-as-dep-nested-scope.expect.md @@ -24,37 +24,31 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // bar(props.b) i // Correctness: // - y depends on either bar(props.b) or bar(props.b) + 1 function AllocatingPrimitiveAsDepNested(props) { - const $ = useMemoCache(7); + const $ = useMemoCache(5); const c_0 = $[0] !== props.b; const c_1 = $[1] !== props.a; - let x; - let y; let t2; if (c_0 || c_1) { - x = {}; + const x = {}; mutate(x); const t0 = bar(props.b) + 1; - const c_5 = $[5] !== t0; + const c_3 = $[3] !== t0; let t1; - if (c_5) { + if (c_3) { t1 = foo(t0); - $[5] = t0; - $[6] = t1; + $[3] = t0; + $[4] = t1; } else { - t1 = $[6]; + t1 = $[4]; } - y = t1; + const y = t1; mutate(x, props.a); t2 = [x, y]; $[0] = props.b; $[1] = props.a; - $[2] = x; - $[3] = y; - $[4] = t2; + $[2] = t2; } else { - x = $[2]; - y = $[3]; - t2 = $[4]; + t2 = $[2]; } return t2; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-access-assignment.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-access-assignment.expect.md index 804c4c0819..fd7afe5f1b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-access-assignment.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-access-assignment.expect.md @@ -24,39 +24,33 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function foo(a, b, c) { - const $ = useMemoCache(8); + const $ = useMemoCache(6); const c_0 = $[0] !== a; const c_1 = $[1] !== b; const c_2 = $[2] !== c; - let x; - let z; let t1; if (c_0 || c_1 || c_2) { - x = [a]; - const c_6 = $[6] !== b; + const x = [a]; + const c_4 = $[4] !== b; let t0; - if (c_6) { + if (c_4) { t0 = [null, b]; - $[6] = b; - $[7] = t0; + $[4] = b; + $[5] = t0; } else { - t0 = $[7]; + t0 = $[5]; } const y = t0; - z = [[], [], [c]]; + const z = [[], [], [c]]; x[0] = y[1]; z[0][0] = x[0]; t1 = [x, z]; $[0] = a; $[1] = b; $[2] = c; - $[3] = x; - $[4] = z; - $[5] = t1; + $[3] = t1; } else { - x = $[3]; - z = $[4]; - t1 = $[5]; + t1 = $[3]; } return t1; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-effect.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-effect.expect.md index 4d47ab5aa6..f20b4b2122 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-effect.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-effect.expect.md @@ -22,42 +22,38 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // arrayInstance. // - read on receiver // - mutate on lvalue function ArrayAtTest(props) { - const $ = useMemoCache(8); + const $ = useMemoCache(7); const c_0 = $[0] !== props.x; let t0; - let t1; if (c_0) { - t0 = foo(props.x); - t1 = [t0]; + t0 = [foo(props.x)]; $[0] = props.x; $[1] = t0; - $[2] = t1; } else { t0 = $[1]; - t1 = $[2]; } - const arr = t1; - const c_3 = $[3] !== props.y; - const c_4 = $[4] !== arr; - let t3; - if (c_3 || c_4) { - const c_6 = $[6] !== props.y; - let t2; - if (c_6) { - t2 = bar(props.y); - $[6] = props.y; - $[7] = t2; + const arr = t0; + const c_2 = $[2] !== props.y; + const c_3 = $[3] !== arr; + let t2; + if (c_2 || c_3) { + const c_5 = $[5] !== props.y; + let t1; + if (c_5) { + t1 = bar(props.y); + $[5] = props.y; + $[6] = t1; } else { - t2 = $[7]; + t1 = $[6]; } - t3 = arr.at(t2); - $[3] = props.y; - $[4] = arr; - $[5] = t3; + t2 = arr.at(t1); + $[2] = props.y; + $[3] = arr; + $[4] = t2; } else { - t3 = $[5]; + t2 = $[4]; } - const result = t3; + const result = t2; return result; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-mutate-after-capture.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-mutate-after-capture.expect.md index 8e88d973b3..121d085be1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-mutate-after-capture.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-at-mutate-after-capture.expect.md @@ -21,19 +21,15 @@ function Component(props) { import { unstable_useMemoCache as useMemoCache } from "react"; // x's mutable range should extend to `mutate(y)` function Component(props) { - const $ = useMemoCache(2); + const $ = useMemoCache(1); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = {}; - t1 = [42, t0]; + t0 = [42, {}]; $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - const x = t1; + const x = t0; const idx = foo(props.b); const y = x.at(idx); mutate(y); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations-complex-lvalue-array.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations-complex-lvalue-array.expect.md index b687bd1851..451f29f572 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations-complex-lvalue-array.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/assignment-variations-complex-lvalue-array.expect.md @@ -22,19 +22,15 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function foo() { - const $ = useMemoCache(2); + const $ = useMemoCache(1); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = [1]; - t1 = [t0]; + t0 = [[1]]; $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - const a = t1; + const a = t0; const first = a.at(0); first.set(0, 2); return a; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/builtin-jsx-tag-lowered-between-mutations.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/builtin-jsx-tag-lowered-between-mutations.expect.md index a74b039dc9..cb80ee96b7 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/builtin-jsx-tag-lowered-between-mutations.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/builtin-jsx-tag-lowered-between-mutations.expect.md @@ -14,20 +14,16 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(2); + const $ = useMemoCache(1); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { const maybeMutable = new MaybeMutable(); - t0 = maybeMutate(maybeMutable); - t1 =
{t0}
; + t0 =
{maybeMutate(maybeMutable)}
; $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - return t1; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call-with-independently-memoizable-arg.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call-with-independently-memoizable-arg.expect.md index c6875751d6..ac3985eaff 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call-with-independently-memoizable-arg.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call-with-independently-memoizable-arg.expect.md @@ -19,31 +19,31 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(5); + const $ = useMemoCache(4); const c_0 = $[0] !== props; - let t2; + let t1; if (c_0) { const x = makeFunction(props); const c_2 = $[2] !== props.text; let t0; - let t1; if (c_2) { - t0 = {props.text}; - t1 =
{t0}
; + t0 = ( +
+ {props.text} +
+ ); $[2] = props.text; $[3] = t0; - $[4] = t1; } else { t0 = $[3]; - t1 = $[4]; } - t2 = x(t1); + t1 = x(t0); $[0] = props; - $[1] = t2; + $[1] = t1; } else { - t2 = $[1]; + t1 = $[1]; } - const y = t2; + const y = t1; return y; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call.expect.md index 7f83179345..fc0a457f7b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/call.expect.md @@ -22,24 +22,18 @@ import { unstable_useMemoCache as useMemoCache } from "react"; function foo() {} function Component(props) { - const $ = useMemoCache(3); - let a; - let b; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - a = []; - b = {}; + const a = []; + const b = {}; foo(a, b); foo(b); t0 =
; - $[0] = a; - $[1] = b; - $[2] = t0; + $[0] = t0; } else { - a = $[0]; - b = $[1]; - t0 = $[2]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md index af1acfe95b..9725ed6b7c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md @@ -23,21 +23,17 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function component(a) { - const $ = useMemoCache(3); + const $ = useMemoCache(2); const c_0 = $[0] !== a; let t0; - let t1; if (c_0) { - t0 = { a }; - t1 = { a: t0 }; + t0 = { a: { a } }; $[0] = a; $[1] = t0; - $[2] = t1; } else { t0 = $[1]; - t1 = $[2]; } - const z = t1; + const z = t0; return z; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md index 018e8493cb..ec43972dd2 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md @@ -25,35 +25,31 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function component(a) { - const $ = useMemoCache(5); + const $ = useMemoCache(4); const c_0 = $[0] !== a; let t0; - let t1; if (c_0) { - t0 = { a }; - t1 = { a: t0 }; + t0 = { a: { a } }; $[0] = a; $[1] = t0; - $[2] = t1; } else { t0 = $[1]; - t1 = $[2]; } - const z = t1; - const c_3 = $[3] !== z.a.a; - let t2; - if (c_3) { - t2 = function () { + const z = t0; + const c_2 = $[2] !== z.a.a; + let t1; + if (c_2) { + t1 = function () { (function () { console.log(z.a.a); })(); }; - $[3] = z.a.a; - $[4] = t2; + $[2] = z.a.a; + $[3] = t1; } else { - t2 = $[4]; + t1 = $[3]; } - const x = t2; + const x = t1; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md index dd136000fe..2be79b9fcb 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md @@ -23,33 +23,29 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function component(a) { - const $ = useMemoCache(5); + const $ = useMemoCache(4); const c_0 = $[0] !== a; let t0; - let t1; if (c_0) { - t0 = { a }; - t1 = { a: t0 }; + t0 = { a: { a } }; $[0] = a; $[1] = t0; - $[2] = t1; } else { t0 = $[1]; - t1 = $[2]; } - const z = t1; - const c_3 = $[3] !== z.a.a; - let t2; - if (c_3) { - t2 = function () { + const z = t0; + const c_2 = $[2] !== z.a.a; + let t1; + if (c_2) { + t1 = function () { console.log(z.a.a); }; - $[3] = z.a.a; - $[4] = t2; + $[2] = z.a.a; + $[3] = t1; } else { - t2 = $[4]; + t1 = $[3]; } - const x = t2; + const x = t1; return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md index cc3012ed37..6cb1eb91ef 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md @@ -23,13 +23,12 @@ import { makeReadOnly } from "react-forget-runtime"; import { unstable_useMemoCache as useMemoCache } from "react"; // @enableEmitFreeze true function MyComponentName(props) { - const $ = useMemoCache(4); + const $ = useMemoCache(3); const c_0 = $[0] !== props.a; const c_1 = $[1] !== props.b; - let x; let y; if (c_0 || c_1) { - x = {}; + const x = {}; foo(x, props.a); foo(x, props.b); @@ -37,11 +36,9 @@ function MyComponentName(props) { y.push(x); $[0] = props.a; $[1] = props.b; - $[2] = __DEV__ ? makeReadOnly(x, "MyComponentName") : x; - $[3] = __DEV__ ? makeReadOnly(y, "MyComponentName") : y; + $[2] = __DEV__ ? makeReadOnly(y, "MyComponentName") : y; } else { - x = $[2]; - y = $[3]; + y = $[2]; } return y; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md index c7b0b34899..d12d2a8959 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md @@ -30,7 +30,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; // Should print A, B, arg, original function Component() { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = (o) => { @@ -41,20 +41,16 @@ function Component() { t0 = $[0]; } const changeF = t0; - let t1; let x; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = () => console.log("original"); - x = { f: t1 }; + x = { f: () => console.log("original") }; (console.log("A"), x)[(console.log("B"), "f")]( (changeF(x), console.log("arg"), 1) ); - $[1] = t1; - $[2] = x; + $[1] = x; } else { - t1 = $[1]; - x = $[2]; + x = $[1]; } return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constructor.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constructor.expect.md index d52bb75437..1f0f71c9d9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constructor.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constructor.expect.md @@ -22,23 +22,17 @@ import { unstable_useMemoCache as useMemoCache } from "react"; function Foo() {} function Component(props) { - const $ = useMemoCache(3); - let a; - let b; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - a = []; - b = {}; + const a = []; + const b = {}; new Foo(a, b); new Foo(b); t0 =
; - $[0] = a; - $[1] = b; - $[2] = t0; + $[0] = t0; } else { - a = $[0]; - b = $[1]; - t0 = $[2]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.expect.md index b11eba6baf..cd37d40d04 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.expect.md @@ -29,27 +29,27 @@ import { unstable_useMemoCache as useMemoCache } from "react"; import fbt from "fbt"; export function Component(props) { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let count = 0; if (props.items) { count = props.items.length; } const c_0 = $[0] !== count; let t0; - let t1; if (c_0) { - t0 = fbt._("for {count} experiences", [fbt._param("count", count)], { - hk: "nmYpm", - }); - t1 = {t0}; + t0 = ( + + {fbt._("for {count} experiences", [fbt._param("count", count)], { + hk: "nmYpm", + })} + + ); $[0] = count; $[1] = t0; - $[2] = t1; } else { t0 = $[1]; - t1 = $[2]; } - return t1; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-text-must-use-expression-container.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-text-must-use-expression-container.expect.md index eaab450093..986ed8c2f8 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-text-must-use-expression-container.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-text-must-use-expression-container.expect.md @@ -25,19 +25,19 @@ import { unstable_useMemoCache as useMemoCache } from "react"; import fbt from "fbt"; function Component(props) { - const $ = useMemoCache(2); + const $ = useMemoCache(1); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = fbt._("{value}%", [fbt._param("value", "0")], { hk: "10F5Cc" }); - t1 = ; + t0 = ( + + ); $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - return t1; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-element-content.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-element-content.expect.md index 1961f32382..67fd35327e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-element-content.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-element-content.expect.md @@ -29,49 +29,49 @@ import { unstable_useMemoCache as useMemoCache } from "react"; import fbt from "fbt"; function Component(t39) { - const $ = useMemoCache(5); + const $ = useMemoCache(4); const { name, data, icon } = t39; const c_0 = $[0] !== name; const c_1 = $[1] !== icon; const c_2 = $[2] !== data; let t0; - let t1; if (c_0 || c_1 || c_2) { - t0 = fbt._( - "{item author}{icon}{=m2}", - [ - fbt._param( - "item author", + t0 = ( + + {fbt._( + "{item author}{icon}{=m2}", + [ + fbt._param( + "item author", - {name} - ), - fbt._param( - "icon", + {name} + ), + fbt._param( + "icon", - icon - ), - fbt._implicitParam( - "=m2", - - {fbt._("{item details}", [fbt._param("item details", data)], { - hk: "4jLfVq", - })} - - ), - ], - { hk: "2HLm2j" } + icon + ), + fbt._implicitParam( + "=m2", + + {fbt._("{item details}", [fbt._param("item details", data)], { + hk: "4jLfVq", + })} + + ), + ], + { hk: "2HLm2j" } + )} + ); - t1 = {t0}; $[0] = name; $[1] = icon; $[2] = data; $[3] = t0; - $[4] = t1; } else { t0 = $[3]; - t1 = $[4]; } - return t1; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-fragment-value.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-fragment-value.expect.md index ddd997702a..38c76f4e25 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-fragment-value.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/fbtparam-with-jsx-fragment-value.expect.md @@ -27,23 +27,25 @@ import fbt from "fbt"; import { identity } from "shared-runtime"; function Component(props) { - const $ = useMemoCache(3); + const $ = useMemoCache(2); const c_0 = $[0] !== props.text; let t0; - let t1; if (c_0) { - t0 = fbt._("{value}%", [fbt._param("value", <>{identity(props.text)})], { - hk: "10F5Cc", - }); - t1 = ; + t0 = ( + {identity(props.text)})], + { hk: "10F5Cc" } + )} + /> + ); $[0] = props.text; $[1] = t0; - $[2] = t1; } else { t0 = $[1]; - t1 = $[2]; } - return t1; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-in-statement.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-in-statement.expect.md index 17636b4cec..be6d2571d7 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-in-statement.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-in-statement.expect.md @@ -22,23 +22,20 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(3); + const $ = useMemoCache(2); const c_0 = $[0] !== props; - let items; let t0; if (c_0) { - items = []; + const items = []; for (const key in props) { items.push(
{key}
); } t0 =
{items}
; $[0] = props; - $[1] = items; - $[2] = t0; + $[1] = t0; } else { - items = $[1]; - t0 = $[2]; + t0 = $[1]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md index 5c23d9aae0..9722778e08 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md @@ -28,22 +28,19 @@ import { unstable_useMemoCache as useMemoCache } from "react"; import { makeObject_Primitives, mutate, Stringify } from "shared-runtime"; function Component(_props) { - const $ = useMemoCache(2); - let results; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { const collection = [makeObject_Primitives()]; - results = []; + const results = []; for (const item of collection) { results.push(
{Stringify(mutate(item))}
); } t0 =
{results}
; - $[0] = results; - $[1] = t0; + $[0] = t0; } else { - results = $[0]; - t0 = $[1]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-reassign.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-reassign.expect.md index 6241f21588..fe9932b3cb 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-reassign.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/function-declaration-reassign.expect.md @@ -23,21 +23,17 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function component() { - const $ = useMemoCache(2); - let t0; + const $ = useMemoCache(1); let x; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = function x(a) { + x = function x(a) { a.foo(); }; - x = t0; x = {}; - $[0] = t0; - $[1] = x; + $[0] = x; } else { - t0 = $[0]; - x = $[1]; + x = $[0]; } return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/global-jsx-tag-lowered-between-mutations.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/global-jsx-tag-lowered-between-mutations.expect.md index c856c68e74..1454691048 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/global-jsx-tag-lowered-between-mutations.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/global-jsx-tag-lowered-between-mutations.expect.md @@ -25,25 +25,17 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(3); - let T0; - let t1; - let t2; + const $ = useMemoCache(1); + let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { const maybeMutable = new MaybeMutable(); - T0 = View; - t1 = maybeMutate(maybeMutable); - t2 = {t1}; - $[0] = T0; - $[1] = t1; - $[2] = t2; + t0 = {maybeMutate(maybeMutable)}; + $[0] = t0; } else { - T0 = $[0]; - t1 = $[1]; - t2 = $[2]; + t0 = $[0]; } - return t2; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md index 991f6c5a8d..88981b7834 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md @@ -24,20 +24,17 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function hoisting() { - const $ = useMemoCache(2); - let foo; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - foo = () => bar + baz; + const foo = () => bar + baz; const bar = 3; const baz = 2; t0 = foo(); - $[0] = foo; - $[1] = t0; + $[0] = t0; } else { - foo = $[0]; - t0 = $[1]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inner-memo-value-not-promoted-to-outer-scope-static.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inner-memo-value-not-promoted-to-outer-scope-static.expect.md index 7e793a43bf..09cd5b1202 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inner-memo-value-not-promoted-to-outer-scope-static.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/inner-memo-value-not-promoted-to-outer-scope-static.expect.md @@ -21,70 +21,30 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(11); + const $ = useMemoCache(2); let t1; - let T2; - let t3; - let t0; - let t4; - let T5; - let t6; - let t7; - let t8; - let t9; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { const count = new MaybeMutable(); - - T5 = View; - t6 = "\n "; - T2 = View; - t3 = "\n "; - if ($[10] === Symbol.for("react.memo_cache_sentinel")) { + let t0; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { t0 = Text; - $[10] = t0; + $[1] = t0; } else { - t0 = $[10]; + t0 = $[1]; } - t4 = "\n "; - t1 = maybeMutate(count); - t7 = {t1}; - t8 = ( - - {t3} - {t0} - {t4} - {t7} - - ); - t9 = ( - - {t6} - {t8} - + t1 = ( + + + {t0} + {maybeMutate(count)} + + ); $[0] = t1; - $[1] = T2; - $[2] = t3; - $[3] = t0; - $[4] = t4; - $[5] = T5; - $[6] = t6; - $[7] = t7; - $[8] = t8; - $[9] = t9; } else { t1 = $[0]; - T2 = $[1]; - t3 = $[2]; - t0 = $[3]; - t4 = $[4]; - T5 = $[5]; - t6 = $[6]; - t7 = $[7]; - t8 = $[8]; - t9 = $[9]; } - return t9; + return t1; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-fragment.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-fragment.expect.md index a771441c39..70f2ad78a5 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-fragment.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-fragment.expect.md @@ -26,34 +26,35 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Foo(props) { - const $ = useMemoCache(4); + const $ = useMemoCache(3); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = <>Text; - t1 =
{t0}
; + t0 = ( +
+ <>Text +
+ ); $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - const c_2 = $[2] !== props.greeting; - let t2; - if (c_2) { - t2 = ( + const c_1 = $[1] !== props.greeting; + let t1; + if (c_1) { + t1 = ( <> Hello {props.greeting} - {t1} + {t0} ); - $[2] = props.greeting; - $[3] = t2; + $[1] = props.greeting; + $[2] = t1; } else { - t2 = $[3]; + t1 = $[2]; } - return t2; + return t1; } + export const FIXTURE_ENTRYPOINT = { fn: Foo, params: ["TodoAdd"], diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression-tag-grouping.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression-tag-grouping.expect.md index e238a394e4..e54f7c880b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression-tag-grouping.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression-tag-grouping.expect.md @@ -14,24 +14,16 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(3); - let T0; - let t1; - let t2; + const $ = useMemoCache(1); + let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { const maybeMutable = new MaybeMutable(); - T0 = Foo.Bar; - t1 = maybeMutate(maybeMutable); - t2 = {t1}; - $[0] = T0; - $[1] = t1; - $[2] = t2; + t0 = {maybeMutate(maybeMutable)}; + $[0] = t0; } else { - T0 = $[0]; - t1 = $[1]; - t2 = $[2]; + t0 = $[0]; } - return t2; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression.expect.md index f37349894c..4572807f1f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-member-expression.expect.md @@ -17,19 +17,19 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(2); + const $ = useMemoCache(1); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = ; - t1 = {t0}; + t0 = ( + + + + ); $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - return t1; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-with-fbt.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-with-fbt.expect.md index d41f6bc42e..0bae6661db 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-with-fbt.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/lambda-with-fbt.expect.md @@ -42,7 +42,7 @@ import { unstable_useMemoCache as useMemoCache } from "react"; import { fbt } from "fbt"; function Component() { - const $ = useMemoCache(4); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = () => { @@ -72,21 +72,17 @@ function Component() { } const buttonLabel = t0; let t1; - let t2; - let t3; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = buttonLabel(); - t2 = ); $[3] = state; $[4] = t2; - $[5] = t3; } else { t2 = $[4]; - t3 = $[5]; } - const c_6 = $[6] !== t1; - const c_7 = $[7] !== t3; - let t4; - if (c_6 || c_7) { - t4 = ( + const c_5 = $[5] !== t1; + const c_6 = $[6] !== t2; + let t3; + if (c_5 || c_6) { + t3 = (
{t0} {t1} - {t3} + {t2}
); - $[6] = t1; + $[5] = t1; + $[6] = t2; $[7] = t3; - $[8] = t4; } else { - t4 = $[8]; + t3 = $[7]; } - return t4; + return t3; } export const FIXTURE_ENTRYPOINT = { fn: Component, diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.expect.md index 4df08363bd..7539119009 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.expect.md @@ -37,7 +37,7 @@ const ErrorView = isForgetEnabled_Fixtures() export default Renderer = isForgetEnabled_Fixtures() ? (props) => { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = ; @@ -46,22 +46,18 @@ export default Renderer = isForgetEnabled_Fixtures() t0 = $[0]; } let t1; - let t2; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = ; - t2 = ( + t1 = ( {t0} - {t1} + ); $[1] = t1; - $[2] = t2; } else { t1 = $[1]; - t2 = $[2]; } - return t2; + return t1; } : (props) => ( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-gating-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-gating-test.expect.md index 3dbf15de57..a436ab8c08 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-gating-test.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-gating-test.expect.md @@ -37,7 +37,7 @@ const ErrorView = isForgetEnabled_Fixtures() export const Renderer = isForgetEnabled_Fixtures() ? (props) => { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = ; @@ -46,22 +46,18 @@ export const Renderer = isForgetEnabled_Fixtures() t0 = $[0]; } let t1; - let t2; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = ; - t2 = ( + t1 = ( {t0} - {t1} + ); $[1] = t1; - $[2] = t2; } else { t1 = $[1]; - t2 = $[2]; } - return t2; + return t1; } : (props) => ( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-gating-test.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-gating-test.expect.md index a769591f7a..91c26e597c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-gating-test.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-gating-test.expect.md @@ -39,7 +39,7 @@ const ErrorView = isForgetEnabled_Fixtures() const Renderer = isForgetEnabled_Fixtures() ? (props) => { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = ; @@ -48,22 +48,18 @@ const Renderer = isForgetEnabled_Fixtures() t0 = $[0]; } let t1; - let t2; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = ; - t2 = ( + t1 = ( {t0} - {t1} + ); $[1] = t1; - $[2] = t2; } else { t1 = $[1]; - t2 = $[2]; } - return t2; + return t1; } : (props) => ( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md index fc7ada3dbf..e244886553 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md @@ -76,17 +76,13 @@ function cond(x) { } function testFunction(props) { - const $ = useMemoCache(5); - let a; - let b; - let c; - let d; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - a = {}; - b = {}; - c = {}; - d = {}; + let a = {}; + let b = {}; + let c = {}; + let d = {}; while (true) { const z = a; a = b; @@ -109,17 +105,9 @@ function testFunction(props) { mutate(d, null); t0 = { a, b, c, d }; - $[0] = a; - $[1] = b; - $[2] = c; - $[3] = d; - $[4] = t0; + $[0] = t0; } else { - a = $[0]; - b = $[1]; - c = $[2]; - d = $[3]; - t0 = $[4]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md index 9ed9e60ca7..35b3b83a31 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md @@ -24,24 +24,20 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Foo() { - const $ = useMemoCache(2); + const $ = useMemoCache(1); let t0; - let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = function t() { + t0 = (function t() { return function a(t25) { const x_0 = t25 === undefined ? () => {} : t25; return x_0; }; - }; - t1 = t0(); + })(); $[0] = t0; - $[1] = t1; } else { t0 = $[0]; - t1 = $[1]; } - return t1; + return t0; } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md index af6414aa51..0ec2c1dae6 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md @@ -32,14 +32,12 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; // @debug function Component(props) { - const $ = useMemoCache(4); + const $ = useMemoCache(2); const c_0 = $[0] !== props; - let x; - let y; let t0; if (c_0) { - x = {}; - y = undefined; + const x = {}; + let y = undefined; if (props.cond) { y = [props.value]; } else { @@ -50,13 +48,9 @@ function Component(props) { t0 = [x, y]; $[0] = props; - $[1] = x; - $[2] = y; - $[3] = t0; + $[1] = t0; } else { - x = $[1]; - y = $[2]; - t0 = $[3]; + t0 = $[1]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-as-dep-nested-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-as-dep-nested-scope.expect.md index a3e13940b9..98c45d00ff 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-as-dep-nested-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/primitive-as-dep-nested-scope.expect.md @@ -26,37 +26,31 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // props.b + 1 is // Correctness: // y depends on either props.b or props.b + 1 function PrimitiveAsDepNested(props) { - const $ = useMemoCache(7); + const $ = useMemoCache(5); const c_0 = $[0] !== props.b; const c_1 = $[1] !== props.a; - let x; - let y; let t2; if (c_0 || c_1) { - x = {}; + const x = {}; mutate(x); const t0 = props.b + 1; - const c_5 = $[5] !== t0; + const c_3 = $[3] !== t0; let t1; - if (c_5) { + if (c_3) { t1 = foo(t0); - $[5] = t0; - $[6] = t1; + $[3] = t0; + $[4] = t1; } else { - t1 = $[6]; + t1 = $[4]; } - y = t1; + const y = t1; mutate(x, props.a); t2 = [x, y]; $[0] = props.b; $[1] = props.a; - $[2] = x; - $[3] = y; - $[4] = t2; + $[2] = t2; } else { - x = $[2]; - y = $[3]; - t2 = $[4]; + t2 = $[2]; } return t2; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/property-call-evaluation-order.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/property-call-evaluation-order.expect.md index be25b9db33..2311c93d14 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/property-call-evaluation-order.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/property-call-evaluation-order.expect.md @@ -30,7 +30,7 @@ export const FIXTURE_ENTRYPOINT = { import { unstable_useMemoCache as useMemoCache } from "react"; // Should print A, arg, original function Component() { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = (o) => { @@ -41,18 +41,14 @@ function Component() { t0 = $[0]; } const changeF = t0; - let t1; let x; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = () => console.log("original"); - x = { f: t1 }; + x = { f: () => console.log("original") }; (console.log("A"), x).f((changeF(x), console.log("arg"), 1)); - $[1] = t1; - $[2] = x; + $[1] = x; } else { - t1 = $[1]; - x = $[2]; + x = $[1]; } return x; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reactive-scopes.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reactive-scopes.expect.md index 2eb392ebbe..b6be2399d1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reactive-scopes.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reactive-scopes.expect.md @@ -26,13 +26,12 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function f(a, b) { - const $ = useMemoCache(4); + const $ = useMemoCache(3); const c_0 = $[0] !== a.length; const c_1 = $[1] !== b; - let x; let t0; if (c_0 || c_1) { - x = []; + const x = []; if (a.length === 1) { if (b) { x.push(b); @@ -42,11 +41,9 @@ function f(a, b) { t0 =
{x}
; $[0] = a.length; $[1] = b; - $[2] = x; - $[3] = t0; + $[2] = t0; } else { - x = $[2]; - t0 = $[3]; + t0 = $[2]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps-join-uncond-scopes-cond-deps.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps-join-uncond-scopes-cond-deps.expect.md index 32ac0608f1..c16f470dc2 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps-join-uncond-scopes-cond-deps.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps-join-uncond-scopes-cond-deps.expect.md @@ -52,35 +52,30 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // This tests an // } function TestJoinCondDepsInUncondScopes(props) { - const $ = useMemoCache(6); + const $ = useMemoCache(4); const c_0 = $[0] !== props.a.b; - let x; - let y; let t0; if (c_0) { - y = {}; - const c_4 = $[4] !== props; - if (c_4) { + const y = {}; + const c_2 = $[2] !== props; + let x; + if (c_2) { x = {}; if (foo) { mutate1(x, props.a.b); } - $[4] = props; - $[5] = x; + $[2] = props; + $[3] = x; } else { - x = $[5]; + x = $[3]; } mutate2(y, props.a.b); t0 = [x, y]; $[0] = props.a.b; - $[1] = x; - $[2] = y; - $[3] = t0; + $[1] = t0; } else { - x = $[1]; - y = $[2]; - t0 = $[3]; + t0 = $[1]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx-2.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx-2.expect.md index 1bc1a61df2..7e087af9d2 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx-2.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx-2.expect.md @@ -25,26 +25,20 @@ import { unstable_useMemoCache as useMemoCache } from "react"; // @Pass runMutab function foo() {} function Component(props) { - const $ = useMemoCache(3); - let a; - let b; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - a = []; - b = {}; + const a = []; + const b = {}; foo(a, b); if (foo()) { } foo(a, b); t0 =
; - $[0] = a; - $[1] = b; - $[2] = t0; + $[0] = t0; } else { - a = $[0]; - b = $[1]; - t0 = $[2]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx.expect.md index 58f433d676..5a152ab5d6 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/ssa-call-jsx.expect.md @@ -22,24 +22,18 @@ import { unstable_useMemoCache as useMemoCache } from "react"; function foo() {} function Component(props) { - const $ = useMemoCache(3); - let a; - let b; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - a = []; - b = {}; + const a = []; + const b = {}; foo(a, b); foo(a, b); t0 =
; - $[0] = a; - $[1] = b; - $[2] = t0; + $[0] = t0; } else { - a = $[0]; - b = $[1]; - t0 = $[2]; + t0 = $[0]; } return t0; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-accessed-outside-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-accessed-outside-scope.expect.md index 465c30ab0a..0756387243 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-accessed-outside-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-accessed-outside-scope.expect.md @@ -15,27 +15,19 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(4); + const $ = useMemoCache(2); const c_0 = $[0] !== props; let t0; - let t1; - let t2; if (c_0) { const maybeMutable = new MaybeMutable(); const x = props; - t0 = x; - t1 = maybeMutate(maybeMutable); - t2 = [t0, t1]; + t0 = [x, maybeMutate(maybeMutable)]; $[0] = props; $[1] = t0; - $[2] = t1; - $[3] = t2; } else { t0 = $[1]; - t1 = $[2]; - t2 = $[3]; } - return t2; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-property-load-accessed-outside-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-property-load-accessed-outside-scope.expect.md index 6d2813a631..bba789c0f4 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-property-load-accessed-outside-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/temporary-property-load-accessed-outside-scope.expect.md @@ -15,27 +15,19 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(4); + const $ = useMemoCache(2); const c_0 = $[0] !== props.value; let t0; - let t1; - let t2; if (c_0) { const maybeMutable = new MaybeMutable(); const x = props.value; - t0 = x; - t1 = maybeMutate(maybeMutable); - t2 = [t0, t1]; + t0 = [x, maybeMutate(maybeMutable)]; $[0] = props.value; $[1] = t0; - $[2] = t1; - $[3] = t2; } else { t0 = $[1]; - t1 = $[2]; - t2 = $[3]; } - return t2; + return t0; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-test-field-load-binary-op.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-test-field-load-binary-op.expect.md index 55ddeaa04e..014c13003c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-test-field-load-binary-op.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-test-field-load-binary-op.expect.md @@ -21,7 +21,7 @@ function component() { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function component() { - const $ = useMemoCache(3); + const $ = useMemoCache(2); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = makeSomePrimitive(); @@ -30,17 +30,13 @@ function component() { t0 = $[0]; } let t1; - let t2; if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = makeSomePrimitive(); - t2 = { u: t0, v: t1 }; + t1 = { u: t0, v: makeSomePrimitive() }; $[1] = t1; - $[2] = t2; } else { t1 = $[1]; - t2 = $[2]; } - const x = t2; + const x = t1; const u = x.u; const v = x.v; if (u > v) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md index 3d0b9aa09a..7a8126d509 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/uninitialized-declaration-in-reactive-scope.expect.md @@ -16,22 +16,16 @@ function Component(props) { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; function Component(props) { - const $ = useMemoCache(3); - let y; - let x; + const $ = useMemoCache(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - x = mutate(); - + const x = mutate(); + let y; foo(x); t0 = [y, x]; - $[0] = y; - $[1] = x; - $[2] = t0; + $[0] = t0; } else { - y = $[0]; - x = $[1]; - t0 = $[2]; + t0 = $[0]; } return t0; }