[hir] Put useMemo inlining behind a flag

It's still a WIP so disable behind a flag for now.
This commit is contained in:
Sathya Gunasekaran
2023-04-11 13:41:33 +01:00
parent 67b0cf8a8c
commit 2d8b36467c
20 changed files with 34 additions and 3 deletions
+4 -2
View File
@@ -61,8 +61,10 @@ export function* run(
const hir = lower(func, env).unwrap();
yield log({ kind: "hir", name: "HIR", value: hir });
inlineUseMemo(hir);
yield log({ kind: "hir", name: "RewriteUseMemo", value: hir });
if (config?.inlineUseMemo) {
inlineUseMemo(hir);
yield log({ kind: "hir", name: "RewriteUseMemo", value: hir });
}
mergeConsecutiveBlocks(hir);
yield log({ kind: "hir", name: "MergeConsecutiveBlocks", value: hir });
+1
View File
@@ -40,6 +40,7 @@ const HOOK_PATTERN = /^_?use/;
export type EnvironmentConfig = Partial<{
customHooks: Map<string, Hook>;
memoizeJsxElements: boolean;
inlineUseMemo: boolean;
}>;
export class Environment {
@@ -54,6 +54,7 @@ describe("React Forget", () => {
},
],
]),
inlineUseMemo: options.environment?.inlineUseMemo ?? false,
},
logger: null,
gating: options.gating,
@@ -50,6 +50,7 @@ describe("React Forget (Disable memoization of JSX elements)", () => {
},
],
]),
inlineUseMemo: options.environment?.inlineUseMemo ?? false,
},
logger: null,
gating: options.gating,
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
if (props.cond) {
@@ -17,6 +18,7 @@ function Component(props) {
## Code
```javascript
// @inlineUseMemo
function Component(props) {
const $ = React.unstable_useMemoCache(5);
if (props.cond) {
@@ -1,3 +1,4 @@
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
if (props.cond) {
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function Component(props) {
const [a, b] = useMemo(() => {
const items = [];
@@ -17,6 +18,7 @@ function Component(props) {
## Code
```javascript
// @inlineUseMemo
function Component(props) {
const $ = React.unstable_useMemoCache(10);
const c_0 = $[0] !== props.a;
@@ -1,3 +1,4 @@
// @inlineUseMemo
function Component(props) {
const [a, b] = useMemo(() => {
const items = [];
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
label: {
@@ -16,6 +17,7 @@ function Component(props) {
## Code
```javascript
// @inlineUseMemo
function Component(props) {
const t19 = props.value;
const x = t19;
@@ -1,3 +1,4 @@
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
label: {
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => props.a && props.b);
return x;
@@ -12,6 +13,7 @@ function Component(props) {
## Code
```javascript
// @inlineUseMemo
function Component(props) {
const t32 = props.a && props.b;
const x = t32;
@@ -1,3 +1,4 @@
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => props.a && props.b);
return x;
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
let y = [];
@@ -22,6 +23,7 @@ function Component(props) {
## Code
```javascript
// @inlineUseMemo
function Component(props) {
const $ = React.unstable_useMemoCache(2);
const c_0 = $[0] !== props;
@@ -1,3 +1,4 @@
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
let y = [];
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function component(a) {
let x = useMemo(() => [a], [a]);
return <Foo x={x}></Foo>;
@@ -12,6 +13,7 @@ function component(a) {
## Code
```javascript
// @inlineUseMemo
function component(a) {
const $ = React.unstable_useMemoCache(4);
const c_0 = $[0] !== a;
@@ -1,3 +1,4 @@
// @inlineUseMemo
function component(a) {
let x = useMemo(() => [a], [a]);
return <Foo x={x}></Foo>;
@@ -2,6 +2,7 @@
## Input
```javascript
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
switch (props.key) {
@@ -21,6 +22,7 @@ function Component(props) {
## Code
```javascript
// @inlineUseMemo
function Component(props) {
bb8: switch (props.key) {
case "key": {
@@ -1,3 +1,4 @@
// @inlineUseMemo
function Component(props) {
const x = useMemo(() => {
switch (props.key) {
@@ -45,6 +45,7 @@ describe("React Forget (HIR version)", () => {
},
],
]),
inlineUseMemo: options.environment?.inlineUseMemo ?? false,
});
if (compileResult.isErr()) {
@@ -91,6 +91,7 @@ export default function generateTestsFromFixtures(
let debug = false;
let enableOnlyOnUseForgetDirective = false;
let gating: GatingOptions | null = null;
let inlineUseMemo = true;
if (inputFile != null) {
input = fs.readFileSync(inputFile, "utf8");
@@ -111,13 +112,16 @@ export default function generateTestsFromFixtures(
importSpecifierName: "isForgetEnabled_Fixtures",
};
}
if (lines[0]!.indexOf("@inlineUseMemo") !== -1) {
inlineUseMemo = true;
}
}
testCommand(basename, () => {
let receivedOutput;
if (input !== null) {
receivedOutput = transform(input, basename, {
environment: null,
environment: { inlineUseMemo },
logger: null,
debug,
enableOnlyOnUseForgetDirective,