mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 22:56:11 +03:00
perf(es/minifier): Make more passes parallel (#4821)
This commit is contained in:
parent
01e90a650c
commit
4b27df9f12
@ -1,6 +1,6 @@
|
|||||||
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
|
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
|
||||||
use swc_ecma_ast::*;
|
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 swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
|
||||||
|
|
||||||
use super::Optimizer;
|
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(
|
pub(super) fn merge_if_returns(
|
||||||
&mut self,
|
&mut self,
|
||||||
stmts: &mut Vec<Stmt>,
|
stmts: &mut Vec<Stmt>,
|
||||||
|
@ -2039,8 +2039,6 @@ where
|
|||||||
self.negate_if_stmt(n);
|
self.negate_if_stmt(n);
|
||||||
|
|
||||||
self.merge_nested_if(n);
|
self.merge_nested_if(n);
|
||||||
|
|
||||||
self.merge_else_if(n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
|
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use swc_common::{util::take::Take, DUMMY_SP};
|
use swc_common::{util::take::Take, DUMMY_SP};
|
||||||
use swc_ecma_ast::*;
|
use swc_ecma_ast::*;
|
||||||
|
use swc_ecma_utils::prepend_stmt;
|
||||||
|
|
||||||
use super::Pure;
|
use super::Pure;
|
||||||
use crate::compress::util::{is_fine_for_if_cons, negate};
|
use crate::compress::util::{is_fine_for_if_cons, negate};
|
||||||
@ -125,4 +126,56 @@ impl Pure<'_> {
|
|||||||
|
|
||||||
*stmts = new;
|
*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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -459,6 +459,8 @@ impl VisitMut for Pure<'_> {
|
|||||||
s.visit_mut_children_with(self);
|
s.visit_mut_children_with(self);
|
||||||
|
|
||||||
self.optimize_expr_in_bool_ctx(&mut s.test, false);
|
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) {
|
fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) {
|
||||||
|
@ -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_export_default_function/input.js
|
||||||
export/name_cache_mangle_local_import_and_export_aliases/input.js
|
export/name_cache_mangle_local_import_and_export_aliases/input.js
|
||||||
functions/duplicate_arg_var/input.js
|
functions/duplicate_arg_var/input.js
|
||||||
functions/empty_body/input.js
|
|
||||||
functions/hoist_funs/input.js
|
functions/hoist_funs/input.js
|
||||||
functions/hoist_funs_strict/input.js
|
functions/hoist_funs_strict/input.js
|
||||||
functions/inline_1/input.js
|
functions/inline_1/input.js
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
function f() {
|
function f() {
|
||||||
function noop() {}
|
return function noop() {};
|
||||||
return noop;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user