-
Notifications
You must be signed in to change notification settings - Fork 47.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize method calls w props receiver (#31775)
Redo of #31771 without ghstack
- Loading branch information
1 parent
1520802
commit a1b3bd0
Showing
7 changed files
with
164 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
compiler/packages/babel-plugin-react-compiler/src/Optimization/OptimizePropsMethodCalls.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {HIRFunction, isPropsType} from '../HIR'; | ||
|
||
/** | ||
* Converts method calls into regular calls where the receiver is the props object: | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* // INPUT | ||
* props.foo(); | ||
* | ||
* // OUTPUT | ||
* const t0 = props.foo; | ||
* t0(); | ||
* ``` | ||
* | ||
* Counter example: | ||
* | ||
* Here the receiver is `props.foo`, not the props object, so we don't rewrite it: | ||
* | ||
* // INPUT | ||
* props.foo.bar(); | ||
* | ||
* // OUTPUT | ||
* props.foo.bar(); | ||
* ``` | ||
*/ | ||
export function optimizePropsMethodCalls(fn: HIRFunction): void { | ||
for (const [, block] of fn.body.blocks) { | ||
for (let i = 0; i < block.instructions.length; i++) { | ||
const instr = block.instructions[i]!; | ||
if ( | ||
instr.value.kind === 'MethodCall' && | ||
isPropsType(instr.value.receiver.identifier) | ||
) { | ||
instr.value = { | ||
kind: 'CallExpression', | ||
callee: instr.value.property, | ||
args: instr.value.args, | ||
loc: instr.value.loc, | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-outlining-child-stored-in-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...eact-compiler/src/__tests__/fixtures/compiler/props-method-dependency.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @compilationMode(infer) | ||
import {useMemo} from 'react'; | ||
import {ValidateMemoization} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const x = useMemo(() => props.x(), [props.x]); | ||
return <ValidateMemoization inputs={[props.x]} output={x} />; | ||
} | ||
|
||
const f = () => ['React']; | ||
const g = () => ['Compiler']; | ||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{x: () => ['React']}], | ||
sequentialRenders: [{x: f}, {x: g}, {x: g}, {x: f}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @compilationMode(infer) | ||
import { useMemo } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
const $ = _c(7); | ||
let t0; | ||
let t1; | ||
if ($[0] !== props.x) { | ||
t1 = props.x(); | ||
$[0] = props.x; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
t0 = t1; | ||
const x = t0; | ||
let t2; | ||
if ($[2] !== props.x) { | ||
t2 = [props.x]; | ||
$[2] = props.x; | ||
$[3] = t2; | ||
} else { | ||
t2 = $[3]; | ||
} | ||
let t3; | ||
if ($[4] !== t2 || $[5] !== x) { | ||
t3 = <ValidateMemoization inputs={t2} output={x} />; | ||
$[4] = t2; | ||
$[5] = x; | ||
$[6] = t3; | ||
} else { | ||
t3 = $[6]; | ||
} | ||
return t3; | ||
} | ||
|
||
const f = () => ["React"]; | ||
const g = () => ["Compiler"]; | ||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ x: () => ["React"] }], | ||
sequentialRenders: [{ x: f }, { x: g }, { x: g }, { x: f }], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div>{"inputs":["[[ function params=0 ]]"],"output":["React"]}</div> | ||
<div>{"inputs":["[[ function params=0 ]]"],"output":["Compiler"]}</div> | ||
<div>{"inputs":["[[ function params=0 ]]"],"output":["Compiler"]}</div> | ||
<div>{"inputs":["[[ function params=0 ]]"],"output":["React"]}</div> |
16 changes: 16 additions & 0 deletions
16
...es/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/props-method-dependency.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @compilationMode(infer) | ||
import {useMemo} from 'react'; | ||
import {ValidateMemoization} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const x = useMemo(() => props.x(), [props.x]); | ||
return <ValidateMemoization inputs={[props.x]} output={x} />; | ||
} | ||
|
||
const f = () => ['React']; | ||
const g = () => ['Compiler']; | ||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{x: () => ['React']}], | ||
sequentialRenders: [{x: f}, {x: g}, {x: g}, {x: f}], | ||
}; |