From f7f7a88e19674e6dec8c60ec22601ef347113636 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 7 Apr 2023 16:34:51 -0700 Subject: [PATCH] Example suboptimal memoization with non-inlined useMemo --- ...seMemo-independently-memoizeable.expect.md | 61 +++++++++++++++++++ .../useMemo-independently-memoizeable.js | 9 +++ 2 files changed, 70 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.js diff --git a/compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.expect.md new file mode 100644 index 0000000000..fb6918b129 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +function Component(props) { + const [a, b] = useMemo(() => { + const items = []; + const a = makeObject(props.a); + const b = makeObject(props.b); + return [a, b]; + }); + return [a, b]; +} + +``` + +## Code + +```javascript +function Component(props) { + const $ = React.unstable_useMemoCache(7); + const c_0 = $[0] !== props.a; + let t0; + if (c_0) { + t0 = () => { + const items = []; + const a = makeObject(props.a); + const b = makeObject(props.b); + return [a, b]; + }; + $[0] = props.a; + $[1] = t0; + } else { + t0 = $[1]; + } + const c_2 = $[2] !== t0; + let t1; + if (c_2) { + t1 = t0(); + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + const [a_0, b_0] = t1; + const c_4 = $[4] !== a_0; + const c_5 = $[5] !== b_0; + let t2; + if (c_4 || c_5) { + t2 = [a_0, b_0]; + $[4] = a_0; + $[5] = b_0; + $[6] = t2; + } else { + t2 = $[6]; + } + return t2; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.js b/compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.js new file mode 100644 index 0000000000..4f9f5f39c1 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.js @@ -0,0 +1,9 @@ +function Component(props) { + const [a, b] = useMemo(() => { + const items = []; + const a = makeObject(props.a); + const b = makeObject(props.b); + return [a, b]; + }); + return [a, b]; +}