[hir] Put useMemo inlining behind a flag
It's still a WIP so disable behind a flag for now.
Sathya Gunasekaran committed
Apr 11, 2023 at 13:41 UTC
2d8b36467c768af5cc63845f73f2beaeddfeb59c
20 files changed
+34
-3
compiler/forget/src/CompilerPipeline.ts
+4
-2
@@ -61,8 +61,10 @@ export function* run(
61
const hir = lower(func, env).unwrap();
62
yield log({ kind: "hir", name: "HIR", value: hir });
63
64
- inlineUseMemo(hir);
65
- yield log({ kind: "hir", name: "RewriteUseMemo", value: hir });
64
+ if (config?.inlineUseMemo) {
65
+ inlineUseMemo(hir);
66
+ yield log({ kind: "hir", name: "RewriteUseMemo", value: hir });
67
+ }
68
69
mergeConsecutiveBlocks(hir);
70
yield log({ kind: "hir", name: "MergeConsecutiveBlocks", value: hir });
compiler/forget/src/HIR/Environment.ts
+1
@@ -40,6 +40,7 @@ const HOOK_PATTERN = /^_?use/;
40
export type EnvironmentConfig = Partial<{
41
customHooks: Map<string, Hook>;
42
memoizeJsxElements: boolean;
43
+ inlineUseMemo: boolean;
44
}>;
45
46
export class Environment {
compiler/forget/src/__tests__/compiler-test.ts
+1
@@ -54,6 +54,7 @@ describe("React Forget", () => {
54
},
55
],
56
]),
57
+ inlineUseMemo: options.environment?.inlineUseMemo ?? false,
58
},
59
logger: null,
60
gating: options.gating,
compiler/forget/src/__tests__/disableMemoizeJsxElements-test.ts
+1
@@ -50,6 +50,7 @@ describe("React Forget (Disable memoization of JSX elements)", () => {
50
},
51
],
52
]),
53
+ inlineUseMemo: options.environment?.inlineUseMemo ?? false,
54
},
55
logger: null,
56
gating: options.gating,
compiler/forget/src/__tests__/fixtures/compiler/useMemo-if-else-multiple-return.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function Component(props) {
7
const x = useMemo(() => {
8
if (props.cond) {
@@ -17,6 +18,7 @@ function Component(props) {
18
## Code
19
20
```javascript
21
+// @inlineUseMemo
22
function Component(props) {
23
const $ = React.unstable_useMemoCache(5);
24
if (props.cond) {
compiler/forget/src/__tests__/fixtures/compiler/useMemo-if-else-multiple-return.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function Component(props) {
3
const x = useMemo(() => {
4
if (props.cond) {
compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function Component(props) {
7
const [a, b] = useMemo(() => {
8
const items = [];
@@ -17,6 +18,7 @@ function Component(props) {
18
## Code
19
20
```javascript
21
+// @inlineUseMemo
22
function Component(props) {
23
const $ = React.unstable_useMemoCache(10);
24
const c_0 = $[0] !== props.a;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function Component(props) {
3
const [a, b] = useMemo(() => {
4
const items = [];
compiler/forget/src/__tests__/fixtures/compiler/useMemo-labeled-statement-unconditional-return.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function Component(props) {
7
const x = useMemo(() => {
8
label: {
@@ -16,6 +17,7 @@ function Component(props) {
17
## Code
18
19
```javascript
20
+// @inlineUseMemo
21
function Component(props) {
22
const t19 = props.value;
23
const x = t19;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-labeled-statement-unconditional-return.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function Component(props) {
3
const x = useMemo(() => {
4
label: {
compiler/forget/src/__tests__/fixtures/compiler/useMemo-logical.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function Component(props) {
7
const x = useMemo(() => props.a && props.b);
8
return x;
@@ -12,6 +13,7 @@ function Component(props) {
13
## Code
14
15
```javascript
16
+// @inlineUseMemo
17
function Component(props) {
18
const t32 = props.a && props.b;
19
const x = t32;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-logical.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function Component(props) {
3
const x = useMemo(() => props.a && props.b);
4
return x;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-multiple-if-else.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function Component(props) {
7
const x = useMemo(() => {
8
let y = [];
@@ -22,6 +23,7 @@ function Component(props) {
23
## Code
24
25
```javascript
26
+// @inlineUseMemo
27
function Component(props) {
28
const $ = React.unstable_useMemoCache(2);
29
const c_0 = $[0] !== props;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-multiple-if-else.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function Component(props) {
3
const x = useMemo(() => {
4
let y = [];
compiler/forget/src/__tests__/fixtures/compiler/useMemo-simple.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function component(a) {
7
let x = useMemo(() => [a], [a]);
8
return <Foo x={x}></Foo>;
@@ -12,6 +13,7 @@ function component(a) {
13
## Code
14
15
```javascript
16
+// @inlineUseMemo
17
function component(a) {
18
const $ = React.unstable_useMemoCache(4);
19
const c_0 = $[0] !== a;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-simple.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function component(a) {
3
let x = useMemo(() => [a], [a]);
4
return <Foo x={x}></Foo>;
compiler/forget/src/__tests__/fixtures/compiler/useMemo-switch-no-fallthrough.expect.md
+2
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @inlineUseMemo
6
function Component(props) {
7
const x = useMemo(() => {
8
switch (props.key) {
@@ -21,6 +22,7 @@ function Component(props) {
22
## Code
23
24
```javascript
25
+// @inlineUseMemo
26
function Component(props) {
27
bb8: switch (props.key) {
28
case "key": {
compiler/forget/src/__tests__/fixtures/compiler/useMemo-switch-no-fallthrough.js
+1
@@ -1,3 +1,4 @@
1
+// @inlineUseMemo
2
function Component(props) {
3
const x = useMemo(() => {
4
switch (props.key) {
compiler/forget/src/__tests__/hir-test.ts
+1
@@ -45,6 +45,7 @@ describe("React Forget (HIR version)", () => {
45
},
46
],
47
]),
48
+ inlineUseMemo: options.environment?.inlineUseMemo ?? false,
49
});
50
51
if (compileResult.isErr()) {
compiler/forget/src/__tests__/test-utils/generateTestsFromFixtures.ts
+5
-1
@@ -91,6 +91,7 @@ export default function generateTestsFromFixtures(
91
let debug = false;
92
let enableOnlyOnUseForgetDirective = false;
93
let gating: GatingOptions | null = null;
94
+ let inlineUseMemo = true;
95
96
if (inputFile != null) {
97
input = fs.readFileSync(inputFile, "utf8");
@@ -111,13 +112,16 @@ export default function generateTestsFromFixtures(
112
importSpecifierName: "isForgetEnabled_Fixtures",
113
};
114
}
115
+ if (lines[0]!.indexOf("@inlineUseMemo") !== -1) {
116
+ inlineUseMemo = true;
117
+ }
118
}
119
120
testCommand(basename, () => {
121
let receivedOutput;
122
if (input !== null) {
123
receivedOutput = transform(input, basename, {
120
- environment: null,
124
+ environment: { inlineUseMemo },
125
logger: null,
126
debug,
127
enableOnlyOnUseForgetDirective,