-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
ghstack-source-id: 6a49070add645384c4b764db4749f829a1992f43 Pull Request resolved: #31771
- Loading branch information
1 parent
1520802
commit 32b81a3
Showing
6 changed files
with
118 additions
and
4 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
41 changes: 41 additions & 0 deletions
41
...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,41 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component(props) { | ||
const x = props.x(); | ||
return <div>{x}</div>; | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
function Component(props) { | ||
const $ = _c(4); | ||
let t0; | ||
if ($[0] !== props.x) { | ||
t0 = props.x(); | ||
$[0] = props.x; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
const x = t0; | ||
let t1; | ||
if ($[2] !== x) { | ||
t1 = <div>{x}</div>; | ||
$[2] = x; | ||
$[3] = t1; | ||
} else { | ||
t1 = $[3]; | ||
} | ||
return t1; | ||
} | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) Fixture not implemented |
4 changes: 4 additions & 0 deletions
4
...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,4 @@ | ||
function Component(props) { | ||
const x = props.x(); | ||
return <div>{x}</div>; | ||
} |