mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[codegen] Elide final return stmt if no value
This is a random driveby improvement. I realized that we can eliminate `return` statements if a) they have no value and b) they are in the top-level block. Functions implicitly return at that point — there can't be any succeeding instructions anyway — so we can save bytes in the output.
This commit is contained in:
@@ -94,9 +94,11 @@ class CodegenVisitor
|
||||
t.SwitchCase
|
||||
>
|
||||
{
|
||||
depth: number = 0;
|
||||
temp: Map<IdentifierId, t.Expression> = new Map();
|
||||
|
||||
enterBlock(): t.Statement[] {
|
||||
this.depth++;
|
||||
return [];
|
||||
}
|
||||
visitValue(
|
||||
@@ -190,6 +192,11 @@ class CodegenVisitor
|
||||
case "return": {
|
||||
if (terminal.value !== null) {
|
||||
return t.returnStatement(terminal.value);
|
||||
} else if (this.depth === 1) {
|
||||
// A return at the top-level of a function must be the last instruction,
|
||||
// and functions implicitly return after the last instruction of the top-level.
|
||||
// Elide the return.
|
||||
return t.emptyStatement();
|
||||
} else {
|
||||
return t.returnStatement();
|
||||
}
|
||||
@@ -223,6 +230,7 @@ class CodegenVisitor
|
||||
}
|
||||
}
|
||||
leaveBlock(block: t.Statement[]): t.Statement {
|
||||
this.depth--;
|
||||
return t.blockStatement(block);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,9 +140,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mutate$0(x$1, y$2) {
|
||||
return;
|
||||
}
|
||||
function mutate$0(x$1, y$2) {}
|
||||
|
||||
```
|
||||
|
||||
@@ -60,7 +60,6 @@ function f$0() {
|
||||
x$1 = x$1 + 1;
|
||||
x$1 = x$1 + 1;
|
||||
x$1 = x$1 >>> 1;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
@@ -100,7 +99,6 @@ flowchart TB
|
||||
function g$0(a$1) {
|
||||
a$1.c.b = a$1.b.c + 1;
|
||||
a$1.c.b = a$1.b.c * 2;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -38,9 +38,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -83,7 +83,6 @@ function foo$0(a$1, b$2, c$3) {
|
||||
bb7: if (c$3) break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -260,9 +260,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -288,9 +286,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mayMutate$0() {
|
||||
return;
|
||||
}
|
||||
function mayMutate$0() {}
|
||||
|
||||
```
|
||||
|
||||
@@ -38,9 +38,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -52,7 +52,6 @@ function Component$0() {
|
||||
const b$2 = a$1;
|
||||
useFreeze$3(a$1);
|
||||
foo$4(b$2);
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
@@ -79,9 +78,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function useFreeze$0() {
|
||||
return;
|
||||
}
|
||||
function useFreeze$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -107,9 +104,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0(x$1) {
|
||||
return;
|
||||
}
|
||||
function foo$0(x$1) {}
|
||||
|
||||
```
|
||||
|
||||
@@ -42,9 +42,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function useFreeze$0() {
|
||||
return;
|
||||
}
|
||||
function useFreeze$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -70,9 +68,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -80,9 +80,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function useFreeze$0(x$1) {
|
||||
return;
|
||||
}
|
||||
function useFreeze$0(x$1) {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -108,9 +106,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function call$0(x$1) {
|
||||
return;
|
||||
}
|
||||
function call$0(x$1) {}
|
||||
|
||||
```
|
||||
|
||||
+2
-6
@@ -132,9 +132,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function useFreeze$0(x$1) {
|
||||
return;
|
||||
}
|
||||
function useFreeze$0(x$1) {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -160,9 +158,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function call$0(x$1) {
|
||||
return;
|
||||
}
|
||||
function call$0(x$1) {}
|
||||
|
||||
```
|
||||
|
||||
@@ -56,9 +56,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function compute$0() {
|
||||
return;
|
||||
}
|
||||
function compute$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -84,9 +82,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mutate$0() {
|
||||
return;
|
||||
}
|
||||
function mutate$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -112,9 +108,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -140,9 +134,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -85,9 +85,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function compute$0() {
|
||||
return;
|
||||
}
|
||||
function compute$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -113,9 +111,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -141,9 +137,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
|
||||
@@ -50,9 +50,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function compute$0() {
|
||||
return;
|
||||
}
|
||||
function compute$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -78,9 +76,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -106,9 +102,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -88,9 +88,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function compute$0() {
|
||||
return;
|
||||
}
|
||||
function compute$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -116,9 +114,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -144,9 +140,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
|
||||
@@ -75,8 +75,6 @@ function foo$0(a$1, b$2, c$3) {
|
||||
y$4.push(c$3);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -244,9 +244,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function f$0() {
|
||||
return;
|
||||
}
|
||||
function f$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -272,9 +270,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function g$0() {
|
||||
return;
|
||||
}
|
||||
function g$0() {}
|
||||
|
||||
```
|
||||
|
||||
@@ -62,9 +62,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mutate$0(x$1, y$2) {
|
||||
return;
|
||||
}
|
||||
function mutate$0(x$1, y$2) {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -90,9 +88,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function cond$0(x$1) {
|
||||
return;
|
||||
}
|
||||
function cond$0(x$1) {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -247,7 +243,6 @@ function Component$0(props$1) {
|
||||
}
|
||||
|
||||
mutate$7(d$5, null);
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -58,9 +58,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mutate$0(x$1, y$2) {
|
||||
return;
|
||||
}
|
||||
function mutate$0(x$1, y$2) {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -174,7 +172,6 @@ function Component$0(props$1) {
|
||||
}
|
||||
|
||||
mutate$8(x$6, null);
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -57,9 +57,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mutate$0() {
|
||||
return;
|
||||
}
|
||||
function mutate$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -85,9 +83,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function cond$0() {
|
||||
return;
|
||||
}
|
||||
function cond$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -227,7 +223,6 @@ function Component$0(props$1) {
|
||||
}
|
||||
|
||||
mutate$6(d$5, null);
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -189,7 +189,6 @@ function Component$0(props$1) {
|
||||
}
|
||||
|
||||
x$2;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -39,9 +39,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function mutate$0() {
|
||||
return;
|
||||
}
|
||||
function mutate$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -41,9 +41,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -38,9 +38,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function foo$0() {
|
||||
return;
|
||||
}
|
||||
function foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -110,7 +110,6 @@ function foo$0() {
|
||||
}
|
||||
|
||||
y$2 = x$1;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -78,7 +78,6 @@ function foo$0() {
|
||||
}
|
||||
|
||||
y$2 = x$1;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -83,8 +83,6 @@ function foo$0() {
|
||||
} else {
|
||||
let z$4 = x$1;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -36,9 +36,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Foo$0() {
|
||||
return;
|
||||
}
|
||||
function Foo$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
|
||||
@@ -40,9 +40,7 @@ flowchart TB
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function log$0() {
|
||||
return;
|
||||
}
|
||||
function log$0() {}
|
||||
|
||||
```
|
||||
## HIR
|
||||
@@ -119,7 +117,6 @@ function Foo$0(cond$1) {
|
||||
}
|
||||
|
||||
log$4(str$2);
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -91,7 +91,6 @@ function foo$0() {
|
||||
}
|
||||
|
||||
let x$4 = y$1;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -41,7 +41,6 @@ flowchart TB
|
||||
function foo$0() {
|
||||
let x$1 = 1;
|
||||
let y$2 = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -67,8 +67,6 @@ function foo$0() {
|
||||
bb1: if (y$2) {
|
||||
let z$3 = x$1 + y$2;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -134,7 +134,6 @@ function foo$0() {
|
||||
}
|
||||
|
||||
let y$9 = x$1;
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -191,8 +191,6 @@ function foo$0(x$1) {
|
||||
y$2 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -94,7 +94,6 @@ function foo$0(a$1, b$2, c$3, d$4) {
|
||||
}
|
||||
|
||||
d$4();
|
||||
return;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user