Skip to content

Commit 7ecd807

Browse files
authored
perf(es/minifier): Move logic to the pure minifier (#10264)
**Description:** ``` Benchmarking es/minifier/real/es/minifier/real/sequential: Warming up for 3.0000 s Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 99.0s. es/minifier/real/es/minifier/real/sequential time: [10.086 s 10.173 s 10.274 s] change: [-7.5770% -6.7384% -5.7020%] (p = 0.00 < 0.05) Performance has improved. ```
1 parent 1d91571 commit 7ecd807

File tree

41 files changed

+824
-801
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+824
-801
lines changed
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
| File | Original Size | Compressed Size | Gzipped Size |
22
| --- | --- | --- | --- |
3-
| antd.js | 6.38 MiB | 2.05 MiB | 444.48 KiB |
3+
| antd.js | 6.38 MiB | 2.05 MiB | 444.47 KiB |
44
| d3.js | 542.74 KiB | 259.00 KiB | 85.06 KiB |
5-
| echarts.js | 3.41 MiB | 971.04 KiB | 313.32 KiB |
5+
| echarts.js | 3.41 MiB | 971.11 KiB | 313.35 KiB |
66
| jquery.js | 280.89 KiB | 87.11 KiB | 30.15 KiB |
77
| lodash.js | 531.35 KiB | 68.18 KiB | 24.52 KiB |
8-
| moment.js | 169.83 KiB | 57.03 KiB | 18.21 KiB |
8+
| moment.js | 169.83 KiB | 57.04 KiB | 18.22 KiB |
99
| react.js | 70.45 KiB | 22.28 KiB | 8.00 KiB |
10-
| terser.js | 1.08 MiB | 444.88 KiB | 120.45 KiB |
10+
| terser.js | 1.08 MiB | 444.91 KiB | 120.44 KiB |
1111
| three.js | 1.19 MiB | 627.90 KiB | 154.58 KiB |
12-
| typescript.js | 10.45 MiB | 3.17 MiB | 846.03 KiB |
12+
| typescript.js | 10.45 MiB | 3.17 MiB | 846.04 KiB |
1313
| victory.js | 2.30 MiB | 689.65 KiB | 153.64 KiB |
14-
| vue.js | 334.13 KiB | 112.95 KiB | 41.69 KiB |
14+
| vue.js | 334.13 KiB | 112.96 KiB | 41.69 KiB |

crates/swc/tests/tsc-references/parserModule1.2.minified.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function Alert(output) {
66
CompilerDiagnostics.debug = !1, CompilerDiagnostics.diagnosticWriter = null, CompilerDiagnostics.analysisPass = 0, CompilerDiagnostics.Alert = Alert, CompilerDiagnostics.debugPrint = function(s) {
77
CompilerDiagnostics.debug && Alert(s);
88
}, CompilerDiagnostics.assert = function(condition, s) {
9-
CompilerDiagnostics.debug && (condition || Alert(s));
9+
CompilerDiagnostics.debug && !condition && Alert(s);
1010
};
1111
var CompilerDiagnostics1;
1212
export { CompilerDiagnostics1 as CompilerDiagnostics };

crates/swc/tests/tsc-references/parserRealSource1.2.minified.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function Alert(output) {
77
CompilerDiagnostics.debug = !1, CompilerDiagnostics.diagnosticWriter = null, CompilerDiagnostics.analysisPass = 0, CompilerDiagnostics.Alert = Alert, CompilerDiagnostics.debugPrint = function(s) {
88
CompilerDiagnostics.debug && Alert(s);
99
}, CompilerDiagnostics.assert = function(condition, s) {
10-
CompilerDiagnostics.debug && (condition || Alert(s));
10+
CompilerDiagnostics.debug && !condition && Alert(s);
1111
}, TypeScript1.NullLogger = /*#__PURE__*/ function() {
1212
function NullLogger() {
1313
_class_call_check(this, NullLogger);

crates/swc_ecma_minifier/src/compress/optimize/bools.rs

-29
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,6 @@ use super::Optimizer;
66

77
/// Methods related to the options `bools` and `bool_as_ints`.
88
impl Optimizer<'_> {
9-
pub(super) fn compress_if_stmt_as_expr(&mut self, s: &mut Stmt) {
10-
if !self.options.conditionals && !self.options.bools {
11-
return;
12-
}
13-
14-
let stmt = match s {
15-
Stmt::If(v) => v,
16-
_ => return,
17-
};
18-
19-
if stmt.alt.is_none() {
20-
if let Stmt::Expr(cons) = &mut *stmt.cons {
21-
self.changed = true;
22-
report_change!("conditionals: `if (foo) bar;` => `foo && bar`");
23-
*s = ExprStmt {
24-
span: stmt.span,
25-
expr: BinExpr {
26-
span: stmt.test.span(),
27-
op: op!("&&"),
28-
left: stmt.test.take(),
29-
right: cons.expr.take(),
30-
}
31-
.into(),
32-
}
33-
.into();
34-
}
35-
}
36-
}
37-
389
///
3910
/// - `"undefined" == typeof value;` => `void 0 === value`
4011
pub(super) fn compress_typeof_undefined(&mut self, e: &mut BinExpr) {

crates/swc_ecma_minifier/src/compress/optimize/collapse_vars.rs

-1
This file was deleted.

crates/swc_ecma_minifier/src/compress/optimize/if_return.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
1+
use swc_common::{util::take::Take, DUMMY_SP};
22
use swc_ecma_ast::*;
33
use swc_ecma_transforms_optimization::debug_assert_valid;
44
use swc_ecma_utils::StmtLike;
@@ -12,36 +12,6 @@ use crate::{compress::util::is_pure_undefined, util::ExprOptExt};
1212
/// Methods related to the option `if_return`. All methods are noop if
1313
/// `if_return` is false.
1414
impl Optimizer<'_> {
15-
pub(super) fn merge_nested_if(&mut self, s: &mut IfStmt) {
16-
if !self.options.conditionals && !self.options.bools {
17-
return;
18-
}
19-
20-
if s.alt.is_some() {
21-
return;
22-
}
23-
24-
if let Stmt::If(IfStmt {
25-
test,
26-
cons,
27-
alt: None,
28-
..
29-
}) = &mut *s.cons
30-
{
31-
self.changed = true;
32-
report_change!("if_return: Merging nested if statements");
33-
34-
s.test = BinExpr {
35-
span: s.test.span(),
36-
op: op!("&&"),
37-
left: s.test.take(),
38-
right: test.take(),
39-
}
40-
.into();
41-
s.cons = cons.take();
42-
}
43-
}
44-
4515
pub(super) fn merge_if_returns(
4616
&mut self,
4717
stmts: &mut Vec<Stmt>,

crates/swc_ecma_minifier/src/compress/optimize/loops.rs

-26
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,4 @@ impl Optimizer<'_> {
119119
_ => {}
120120
}
121121
}
122-
123-
///
124-
/// - `for (a(), 5; b(); c())` => `for (a(); b(); c())`
125-
pub(super) fn optimize_init_of_for_stmt(&mut self, s: &mut ForStmt) {
126-
if !self.options.side_effects {
127-
return;
128-
}
129-
130-
if let Some(init) = &mut s.init {
131-
match init {
132-
VarDeclOrExpr::VarDecl(_) => {}
133-
VarDeclOrExpr::Expr(init) => {
134-
let new = self.ignore_return_value(init);
135-
if let Some(new) = new {
136-
*init = Box::new(new);
137-
} else {
138-
s.init = None;
139-
self.changed = true;
140-
report_change!(
141-
"loops: Removed side-effect-free expressions in `init` of a for stmt"
142-
);
143-
}
144-
}
145-
}
146-
}
147-
}
148122
}

crates/swc_ecma_minifier/src/compress/optimize/mod.rs

-135
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use crate::{
3737

3838
mod arguments;
3939
mod bools;
40-
mod collapse_vars;
4140
mod conditionals;
4241
mod dead_code;
4342
mod evaluate;
@@ -526,13 +525,6 @@ impl Optimizer<'_> {
526525
stmts.visit_with(&mut AssertValid);
527526
}
528527

529-
self.break_assignments_in_seqs(stmts);
530-
531-
#[cfg(debug_assertions)]
532-
{
533-
stmts.visit_with(&mut AssertValid);
534-
}
535-
536528
// stmts.extend(self.append_stmts.drain(..).map(T::from));
537529

538530
drop_invalid_stmts(stmts);
@@ -542,99 +534,6 @@ impl Optimizer<'_> {
542534
self.append_stmts = append_stmts;
543535
}
544536

545-
/// `a = a + 1` => `a += 1`.
546-
fn compress_bin_assignment_to_left(&mut self, e: &mut AssignExpr) {
547-
if e.op != op!("=") {
548-
return;
549-
}
550-
551-
// TODO: Handle pure properties.
552-
let lhs = match &e.left {
553-
AssignTarget::Simple(SimpleAssignTarget::Ident(i)) => i,
554-
_ => return,
555-
};
556-
557-
// If left operand of a binary expression is not same as lhs, this method has
558-
// nothing to do.
559-
let (op, right) = match &mut *e.right {
560-
Expr::Bin(BinExpr {
561-
left, op, right, ..
562-
}) => match &**left {
563-
Expr::Ident(r) if lhs.sym == r.sym && lhs.ctxt == r.ctxt => (op, right),
564-
_ => return,
565-
},
566-
_ => return,
567-
};
568-
569-
// Don't break code for old browsers.
570-
match op {
571-
BinaryOp::LogicalOr => return,
572-
BinaryOp::LogicalAnd => return,
573-
BinaryOp::Exp => return,
574-
BinaryOp::NullishCoalescing => return,
575-
_ => {}
576-
}
577-
578-
let op = match op {
579-
BinaryOp::In | BinaryOp::InstanceOf => return,
580-
581-
BinaryOp::EqEq | BinaryOp::NotEq | BinaryOp::EqEqEq | BinaryOp::NotEqEq => {
582-
// TODO(kdy1): Check if this is optimizable.
583-
return;
584-
}
585-
586-
BinaryOp::Lt | BinaryOp::LtEq | BinaryOp::Gt | BinaryOp::GtEq => return,
587-
588-
BinaryOp::LShift => op!("<<="),
589-
BinaryOp::RShift => {
590-
op!(">>=")
591-
}
592-
BinaryOp::ZeroFillRShift => {
593-
op!(">>>=")
594-
}
595-
BinaryOp::Add => {
596-
op!("+=")
597-
}
598-
BinaryOp::Sub => {
599-
op!("-=")
600-
}
601-
BinaryOp::Mul => {
602-
op!("*=")
603-
}
604-
BinaryOp::Div => {
605-
op!("/=")
606-
}
607-
BinaryOp::Mod => {
608-
op!("%=")
609-
}
610-
BinaryOp::BitOr => {
611-
op!("|=")
612-
}
613-
BinaryOp::BitXor => {
614-
op!("^=")
615-
}
616-
BinaryOp::BitAnd => {
617-
op!("&=")
618-
}
619-
BinaryOp::LogicalOr => {
620-
op!("||=")
621-
}
622-
BinaryOp::LogicalAnd => {
623-
op!("&&=")
624-
}
625-
BinaryOp::Exp => {
626-
op!("**=")
627-
}
628-
BinaryOp::NullishCoalescing => {
629-
op!("??=")
630-
}
631-
};
632-
633-
e.op = op;
634-
e.right = right.take();
635-
// Now we can compress it to an assignment
636-
}
637-
638537
///
639538
/// - `undefined` => `void 0`
640539
fn compress_undefined(&mut self, e: &mut Expr) {
@@ -1578,15 +1477,8 @@ impl VisitMut for Optimizer<'_> {
15781477
..self.ctx.clone()
15791478
};
15801479
e.left.visit_mut_with(&mut *self.with_ctx(ctx));
1581-
1582-
if is_left_access_to_arguments(&e.left) {
1583-
// self.ctx.can_inline_arguments = false;
1584-
}
15851480
}
15861481
e.right.visit_mut_with(self);
1587-
1588-
self.compress_bin_assignment_to_left(e);
1589-
self.compress_bin_assignment_to_right(e);
15901482
}
15911483

15921484
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
@@ -1937,19 +1829,6 @@ impl VisitMut for Optimizer<'_> {
19371829
debug_assert_valid(e);
19381830
}
19391831

1940-
if let Expr::Bin(bin) = e {
1941-
let expr = self.optimize_lit_cmp(bin);
1942-
if let Some(expr) = expr {
1943-
report_change!("Optimizing: Literal comparison");
1944-
self.changed = true;
1945-
*e = expr;
1946-
}
1947-
}
1948-
1949-
if e.is_seq() {
1950-
debug_assert_valid(e);
1951-
}
1952-
19531832
self.compress_cond_expr_if_similar(e);
19541833

19551834
if e.is_seq() {
@@ -2194,8 +2073,6 @@ impl VisitMut for Optimizer<'_> {
21942073
};
21952074

21962075
s.body.visit_mut_with(&mut *self.with_ctx(ctx.clone()));
2197-
2198-
self.with_ctx(ctx.clone()).optimize_init_of_for_stmt(s);
21992076
}
22002077

22012078
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
@@ -2275,8 +2152,6 @@ impl VisitMut for Optimizer<'_> {
22752152

22762153
n.alt.visit_mut_with(&mut *self.with_ctx(ctx.clone()));
22772154

2278-
self.merge_nested_if(n);
2279-
22802155
self.negate_if_stmt(n);
22812156
}
22822157

@@ -2472,10 +2347,6 @@ impl VisitMut for Optimizer<'_> {
24722347
fn visit_mut_seq_expr(&mut self, n: &mut SeqExpr) {
24732348
n.visit_mut_children_with(self);
24742349

2475-
self.shift_void(n);
2476-
2477-
self.shift_assignment(n);
2478-
24792350
self.merge_sequences_in_seq_expr(n);
24802351
}
24812352

@@ -2707,12 +2578,6 @@ impl VisitMut for Optimizer<'_> {
27072578
debug_assert_eq!(self.prepend_stmts.len(), prepend_len);
27082579
debug_assert_eq!(self.append_stmts.len(), append_len);
27092580
debug_assert_valid(s);
2710-
2711-
self.compress_if_stmt_as_expr(s);
2712-
2713-
debug_assert_eq!(self.prepend_stmts.len(), prepend_len);
2714-
debug_assert_eq!(self.append_stmts.len(), append_len);
2715-
debug_assert_valid(s);
27162581
}
27172582

27182583
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]

0 commit comments

Comments
 (0)