Skip to content

Commit

Permalink
refactor(es/minifier): Don't create invalid nodes (#6191)
Browse files Browse the repository at this point in the history
**Description:**

A sequential expression should have at leat two elements.
  • Loading branch information
kdy1 committed Oct 21, 2022
1 parent bb5bf9a commit ea03ce1
Show file tree
Hide file tree
Showing 21 changed files with 662 additions and 338 deletions.
2 changes: 2 additions & 0 deletions crates/swc/tests/tsc.rs
Expand Up @@ -413,6 +413,8 @@ fn matrix(input: &Path) -> Vec<TestUnitData> {
}

fn compile(output: &Path, test_unit_data: TestUnitData) {
let _testing = testing::init();

let cm = test_unit_data.cm;

let c = Compiler::new(cm.clone());
Expand Down
17 changes: 9 additions & 8 deletions crates/swc_ecma_minifier/src/compress/optimize/conditionals.rs
Expand Up @@ -3,6 +3,7 @@ use std::mem::swap;
use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::ExprRefExt;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_utils::{ExprExt, ExprFactory, StmtExt, StmtLike};

use super::Optimizer;
Expand Down Expand Up @@ -282,7 +283,10 @@ where
};

let new_expr = self.compress_similar_cons_alt(&mut stmt.test, cons, alt, true);

if let Some(v) = new_expr {
debug_assert_valid(&v);

self.changed = true;
*s = Stmt::Expr(ExprStmt {
span: stmt.span,
Expand Down Expand Up @@ -346,6 +350,9 @@ where
alt: &mut Expr,
is_for_if_stmt: bool,
) -> Option<Expr> {
debug_assert_valid(cons);
debug_assert_valid(alt);

if cons.eq_ignore_span(alt) && !matches!(&*cons, Expr::Yield(..) | Expr::Fn(..)) {
report_change!("conditionals: cons is same as alt");
return Some(Expr::Seq(SeqExpr {
Expand Down Expand Up @@ -578,10 +585,7 @@ where
span: DUMMY_SP,
left: test.take(),
op: op!("||"),
right: Box::new(Expr::Seq(SeqExpr {
span: alt.span,
exprs: alt.exprs.take(),
})),
right: Expr::from_exprs(alt.exprs.take()),
}));
Some(Expr::Seq(SeqExpr {
span: DUMMY_SP,
Expand All @@ -601,10 +605,7 @@ where
span: DUMMY_SP,
left: test.take(),
op: op!("&&"),
right: Box::new(Expr::Seq(SeqExpr {
span: cons.span,
exprs: cons.exprs.take(),
})),
right: Expr::from_exprs(cons.exprs.take()),
}));
Some(Expr::Seq(SeqExpr {
span: DUMMY_SP,
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/if_return.rs
@@ -1,5 +1,6 @@
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_utils::{undefined, StmtExt, StmtLike};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};

Expand Down Expand Up @@ -55,6 +56,8 @@ where

for stmt in stmts.iter_mut() {
self.merge_nested_if_returns(stmt, terminates);

debug_assert_valid(&*stmt);
}

if terminates || is_fn_body {
Expand All @@ -69,12 +72,18 @@ where
match s {
Stmt::Block(s) => {
self.merge_if_returns(&mut s.stmts, terminate, false);

debug_assert_valid(&*s);
}
Stmt::If(s) => {
self.merge_nested_if_returns(&mut s.cons, can_work);

debug_assert_valid(&s.cons);

if let Some(alt) = s.alt.as_deref_mut() {
self.merge_nested_if_returns(alt, can_work);

debug_assert_valid(&*alt);
}
}
_ => {}
Expand Down Expand Up @@ -366,6 +375,8 @@ where
}

if let Some(mut cur) = cur {
self.normalize_expr(&mut cur);

match &*cur {
Expr::Seq(seq)
if !should_preserve_last_return
Expand Down
14 changes: 7 additions & 7 deletions crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Expand Up @@ -450,7 +450,6 @@ where
Callee::Super(_) | Callee::Import(_) => return,
Callee::Expr(e) => &mut **e,
};
self.normalize_expr(callee);

if self.ctx.dont_invoke_iife {
log_abort!("iife: Inline is prevented");
Expand Down Expand Up @@ -582,10 +581,7 @@ where
exprs.push(body.take());

report_change!("inline: Inlining a call to an arrow function");
*e = Expr::Seq(SeqExpr {
span: DUMMY_SP,
exprs,
});
*e = *Expr::from_exprs(exprs);
e.visit_mut_with(self);
}
}
Expand Down Expand Up @@ -986,7 +982,9 @@ where
};
self.merge_sequences_in_seq_expr(&mut e);

return Some(Expr::Seq(e));
let mut e = Expr::Seq(e);
self.normalize_expr(&mut e);
return Some(e);
}
_ => {}
}
Expand All @@ -1008,7 +1006,9 @@ where
};
self.merge_sequences_in_seq_expr(&mut e);

Some(Expr::Seq(e))
let mut e = Expr::Seq(e);
self.normalize_expr(&mut e);
Some(e)
}

fn can_be_inlined_for_iife(&self, arg: &Expr) -> bool {
Expand Down

1 comment on commit ea03ce1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ea03ce1 Previous: 725b118 Ratio
es/full/bugs-1 398463 ns/iter (± 55268) 437998 ns/iter (± 20427) 0.91
es/full/minify/libraries/antd 1868943117 ns/iter (± 45585662) 2097944493 ns/iter (± 27070343) 0.89
es/full/minify/libraries/d3 406908770 ns/iter (± 12517998) 416658375 ns/iter (± 19928755) 0.98
es/full/minify/libraries/echarts 1563597958 ns/iter (± 26366033) 1789134089 ns/iter (± 32617028) 0.87
es/full/minify/libraries/jquery 104233204 ns/iter (± 5175822) 118354147 ns/iter (± 1861497) 0.88
es/full/minify/libraries/lodash 123567758 ns/iter (± 12799412) 140474033 ns/iter (± 3041316) 0.88
es/full/minify/libraries/moment 61881135 ns/iter (± 1737794) 71525820 ns/iter (± 738952) 0.87
es/full/minify/libraries/react 20314058 ns/iter (± 618381) 24756386 ns/iter (± 571072) 0.82
es/full/minify/libraries/terser 319006642 ns/iter (± 11946993) 296570773 ns/iter (± 7919194) 1.08
es/full/minify/libraries/three 549245767 ns/iter (± 16560201) 553019094 ns/iter (± 26127325) 0.99
es/full/minify/libraries/typescript 3349974689 ns/iter (± 27497769) 3937085919 ns/iter (± 37768311) 0.85
es/full/minify/libraries/victory 795527820 ns/iter (± 11032740) 856357482 ns/iter (± 31401938) 0.93
es/full/minify/libraries/vue 153140409 ns/iter (± 6168836) 177523192 ns/iter (± 3161882) 0.86
es/full/codegen/es3 33051 ns/iter (± 822) 39690 ns/iter (± 1182) 0.83
es/full/codegen/es5 33108 ns/iter (± 340) 39805 ns/iter (± 523) 0.83
es/full/codegen/es2015 33129 ns/iter (± 485) 39765 ns/iter (± 557) 0.83
es/full/codegen/es2016 33108 ns/iter (± 625) 39826 ns/iter (± 1513) 0.83
es/full/codegen/es2017 33118 ns/iter (± 2032) 39464 ns/iter (± 2445) 0.84
es/full/codegen/es2018 33058 ns/iter (± 1088) 38012 ns/iter (± 1367) 0.87
es/full/codegen/es2019 33091 ns/iter (± 540) 39092 ns/iter (± 1187) 0.85
es/full/codegen/es2020 33126 ns/iter (± 1066) 39791 ns/iter (± 1026) 0.83
es/full/all/es3 195621948 ns/iter (± 8763949) 228657605 ns/iter (± 4966356) 0.86
es/full/all/es5 195549501 ns/iter (± 18593400) 215772922 ns/iter (± 5054573) 0.91
es/full/all/es2015 155019851 ns/iter (± 9870892) 174009898 ns/iter (± 2525259) 0.89
es/full/all/es2016 161167224 ns/iter (± 10887323) 172748665 ns/iter (± 3710628) 0.93
es/full/all/es2017 149127295 ns/iter (± 8886387) 171968377 ns/iter (± 3988163) 0.87
es/full/all/es2018 145730106 ns/iter (± 6280584) 168984539 ns/iter (± 4133700) 0.86
es/full/all/es2019 145170740 ns/iter (± 6189363) 168491880 ns/iter (± 5968416) 0.86
es/full/all/es2020 139824954 ns/iter (± 4517560) 162280641 ns/iter (± 5464987) 0.86
es/full/parser 734790 ns/iter (± 24807) 846855 ns/iter (± 33627) 0.87
es/full/base/fixer 26174 ns/iter (± 621) 31545 ns/iter (± 397) 0.83
es/full/base/resolver_and_hygiene 91176 ns/iter (± 3685) 113310 ns/iter (± 4441) 0.80
serialization of ast node 214 ns/iter (± 6) 256 ns/iter (± 7) 0.84
serialization of serde 216 ns/iter (± 16) 260 ns/iter (± 2) 0.83

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.