From 4b27df9f123262161e06f242a4d5098364a9844f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 27 May 2022 15:48:08 +0900 Subject: [PATCH] perf(es/minifier): Make more passes parallel (#4821) --- .../src/compress/optimize/if_return.rs | 54 +------------------ .../src/compress/optimize/mod.rs | 2 - .../src/compress/pure/if_return.rs | 53 ++++++++++++++++++ .../src/compress/pure/mod.rs | 2 + crates/swc_ecma_minifier/tests/postponed.txt | 1 - .../compress/functions/empty_body/output.js | 3 +- 6 files changed, 57 insertions(+), 58 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs b/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs index 49f031ba48b..f87820c8026 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/if_return.rs @@ -1,6 +1,6 @@ use swc_common::{util::take::Take, Spanned, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_utils::{prepend_stmt, undefined, StmtExt, StmtLike}; +use swc_ecma_utils::{undefined, StmtExt, StmtLike}; use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; use super::Optimizer; @@ -41,58 +41,6 @@ where } } - pub(super) fn merge_else_if(&mut self, s: &mut IfStmt) { - if let Some(Stmt::If(IfStmt { - span: span_of_alt, - test: test_of_alt, - cons: cons_of_alt, - alt: Some(alt_of_alt), - .. - })) = s.alt.as_deref_mut() - { - match &**cons_of_alt { - Stmt::Return(..) | Stmt::Continue(ContinueStmt { label: None, .. }) => {} - _ => return, - } - - match &mut **alt_of_alt { - Stmt::Block(..) => {} - Stmt::Expr(..) => { - *alt_of_alt = Box::new(Stmt::Block(BlockStmt { - span: DUMMY_SP, - stmts: vec![*alt_of_alt.take()], - })); - } - _ => { - return; - } - } - - self.changed = true; - report_change!("if_return: Merging `else if` into `else`"); - - match &mut **alt_of_alt { - Stmt::Block(alt_of_alt) => { - prepend_stmt( - &mut alt_of_alt.stmts, - Stmt::If(IfStmt { - span: *span_of_alt, - test: test_of_alt.take(), - cons: cons_of_alt.take(), - alt: None, - }), - ); - } - - _ => { - unreachable!() - } - } - - s.alt = Some(alt_of_alt.take()); - } - } - pub(super) fn merge_if_returns( &mut self, stmts: &mut Vec, diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index 862893bb600..55927ad2eab 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -2039,8 +2039,6 @@ where self.negate_if_stmt(n); self.merge_nested_if(n); - - self.merge_else_if(n); } #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] diff --git a/crates/swc_ecma_minifier/src/compress/pure/if_return.rs b/crates/swc_ecma_minifier/src/compress/pure/if_return.rs index 96fb747fcd5..d9b75e02413 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/if_return.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/if_return.rs @@ -1,5 +1,6 @@ use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; +use swc_ecma_utils::prepend_stmt; use super::Pure; use crate::compress::util::{is_fine_for_if_cons, negate}; @@ -125,4 +126,56 @@ impl Pure<'_> { *stmts = new; } + + pub(super) fn merge_else_if(&mut self, s: &mut IfStmt) { + if let Some(Stmt::If(IfStmt { + span: span_of_alt, + test: test_of_alt, + cons: cons_of_alt, + alt: Some(alt_of_alt), + .. + })) = s.alt.as_deref_mut() + { + match &**cons_of_alt { + Stmt::Return(..) | Stmt::Continue(ContinueStmt { label: None, .. }) => {} + _ => return, + } + + match &mut **alt_of_alt { + Stmt::Block(..) => {} + Stmt::Expr(..) => { + *alt_of_alt = Box::new(Stmt::Block(BlockStmt { + span: DUMMY_SP, + stmts: vec![*alt_of_alt.take()], + })); + } + _ => { + return; + } + } + + self.changed = true; + report_change!("if_return: Merging `else if` into `else`"); + + match &mut **alt_of_alt { + Stmt::Block(alt_of_alt) => { + prepend_stmt( + &mut alt_of_alt.stmts, + Stmt::If(IfStmt { + span: *span_of_alt, + test: test_of_alt.take(), + cons: cons_of_alt.take(), + alt: None, + }), + ); + } + + _ => { + unreachable!() + } + } + + s.alt = Some(alt_of_alt.take()); + } + } } diff --git a/crates/swc_ecma_minifier/src/compress/pure/mod.rs b/crates/swc_ecma_minifier/src/compress/pure/mod.rs index c70ab33344b..ab41485b793 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/mod.rs @@ -459,6 +459,8 @@ impl VisitMut for Pure<'_> { s.visit_mut_children_with(self); self.optimize_expr_in_bool_ctx(&mut s.test, false); + + self.merge_else_if(s); } fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) { diff --git a/crates/swc_ecma_minifier/tests/postponed.txt b/crates/swc_ecma_minifier/tests/postponed.txt index 8f97135c785..2b032457e86 100644 --- a/crates/swc_ecma_minifier/tests/postponed.txt +++ b/crates/swc_ecma_minifier/tests/postponed.txt @@ -216,7 +216,6 @@ export/name_cache_mangle_export_default_class/input.js export/name_cache_mangle_export_default_function/input.js export/name_cache_mangle_local_import_and_export_aliases/input.js functions/duplicate_arg_var/input.js -functions/empty_body/input.js functions/hoist_funs/input.js functions/hoist_funs_strict/input.js functions/inline_1/input.js diff --git a/crates/swc_ecma_minifier/tests/terser/compress/functions/empty_body/output.js b/crates/swc_ecma_minifier/tests/terser/compress/functions/empty_body/output.js index da459e589e6..b19cffb3e46 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/functions/empty_body/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/functions/empty_body/output.js @@ -1,4 +1,3 @@ function f() { - function noop() {} - return noop; + return function noop() {}; }