Skip to content

Commit e74929c

Browse files
authored
perf(es/minifier): Improve arrow function inlining cost analysis (#10093)
**Description:** This PR fixes the cost analysis of arrow functions by making it use `Option<u8>` instead of `bool`.
1 parent 1425b56 commit e74929c

File tree

1 file changed

+16
-14
lines changed
  • crates/swc_ecma_minifier/src/compress/optimize

1 file changed

+16
-14
lines changed

Diff for: crates/swc_ecma_minifier/src/compress/optimize/inline.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl Optimizer<'_> {
250250
}) => arg.is_lit(),
251251
Expr::This(..) => usage.is_fn_local,
252252
Expr::Arrow(arr) => {
253-
is_arrow_simple_enough_for_copy(arr)
253+
is_arrow_simple_enough_for_copy(arr).map_or(false, |cost| cost <= 8)
254254
&& !(usage.property_mutation_count > 0
255255
|| usage.executed_multiple_time
256256
|| usage.used_as_arg && ref_count > 1)
@@ -906,9 +906,9 @@ impl Optimizer<'_> {
906906
}
907907
}
908908

909-
fn is_arrow_simple_enough_for_copy(e: &ArrowExpr) -> bool {
909+
fn is_arrow_simple_enough_for_copy(e: &ArrowExpr) -> Option<u8> {
910910
if e.is_async {
911-
return false;
911+
return None;
912912
}
913913

914914
match &*e.body {
@@ -917,32 +917,34 @@ fn is_arrow_simple_enough_for_copy(e: &ArrowExpr) -> bool {
917917
}
918918
}
919919

920-
fn is_arrow_body_simple_enough_for_copy(e: &Expr) -> bool {
920+
fn is_arrow_body_simple_enough_for_copy(e: &Expr) -> Option<u8> {
921921
match e {
922-
Expr::Ident(..) | Expr::Lit(..) => return true,
923-
Expr::Member(MemberExpr { prop, .. }) if !prop.is_computed() => return true,
924-
Expr::Unary(u) => return is_arrow_body_simple_enough_for_copy(&u.arg),
922+
Expr::Ident(..) | Expr::Lit(..) => return Some(1),
923+
Expr::Member(MemberExpr { prop, .. }) if !prop.is_computed() => return Some(3),
924+
Expr::Unary(u) => return Some(is_arrow_body_simple_enough_for_copy(&u.arg)? + 1),
925925

926926
Expr::Bin(b) => {
927-
return is_arrow_body_simple_enough_for_copy(&b.left)
928-
&& is_arrow_body_simple_enough_for_copy(&b.right)
927+
return Some(
928+
is_arrow_body_simple_enough_for_copy(&b.left)?
929+
+ is_arrow_body_simple_enough_for_copy(&b.right)?
930+
+ 2,
931+
)
929932
}
930933
_ => {}
931934
}
932935

933-
false
936+
None
934937
}
935938

936-
fn is_block_stmt_of_fn_simple_enough_for_copy(b: &BlockStmt) -> bool {
939+
fn is_block_stmt_of_fn_simple_enough_for_copy(b: &BlockStmt) -> Option<u8> {
937940
if b.stmts.len() == 1 {
938941
if let Stmt::Return(ret) = &b.stmts[0] {
939942
return ret
940943
.arg
941944
.as_deref()
942-
.map(is_arrow_body_simple_enough_for_copy)
943-
.unwrap_or(true);
945+
.map_or(Some(0), is_arrow_body_simple_enough_for_copy);
944946
}
945947
}
946948

947-
false
949+
None
948950
}

0 commit comments

Comments
 (0)