@samitouri / QOS-React-2 / commits / d649940c27

Test case showing lack of independent memoization of object properties

Joe Savona committed Dec 18, 2022 at 12:14 UTC d649940c2754f73b1098c71dcc1c9ac9574c5710
2 files changed +141
compiler/forget/src/__tests__/fixtures/hir/_bug_independently-memoize-object-property.expect.md new
+128
@@ -0,0 +1,128 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function foo(a, b, c) {
6 + const x = { a: a };
7 + // TODO @josephsavona: this array *should* be memoized independently from `x`,
8 + // similar to the behavior if we extract into a variable as with `z` below:
9 + x.y = [b, c];
10 +
11 + const y = { a: a };
12 + // this array correctly memoizes independently
13 + const z = [b, c];
14 + y.y = z;
15 +
16 + return [x, y];
17 +}
18 +
19 +```
20 +
21 +## HIR
22 +
23 +```
24 +bb0:
25 + [1] Const mutate x$11_@0:TObject[1:3] = Object { a: read a$8 }
26 + [2] Reassign mutate x$11_@0.y[1:3] = Array [read b$9, read c$10]
27 + [3] Const mutate y$12_@1:TObject[3:6] = Object { a: read a$8 }
28 + [4] Const mutate z$13_@2 = Array [read b$9, read c$10]
29 + [5] Reassign mutate y$12_@1.y[3:6] = read z$13_@2
30 + [6] Const mutate t12$14_@3 = Array [read x$11_@0:TObject, read y$12_@1:TObject]
31 + [7] Return freeze t12$14_@3
32 +```
33 +
34 +## Reactive Scopes
35 +
36 +```
37 +function foo(
38 + a,
39 + b,
40 + c,
41 +) {
42 + scope @0 [1:3] deps=[read a$8, read b$9, read c$10] out=[x$11_@0] {
43 + [1] Const mutate x$11_@0:TObject[1:3] = Object { a: read a$8 }
44 + [2] Reassign mutate x$11_@0.y[1:3] = Array [read b$9, read c$10]
45 + }
46 + scope @1 [3:6] deps=[read a$8, read b$9, read c$10] out=[y$12_@1] {
47 + [3] Const mutate y$12_@1:TObject[3:6] = Object { a: read a$8 }
48 + scope @2 [4:5] deps=[read b$9, read c$10] out=[] {
49 + [4] Const mutate z$13_@2 = Array [read b$9, read c$10]
50 + }
51 + [5] Reassign mutate y$12_@1.y[3:6] = read z$13_@2
52 + }
53 + scope @3 [6:7] deps=[read x$11_@0:TObject, read y$12_@1:TObject] out=[$14_@3] {
54 + [6] Const mutate $14_@3 = Array [read x$11_@0:TObject, read y$12_@1:TObject]
55 + }
56 + return freeze $14_@3
57 +}
58 +
59 +```
60 +
61 +## Code
62 +
63 +```javascript
64 +function foo$0(a$8, b$9, c$10) {
65 + const $ = React.useMemoCache();
66 + const c_0 = $[0] !== a$8;
67 + const c_1 = $[1] !== b$9;
68 + const c_2 = $[2] !== c$10;
69 + let x$11;
70 + if (c_0 || c_1 || c_2) {
71 + x$11 = {
72 + a: a$8,
73 + };
74 + x$11.y = [b$9, c$10];
75 + $[0] = a$8;
76 + $[1] = b$9;
77 + $[2] = c$10;
78 + $[3] = x$11;
79 + } else {
80 + x$11 = $[3];
81 + }
82 +
83 + const c_4 = $[4] !== a$8;
84 + const c_5 = $[5] !== b$9;
85 + const c_6 = $[6] !== c$10;
86 + let y$12;
87 +
88 + if (c_4 || c_5 || c_6) {
89 + y$12 = {
90 + a: a$8,
91 + };
92 + const c_8 = $[8] !== b$9;
93 + const c_9 = $[9] !== c$10;
94 +
95 + if (c_8 || c_9) {
96 + const z$13 = [b$9, c$10];
97 + $[8] = b$9;
98 + $[9] = c$10;
99 + } else {
100 + }
101 +
102 + y$12.y = z$13;
103 + $[4] = a$8;
104 + $[5] = b$9;
105 + $[6] = c$10;
106 + $[7] = y$12;
107 + } else {
108 + y$12 = $[7];
109 + }
110 +
111 + const c_10 = $[10] !== x$11;
112 + const c_11 = $[11] !== y$12;
113 + let t12$14;
114 +
115 + if (c_10 || c_11) {
116 + t12$14 = [x$11, y$12];
117 + $[10] = x$11;
118 + $[11] = y$12;
119 + $[12] = t12$14;
120 + } else {
121 + t12$14 = $[12];
122 + }
123 +
124 + return t12$14;
125 +}
126 +
127 +```
128 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/_bug_independently-memoize-object-property.js new
+13
@@ -0,0 +1,13 @@
1 +function foo(a, b, c) {
2 + const x = { a: a };
3 + // TODO @josephsavona: this array *should* be memoized independently from `x`,
4 + // similar to the behavior if we extract into a variable as with `z` below:
5 + x.y = [b, c];
6 +
7 + const y = { a: a };
8 + // this array correctly memoizes independently
9 + const z = [b, c];
10 + y.y = z;
11 +
12 + return [x, y];
13 +}