diff --git a/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep-nested-scope.expect.md b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep-nested-scope.expect.md new file mode 100644 index 0000000000..4dc4c0d670 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep-nested-scope.expect.md @@ -0,0 +1,79 @@ + +## Input + +```javascript +// bar(props.b) is an allocating expression that produces a primitive, which means +// that Forget should memoize it. +// Correctness: +// - y depends on either bar(props.b) or bar(props.b) + 1 +function AllocatingPrimitiveAsDepNested(props) { + let x = {}; + mutate(x); + let y = foo(bar(props.b) + 1); + mutate(x, props.a); + return [x, y]; +} + +``` + +## Code + +```javascript +// bar(props.b) is an allocating expression that produces a primitive, which means +// that Forget should memoize it. +// Correctness: +// - y depends on either bar(props.b) or bar(props.b) + 1 +function AllocatingPrimitiveAsDepNested(props) { + const $ = React.unstable_useMemoCache(11); + const c_0 = $[0] !== props.b; + const c_1 = $[1] !== props.a; + let x; + let y; + if (c_0 || c_1) { + x = {}; + mutate(x); + const c_4 = $[4] !== props.b; + let t0; + if (c_4) { + t0 = bar(props.b); + $[4] = props.b; + $[5] = t0; + } else { + t0 = $[5]; + } + const t1 = t0 + 1; + const c_6 = $[6] !== t1; + let t2; + if (c_6) { + t2 = foo(t1); + $[6] = t1; + $[7] = t2; + } else { + t2 = $[7]; + } + y = t2; + mutate(x, props.a); + $[0] = props.b; + $[1] = props.a; + $[2] = x; + $[3] = y; + } else { + x = $[2]; + y = $[3]; + } + const c_8 = $[8] !== x; + const c_9 = $[9] !== y; + let t3; + if (c_8 || c_9) { + t3 = [x, y]; + $[8] = x; + $[9] = y; + $[10] = t3; + } else { + t3 = $[10]; + } + return t3; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep-nested-scope.js b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep-nested-scope.js new file mode 100644 index 0000000000..765c7ef79f --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep-nested-scope.js @@ -0,0 +1,11 @@ +// bar(props.b) is an allocating expression that produces a primitive, which means +// that Forget should memoize it. +// Correctness: +// - y depends on either bar(props.b) or bar(props.b) + 1 +function AllocatingPrimitiveAsDepNested(props) { + let x = {}; + mutate(x); + let y = foo(bar(props.b) + 1); + mutate(x, props.a); + return [x, y]; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.expect.md b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.expect.md index 4d64b373f8..7836f17a83 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.expect.md @@ -11,14 +11,6 @@ function AllocatingPrimitiveAsDep(props) { return y; } -function PrimitiveAsDepNested(props) { - let x = {}; - mutate(x); - let y = foo(bar(props.b) + 1); - mutate(x, props.a); - return [x, y]; -} - ``` ## Code @@ -53,57 +45,5 @@ function AllocatingPrimitiveAsDep(props) { return y; } -function PrimitiveAsDepNested(props) { - const $ = React.unstable_useMemoCache(11); - const c_0 = $[0] !== props.b; - const c_1 = $[1] !== props.a; - let x; - let y; - if (c_0 || c_1) { - x = {}; - mutate(x); - const c_4 = $[4] !== props.b; - let t0; - if (c_4) { - t0 = bar(props.b); - $[4] = props.b; - $[5] = t0; - } else { - t0 = $[5]; - } - const t1 = t0 + 1; - const c_6 = $[6] !== t1; - let t2; - if (c_6) { - t2 = foo(t1); - $[6] = t1; - $[7] = t2; - } else { - t2 = $[7]; - } - y = t2; - mutate(x, props.a); - $[0] = props.b; - $[1] = props.a; - $[2] = x; - $[3] = y; - } else { - x = $[2]; - y = $[3]; - } - const c_8 = $[8] !== x; - const c_9 = $[9] !== y; - let t3; - if (c_8 || c_9) { - t3 = [x, y]; - $[8] = x; - $[9] = y; - $[10] = t3; - } else { - t3 = $[10]; - } - return t3; -} - ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.js b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.js index ba0095d7f3..c7ef86f7c2 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.js +++ b/compiler/forget/src/__tests__/fixtures/hir/allocating-primitive-as-dep.js @@ -6,11 +6,3 @@ function AllocatingPrimitiveAsDep(props) { let y = foo(bar(props).b + 1); return y; } - -function PrimitiveAsDepNested(props) { - let x = {}; - mutate(x); - let y = foo(bar(props.b) + 1); - mutate(x, props.a); - return [x, y]; -} diff --git a/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep-nested-scope.expect.md b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep-nested-scope.expect.md new file mode 100644 index 0000000000..924b8fa2af --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep-nested-scope.expect.md @@ -0,0 +1,72 @@ + +## Input + +```javascript +// props.b + 1 is an non-allocating expression, which means Forget can +// emit it trivially and repeatedly (e.g. no need to memoize props.b + 1 +// separately from props.b) +// Correctness: +// y depends on either props.b or props.b + 1 +function PrimitiveAsDepNested(props) { + let x = {}; + mutate(x); + let y = foo(props.b + 1); + mutate(x, props.a); + return [x, y]; +} + +``` + +## Code + +```javascript +// props.b + 1 is an non-allocating expression, which means Forget can +// emit it trivially and repeatedly (e.g. no need to memoize props.b + 1 +// separately from props.b) +// Correctness: +// y depends on either props.b or props.b + 1 +function PrimitiveAsDepNested(props) { + const $ = React.unstable_useMemoCache(9); + const c_0 = $[0] !== props.b; + const c_1 = $[1] !== props.a; + let x; + let y; + if (c_0 || c_1) { + x = {}; + mutate(x); + const t0 = props.b + 1; + const c_4 = $[4] !== t0; + let t1; + if (c_4) { + t1 = foo(t0); + $[4] = t0; + $[5] = t1; + } else { + t1 = $[5]; + } + y = t1; + mutate(x, props.a); + $[0] = props.b; + $[1] = props.a; + $[2] = x; + $[3] = y; + } else { + x = $[2]; + y = $[3]; + } + const c_6 = $[6] !== x; + const c_7 = $[7] !== y; + let t2; + if (c_6 || c_7) { + t2 = [x, y]; + $[6] = x; + $[7] = y; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep-nested-scope.js b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep-nested-scope.js new file mode 100644 index 0000000000..19299e9d6e --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep-nested-scope.js @@ -0,0 +1,12 @@ +// props.b + 1 is an non-allocating expression, which means Forget can +// emit it trivially and repeatedly (e.g. no need to memoize props.b + 1 +// separately from props.b) +// Correctness: +// y depends on either props.b or props.b + 1 +function PrimitiveAsDepNested(props) { + let x = {}; + mutate(x); + let y = foo(props.b + 1); + mutate(x, props.a); + return [x, y]; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.expect.md b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.expect.md index 7db41c5a0f..43cfdd5627 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.expect.md @@ -12,14 +12,6 @@ function PrimitiveAsDep(props) { return y; } -function PrimitiveAsDepNested(props) { - let x = {}; - mutate(x); - let y = foo(props.b + 1); - mutate(x, props.a); - return [x, y]; -} - ``` ## Code @@ -46,48 +38,5 @@ function PrimitiveAsDep(props) { return y; } -function PrimitiveAsDepNested(props) { - const $ = React.unstable_useMemoCache(9); - const c_0 = $[0] !== props.b; - const c_1 = $[1] !== props.a; - let x; - let y; - if (c_0 || c_1) { - x = {}; - mutate(x); - const t0 = props.b + 1; - const c_4 = $[4] !== t0; - let t1; - if (c_4) { - t1 = foo(t0); - $[4] = t0; - $[5] = t1; - } else { - t1 = $[5]; - } - y = t1; - mutate(x, props.a); - $[0] = props.b; - $[1] = props.a; - $[2] = x; - $[3] = y; - } else { - x = $[2]; - y = $[3]; - } - const c_6 = $[6] !== x; - const c_7 = $[7] !== y; - let t2; - if (c_6 || c_7) { - t2 = [x, y]; - $[6] = x; - $[7] = y; - $[8] = t2; - } else { - t2 = $[8]; - } - return t2; -} - ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.js b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.js index ca8b2e987f..1c1d23f15f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.js +++ b/compiler/forget/src/__tests__/fixtures/hir/primitive-as-dep.js @@ -7,11 +7,3 @@ function PrimitiveAsDep(props) { let y = foo(props.b + 1); return y; } - -function PrimitiveAsDepNested(props) { - let x = {}; - mutate(x); - let y = foo(props.b + 1); - mutate(x, props.a); - return [x, y]; -}