Fix comments, extend fixtures
Joe Savona committed
Dec 15, 2023 at 15:19 UTC
c6200d1a2bdfaee1b91f9a6364e7eecc060d5989
5 files changed
+48
-10
compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts
+13
-10
@@ -130,12 +130,12 @@ export function dropManualMemoization(func: HIRFunction): void {
130
* $1 = LoadGlobal useMemo // load the useMemo global (dead code)
131
* $2 = FunctionExpression ... // memo function
132
* $3 = ArrayExpression [ ... ] // deps array (dead code)
133
- * $5 = Call $2 () // invoke the memo function itself
134
- * $4 = Memoize $5 // preserve memo information
133
+ * .. = Memoize ... // memoize dependencies
134
+ * $4 = Call $2 () // invoke the memo function itself
135
+ * .. = Memoize $4 // preserve memo information
136
*
136
- * Note that we synthesize a new temporary for the call ($5) and use
137
- * the original lvalue for the result of the Memoize instruction, so that
138
- * we don't have to rewrite subsequent instructions.
137
+ * Note that Memoize does not produce a result and is called for its side
138
+ * effects only.
139
*/
140
nextInstructions =
141
nextInstructions ?? block.instructions.slice(0, i);
@@ -194,13 +194,13 @@ export function dropManualMemoization(func: HIRFunction): void {
194
* $1 = LoadGlobal useCallback
195
* $2 = FunctionExpression ... // the callback being memoized
196
* $3 = ArrayExpression ... // deps array
197
- * $3 = Call $1 ( $2, $3 ) // invoke useCallback
197
+ * $4 = Call $1 ( $2, $3 ) // invoke useCallback
198
*
199
* after:
200
* $1 = LoadGlobal useCallback // dead code
201
* $2 = FunctionExpression ... // the callback being memoized
202
* $3 = ArrayExpression ... // deps array (dead code)
203
- * $3 = LoadLocal $2 // reference the function
203
+ * $4 = LoadLocal $2 // reference the function
204
*/
205
if (fn.kind === "Identifier") {
206
instr.value = {
@@ -227,15 +227,18 @@ export function dropManualMemoization(func: HIRFunction): void {
227
* $1 = LoadGlobal useCallback // dead code
228
* $2 = FunctionExpression ... // the callback being memoized
229
* $3 = ArrayExpression ... // deps array (dead code)
230
- * $3 = LoadLocal $2 // reference the function
230
+ * $4 = LoadLocal $2 // reference the function
231
*
232
* With flag enabled:
233
* $1 = LoadGlobal useCallback // dead code
234
* $2 = FunctionExpression ... // the callback being memoized
235
* $3 = ArrayExpression ... // deps array (dead code)
236
- * $3 = Memoize $2 // reference the function
236
+ * .. = Memoize ... // memoize dependencies
237
+ * $n = Memoize $2 // reference the function
238
+ * $4 = LoadLocal $2 // reference the function
239
*
238
- * Note the s/LoadLocal/Memoize/
240
+ * Note that Memoize does not produce a result and is called for its side effects
241
+ * only.
242
*/
243
const functionExpression = functions.get(fn.identifier.id);
244
if (functionExpression !== undefined) {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-mabye-modified-free-variable-dont-preserve-memoization-guarantees.expect.md
+6
@@ -25,6 +25,9 @@ function Component(props) {
25
mutate(x, free, part);
26
return x;
27
}, [props.value]);
28
+
29
+ identity(free);
30
+ identity(part);
31
return object;
32
}
33
@@ -60,6 +63,9 @@ function Component(props) {
63
mutate(x, free, part);
64
t39 = x;
65
const object = t39;
66
+
67
+ identity(free);
68
+ identity(part);
69
return object;
70
}
71
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-mabye-modified-free-variable-dont-preserve-memoization-guarantees.js
+3
@@ -21,6 +21,9 @@ function Component(props) {
21
mutate(x, free, part);
22
return x;
23
}, [props.value]);
24
+
25
+ identity(free);
26
+ identity(part);
27
return object;
28
}
29
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-mabye-modified-free-variable-preserve-memoization-guarantees.expect.md
+15
@@ -12,16 +12,27 @@ import {
12
} from "shared-runtime";
13
14
function Component(props) {
15
+ // With the feature enabled these variables are inferred as frozen as of
16
+ // the useMemo call
17
const free = makeObject_Primitives();
18
const free2 = makeObject_Primitives();
19
const part = free2.part;
20
+
21
+ // Thus their mutable range ends prior to this hook call, and both the above
22
+ // values and the useMemo block value can be memoized
23
useHook();
24
+
25
const object = useMemo(() => {
26
const x = makeObject_Primitives();
27
x.value = props.value;
28
mutate(x, free, part);
29
return x;
30
}, [props.value]);
31
+
32
+ // These calls should be inferred as non-mutating due to the above freeze inference
33
+ identity(free);
34
+ identity(part);
35
+
36
return object;
37
}
38
@@ -63,6 +74,7 @@ function Component(props) {
74
}
75
const free2 = t1;
76
const part = free2.part;
77
+
78
useHook();
79
let t39;
80
let x;
@@ -77,6 +89,9 @@ function Component(props) {
89
}
90
t39 = x;
91
const object = t39;
92
+
93
+ identity(free);
94
+ identity(part);
95
return object;
96
}
97
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useMemo-mabye-modified-free-variable-preserve-memoization-guarantees.js
+11
@@ -8,16 +8,27 @@ import {
8
} from "shared-runtime";
9
10
function Component(props) {
11
+ // With the feature enabled these variables are inferred as frozen as of
12
+ // the useMemo call
13
const free = makeObject_Primitives();
14
const free2 = makeObject_Primitives();
15
const part = free2.part;
16
+
17
+ // Thus their mutable range ends prior to this hook call, and both the above
18
+ // values and the useMemo block value can be memoized
19
useHook();
20
+
21
const object = useMemo(() => {
22
const x = makeObject_Primitives();
23
x.value = props.value;
24
mutate(x, free, part);
25
return x;
26
}, [props.value]);
27
+
28
+ // These calls should be inferred as non-mutating due to the above freeze inference
29
+ identity(free);
30
+ identity(part);
31
+
32
return object;
33
}
34