From 48e08c42be4ce37269b1ddc49ab349c1b74807cf Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 6 Mar 2024 09:28:01 -0800 Subject: [PATCH] Fixtures where unmemoized value invalidates later scopes These fixtures demonstrate how currently, even if the dependency of a scope doesn't get memoized (ie the scope gets pruned), we don't remove later scopes that depend on that value. Those later scopes will always invalidate, so we might as well remove them. --- ...opes-whose-deps-invalidate-array.expect.md | 63 ++++++++++++++++ ...rune-scopes-whose-deps-invalidate-array.js | 16 ++++ ...scopes-whose-deps-invalidate-jsx.expect.md | 73 +++++++++++++++++++ .../prune-scopes-whose-deps-invalidate-jsx.js | 17 +++++ ...scopes-whose-deps-invalidate-new.expect.md | 67 +++++++++++++++++ .../prune-scopes-whose-deps-invalidate-new.js | 18 +++++ ...pes-whose-deps-invalidate-object.expect.md | 63 ++++++++++++++++ ...une-scopes-whose-deps-invalidate-object.js | 16 ++++ 8 files changed, 333 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.js create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.js diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.expect.md new file mode 100644 index 0000000000..9b74851e98 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import { useHook } from "shared-runtime"; + +function Component(props) { + const x = []; + useHook(); // intersperse a hook call to prevent memoization of x + x.push(props.value); + + const y = [x]; + + return [y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +import { useHook } from "shared-runtime"; + +function Component(props) { + const $ = useMemoCache(4); + const x = []; + useHook(); + x.push(props.value); + let t0; + if ($[0] !== x) { + t0 = [x]; + $[0] = x; + $[1] = t0; + } else { + t0 = $[1]; + } + const y = t0; + let t1; + if ($[2] !== y) { + t1 = [y]; + $[2] = y; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +### Eval output +(kind: ok) [[["sathya"]]] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.js new file mode 100644 index 0000000000..f8b17c1b2b --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-array.js @@ -0,0 +1,16 @@ +import { useHook } from "shared-runtime"; + +function Component(props) { + const x = []; + useHook(); // intersperse a hook call to prevent memoization of x + x.push(props.value); + + const y = [x]; + + return [y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.expect.md new file mode 100644 index 0000000000..c9eaed6cb3 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.expect.md @@ -0,0 +1,73 @@ + +## Input + +```javascript +import { useHook } from "shared-runtime"; + +function Component(props) { + const o = {}; + const x =
{props.value}
; // create within the range of x to group with x + useHook(); // intersperse a hook call to prevent memoization of x + o.value = props.value; + + const y =
{x}
; + + return
{y}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +import { useHook } from "shared-runtime"; + +function Component(props) { + const $ = useMemoCache(6); + const o = {}; + let t0; + if ($[0] !== props.value) { + t0 =
{props.value}
; + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + useHook(); + o.value = props.value; + let t1; + if ($[2] !== x) { + t1 =
{x}
; + $[2] = x; + $[3] = t1; + } else { + t1 = $[3]; + } + const y = t1; + let t2; + if ($[4] !== y) { + t2 =
{y}
; + $[4] = y; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +### Eval output +(kind: ok)
sathya
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.js new file mode 100644 index 0000000000..bdcd931905 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-jsx.js @@ -0,0 +1,17 @@ +import { useHook } from "shared-runtime"; + +function Component(props) { + const o = {}; + const x =
{props.value}
; // create within the range of x to group with x + useHook(); // intersperse a hook call to prevent memoization of x + o.value = props.value; + + const y =
{x}
; + + return
{y}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.expect.md new file mode 100644 index 0000000000..3cbff1f94f --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +import { useHook } from "shared-runtime"; + +function Component(props) { + const x = new Foo(); + useHook(); // intersperse a hook call to prevent memoization of x + x.value = props.value; + + const y = { x }; + + return { y }; +} + +class Foo {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +import { useHook } from "shared-runtime"; + +function Component(props) { + const $ = useMemoCache(4); + const x = new Foo(); + useHook(); + x.value = props.value; + let t0; + if ($[0] !== x) { + t0 = { x }; + $[0] = x; + $[1] = t0; + } else { + t0 = $[1]; + } + const y = t0; + let t1; + if ($[2] !== y) { + t1 = { y }; + $[2] = y; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +class Foo {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +### Eval output +(kind: ok) {"y":{"x":{"value":"sathya"}}} \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.js new file mode 100644 index 0000000000..688cac14d8 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-new.js @@ -0,0 +1,18 @@ +import { useHook } from "shared-runtime"; + +function Component(props) { + const x = new Foo(); + useHook(); // intersperse a hook call to prevent memoization of x + x.value = props.value; + + const y = { x }; + + return { y }; +} + +class Foo {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.expect.md new file mode 100644 index 0000000000..5b87500316 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import { useHook } from "shared-runtime"; + +function Component(props) { + const x = {}; + useHook(); // intersperse a hook call to prevent memoization of x + x.value = props.value; + + const y = { x }; + + return { y }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +import { useHook } from "shared-runtime"; + +function Component(props) { + const $ = useMemoCache(4); + const x = {}; + useHook(); + x.value = props.value; + let t0; + if ($[0] !== x) { + t0 = { x }; + $[0] = x; + $[1] = t0; + } else { + t0 = $[1]; + } + const y = t0; + let t1; + if ($[2] !== y) { + t1 = { y }; + $[2] = y; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +}; + +``` + +### Eval output +(kind: ok) {"y":{"x":{"value":"sathya"}}} \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.js new file mode 100644 index 0000000000..1052a39edc --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/prune-scopes-whose-deps-invalidate-object.js @@ -0,0 +1,16 @@ +import { useHook } from "shared-runtime"; + +function Component(props) { + const x = {}; + useHook(); // intersperse a hook call to prevent memoization of x + x.value = props.value; + + const y = { x }; + + return { y }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "sathya" }], +};