From 97dfcb9ccb39620576d6e63c195ec588ff4c5790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 25 Mar 2022 15:44:07 +0900 Subject: [PATCH] chore(es/minifier): Add logging (#4156) --- Cargo.lock | 2 +- crates/swc_ecma_minifier/Cargo.toml | 6 +- crates/swc_ecma_minifier/benches/full.rs | 2 +- crates/swc_ecma_minifier/examples/minifier.rs | 2 +- crates/swc_ecma_minifier/one.sh | 4 +- .../swc_ecma_minifier/src/analyzer/tests.rs | 2 +- crates/swc_ecma_minifier/src/compress/mod.rs | 6 +- .../src/compress/util/mod.rs | 2 +- .../src/compress/util/tests.rs | 2 +- crates/swc_ecma_minifier/src/debug.rs | 2 +- crates/swc_ecma_minifier/src/eval.rs | 2 +- .../src/pass/mangle_names/real_impl.rs | 2 +- crates/swc_ecma_minifier/src/util/unit.rs | 2 +- crates/swc_ecma_minifier/tests/compress.rs | 2 +- crates/swc_ecma_minifier/tests/eval.rs | 2 +- crates/swc_ecma_minifier/tests/exec.rs | 2 +- crates/swc_ecma_minifier/tests/mangle.rs | 2 +- .../Cargo.toml | 1 + .../src/simplify/branch/mod.rs | 87 ++++++++++++++++++- 19 files changed, 107 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f0d6d45fd8e..2198c682c4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3379,8 +3379,8 @@ dependencies = [ "swc_ecma_ast", "swc_ecma_codegen", "swc_ecma_parser", - "swc_ecma_transforms", "swc_ecma_transforms_base", + "swc_ecma_transforms_optimization", "swc_ecma_utils", "swc_ecma_visit", "swc_node_base", diff --git a/crates/swc_ecma_minifier/Cargo.toml b/crates/swc_ecma_minifier/Cargo.toml index 37fceb1020b..a7b2a707078 100644 --- a/crates/swc_ecma_minifier/Cargo.toml +++ b/crates/swc_ecma_minifier/Cargo.toml @@ -14,7 +14,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [features] -debug = ["backtrace"] +debug = ["backtrace", "swc_ecma_transforms_optimization/debug"] [dependencies] ahash = "0.7.6" @@ -35,10 +35,8 @@ swc_common = { version = "0.17.0", path = "../swc_common" } swc_ecma_ast = { version = "0.73.0", path = "../swc_ecma_ast" } swc_ecma_codegen = { version = "0.100.0", path = "../swc_ecma_codegen" } swc_ecma_parser = { version = "0.97.0", path = "../swc_ecma_parser" } -swc_ecma_transforms = { version = "0.138.0", path = "../swc_ecma_transforms/", features = [ - "optimization", -] } swc_ecma_transforms_base = { version = "0.72.0", path = "../swc_ecma_transforms_base" } +swc_ecma_transforms_optimization = { version = "0.108.0", path = "../swc_ecma_transforms_optimization" } swc_ecma_utils = { version = "0.76.0", path = "../swc_ecma_utils" } swc_ecma_visit = { version = "0.59.0", path = "../swc_ecma_visit" } swc_timer = { version = "0.5.0", path = "../swc_timer" } diff --git a/crates/swc_ecma_minifier/benches/full.rs b/crates/swc_ecma_minifier/benches/full.rs index 87bb908c062..0d89d7041bf 100644 --- a/crates/swc_ecma_minifier/benches/full.rs +++ b/crates/swc_ecma_minifier/benches/full.rs @@ -10,7 +10,7 @@ use swc_ecma_minifier::{ option::{ExtraOptions, MangleOptions, MinifyOptions}, }; use swc_ecma_parser::parse_file_as_module; -use swc_ecma_transforms::{fixer, resolver_with_mark}; +use swc_ecma_transforms_base::{fixer::fixer, resolver::resolver_with_mark}; use swc_ecma_visit::FoldWith; pub fn bench_files(c: &mut Criterion) { diff --git a/crates/swc_ecma_minifier/examples/minifier.rs b/crates/swc_ecma_minifier/examples/minifier.rs index 1b946ebb14c..3227a517497 100644 --- a/crates/swc_ecma_minifier/examples/minifier.rs +++ b/crates/swc_ecma_minifier/examples/minifier.rs @@ -11,7 +11,7 @@ use swc_ecma_minifier::{ option::{ExtraOptions, MangleOptions, MinifyOptions}, }; use swc_ecma_parser::parse_file_as_module; -use swc_ecma_transforms::{fixer, resolver_with_mark}; +use swc_ecma_transforms_base::{fixer::fixer, resolver::resolver_with_mark}; use swc_ecma_visit::FoldWith; fn main() { diff --git a/crates/swc_ecma_minifier/one.sh b/crates/swc_ecma_minifier/one.sh index 517a1f510ed..99c8e681a17 100755 --- a/crates/swc_ecma_minifier/one.sh +++ b/crates/swc_ecma_minifier/one.sh @@ -7,5 +7,5 @@ set -eux -export RUST_LOG=swc_ecma_minifier=trace -cargo test $@ --features debug --test compress \ No newline at end of file +export RUST_LOG=swc_ecma_minifier=trace,swc_ecma_transforms_optimization=trace +cargo test $@ --features debug --features swc_ecma_transforms_optimization/debug --test compress \ No newline at end of file diff --git a/crates/swc_ecma_minifier/src/analyzer/tests.rs b/crates/swc_ecma_minifier/src/analyzer/tests.rs index 91952aeb2b0..faa3275dd3f 100644 --- a/crates/swc_ecma_minifier/src/analyzer/tests.rs +++ b/crates/swc_ecma_minifier/src/analyzer/tests.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use swc_common::{comments::SingleThreadedComments, Mark}; use swc_ecma_ast::Id; use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; -use swc_ecma_transforms::resolver_with_mark; +use swc_ecma_transforms_base::resolver::resolver_with_mark; use swc_ecma_visit::FoldWith; use testing::NormalizedOutput; diff --git a/crates/swc_ecma_minifier/src/compress/mod.rs b/crates/swc_ecma_minifier/src/compress/mod.rs index 321b68bf30e..f8b5aa467a4 100644 --- a/crates/swc_ecma_minifier/src/compress/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/mod.rs @@ -15,9 +15,9 @@ use swc_common::{ Globals, }; use swc_ecma_ast::*; -use swc_ecma_transforms::{ - optimization::simplify::{dead_branch_remover, expr_simplifier, ExprSimplifierConfig}, - pass::JsPass, +use swc_ecma_transforms_base::pass::JsPass; +use swc_ecma_transforms_optimization::simplify::{ + dead_branch_remover, expr_simplifier, ExprSimplifierConfig, }; use swc_ecma_utils::StmtLike; use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; diff --git a/crates/swc_ecma_minifier/src/compress/util/mod.rs b/crates/swc_ecma_minifier/src/compress/util/mod.rs index d40492e57c5..fafbfd1f0b8 100644 --- a/crates/swc_ecma_minifier/src/compress/util/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/util/mod.rs @@ -3,7 +3,7 @@ use std::f64; use swc_atoms::js_word; use swc_common::{util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_transforms::fixer; +use swc_ecma_transforms_base::fixer::fixer; use swc_ecma_utils::{ExprExt, Id, UsageFinder, Value}; use swc_ecma_visit::{ as_folder, noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith, diff --git a/crates/swc_ecma_minifier/src/compress/util/tests.rs b/crates/swc_ecma_minifier/src/compress/util/tests.rs index 554282993cf..11732898ae4 100644 --- a/crates/swc_ecma_minifier/src/compress/util/tests.rs +++ b/crates/swc_ecma_minifier/src/compress/util/tests.rs @@ -1,7 +1,7 @@ use swc_common::{util::take::Take, FileName}; use swc_ecma_ast::*; use swc_ecma_parser::parse_file_as_expr; -use swc_ecma_transforms::fixer; +use swc_ecma_transforms_base::fixer::fixer; use swc_ecma_visit::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith}; use tracing::{info, warn}; diff --git a/crates/swc_ecma_minifier/src/debug.rs b/crates/swc_ecma_minifier/src/debug.rs index f41e98e672b..7476b1f7c05 100644 --- a/crates/swc_ecma_minifier/src/debug.rs +++ b/crates/swc_ecma_minifier/src/debug.rs @@ -6,7 +6,7 @@ use once_cell::sync::Lazy; use swc_common::{sync::Lrc, SourceMap, SyntaxContext}; use swc_ecma_ast::*; use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; -use swc_ecma_transforms::{fixer, hygiene}; +use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene}; use swc_ecma_utils::{drop_span, DropSpan}; use swc_ecma_visit::{ noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith, diff --git a/crates/swc_ecma_minifier/src/eval.rs b/crates/swc_ecma_minifier/src/eval.rs index ac2c6436a6a..55f82afec91 100644 --- a/crates/swc_ecma_minifier/src/eval.rs +++ b/crates/swc_ecma_minifier/src/eval.rs @@ -4,7 +4,7 @@ use parking_lot::Mutex; use swc_atoms::js_word; use swc_common::{collections::AHashMap, util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_transforms::optimization::simplify::{expr_simplifier, ExprSimplifierConfig}; +use swc_ecma_transforms_optimization::simplify::{expr_simplifier, ExprSimplifierConfig}; use swc_ecma_utils::{ident::IdentLike, undefined, ExprExt, Id}; use swc_ecma_visit::{FoldWith, VisitMutWith}; diff --git a/crates/swc_ecma_minifier/src/pass/mangle_names/real_impl.rs b/crates/swc_ecma_minifier/src/pass/mangle_names/real_impl.rs index 1f96280e31d..0754c650c99 100644 --- a/crates/swc_ecma_minifier/src/pass/mangle_names/real_impl.rs +++ b/crates/swc_ecma_minifier/src/pass/mangle_names/real_impl.rs @@ -1,6 +1,6 @@ use swc_common::{SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_transforms::hygiene::rename; +use swc_ecma_transforms_base::hygiene::rename; use swc_ecma_utils::UsageFinder; use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith}; diff --git a/crates/swc_ecma_minifier/src/util/unit.rs b/crates/swc_ecma_minifier/src/util/unit.rs index 0d8071685e7..21e2465fb17 100644 --- a/crates/swc_ecma_minifier/src/util/unit.rs +++ b/crates/swc_ecma_minifier/src/util/unit.rs @@ -2,7 +2,7 @@ use std::fmt::Debug; use swc_common::Mark; use swc_ecma_ast::*; -use swc_ecma_transforms::{fixer, hygiene}; +use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene}; use swc_ecma_utils::DropSpan; use swc_ecma_visit::{as_folder, FoldWith, VisitMut, VisitMutWith, VisitWith}; diff --git a/crates/swc_ecma_minifier/tests/compress.rs b/crates/swc_ecma_minifier/tests/compress.rs index 0fc627f3b02..48fa24ef6d7 100644 --- a/crates/swc_ecma_minifier/tests/compress.rs +++ b/crates/swc_ecma_minifier/tests/compress.rs @@ -36,7 +36,7 @@ use swc_ecma_parser::{ lexer::{input::SourceFileInput, Lexer}, EsConfig, Parser, Syntax, }; -use swc_ecma_transforms::{fixer, hygiene::hygiene, resolver_with_mark}; +use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver::resolver_with_mark}; use swc_ecma_utils::drop_span; use swc_ecma_visit::{FoldWith, Visit, VisitMutWith, VisitWith}; use testing::{assert_eq, DebugUsingDisplay, NormalizedOutput}; diff --git a/crates/swc_ecma_minifier/tests/eval.rs b/crates/swc_ecma_minifier/tests/eval.rs index 5ca3c74ae73..6ec97ea6a7a 100644 --- a/crates/swc_ecma_minifier/tests/eval.rs +++ b/crates/swc_ecma_minifier/tests/eval.rs @@ -8,7 +8,7 @@ use swc_ecma_minifier::{ marks::Marks, }; use swc_ecma_parser::{parse_file_as_expr, parse_file_as_module, EsConfig, Syntax}; -use swc_ecma_transforms::resolver; +use swc_ecma_transforms_base::resolver::resolver; use swc_ecma_utils::ExprExt; use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; use testing::{assert_eq, DebugUsingDisplay}; diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index bd1464b5fec..a60389c4f59 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -23,7 +23,7 @@ use swc_ecma_minifier::{ }, }; use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; -use swc_ecma_transforms::{fixer, hygiene, resolver_with_mark}; +use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver::resolver_with_mark}; use swc_ecma_visit::{FoldWith, VisitMutWith}; use testing::DebugUsingDisplay; diff --git a/crates/swc_ecma_minifier/tests/mangle.rs b/crates/swc_ecma_minifier/tests/mangle.rs index 7bc2eca7d65..7cddf5e7bb7 100644 --- a/crates/swc_ecma_minifier/tests/mangle.rs +++ b/crates/swc_ecma_minifier/tests/mangle.rs @@ -13,7 +13,7 @@ use swc_ecma_minifier::{ option::{ExtraOptions, MangleOptions, ManglePropertiesOptions, MinifyOptions}, }; use swc_ecma_parser::parse_file_as_module; -use swc_ecma_transforms::resolver_with_mark; +use swc_ecma_transforms_base::resolver::resolver_with_mark; use swc_ecma_visit::VisitMutWith; use testing::NormalizedOutput; diff --git a/crates/swc_ecma_transforms_optimization/Cargo.toml b/crates/swc_ecma_transforms_optimization/Cargo.toml index f2311e62f41..51a55a344ad 100644 --- a/crates/swc_ecma_transforms_optimization/Cargo.toml +++ b/crates/swc_ecma_transforms_optimization/Cargo.toml @@ -11,6 +11,7 @@ version = "0.108.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] +debug = [] # Process in parallel. concurrent = [ "swc_common/concurrent", diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs index c6903a39c98..bb902d43a5b 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs @@ -15,6 +15,7 @@ use swc_ecma_utils::{ use swc_ecma_visit::{ as_folder, noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith, }; +use tracing::{debug, trace}; #[cfg(test)] mod tests; @@ -69,6 +70,10 @@ impl VisitMut for Remover { } if let Some(i) = preserved { + if cfg!(feature = "debug") { + debug!("Removing elements of an array pattern"); + } + p.elems.drain(i..); } } @@ -90,6 +95,9 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!("Dropping assignment to the same variable"); + } *e = Expr::Ident(r.take().ident().unwrap()); } @@ -103,6 +111,9 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!("Dropping assignment to an empty array pattern"); + } *e = *right.take(); } @@ -116,6 +127,9 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!("Dropping assignment to an empty object pattern"); + } *e = *right.take(); } @@ -134,6 +148,9 @@ impl VisitMut for Remover { .. }) if !arg.may_have_side_effects())) => { + if cfg!(feature = "debug") { + debug!("Dropping side-effect-free expressions"); + } *e = *cond.cons.take(); } @@ -169,6 +186,9 @@ impl VisitMut for Remover { } fn visit_mut_module_items(&mut self, n: &mut Vec) { + if cfg!(feature = "debug") { + debug!("Removing dead branches"); + } self.fold_stmt_like(n) } @@ -191,6 +211,11 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!( + "Dropping key-value pattern property because it's an empty object pattern" + ); + } false } @@ -200,6 +225,11 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!( + "Dropping key-value pattern property because it's an empty array pattern" + ); + } false } _ => true, @@ -333,6 +363,10 @@ impl VisitMut for Remover { let mut stmts = vec![]; if let (p, Known(v)) = test.as_bool() { + if cfg!(feature = "debug") { + trace!("The condition for if statement is always {}", v); + } + // Preserve effect of the test if !p.is_pure() { if let Some(expr) = ignore_result(*test).map(Box::new) { @@ -360,6 +394,10 @@ impl VisitMut for Remover { return Stmt::Empty(EmptyStmt { span }); } + if cfg!(feature = "debug") { + debug!("Optimized an if statement with known condition"); + } + self.changed = true; let mut block = Stmt::Block(BlockStmt { span, stmts }); @@ -395,10 +433,16 @@ impl VisitMut for Remover { } Stmt::Decl(Decl::Var(v)) if v.decls.is_empty() => { + if cfg!(feature = "debug") { + debug!("Dropping an empty var declaration"); + } Stmt::Empty(EmptyStmt { span: v.span }) } Stmt::Labeled(LabeledStmt { span, body, .. }) if body.is_empty() => { + if cfg!(feature = "debug") { + debug!("Dropping an empty label statement"); + } Stmt::Empty(EmptyStmt { span }) } @@ -412,6 +456,9 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!("Dropping a label statement with instant break"); + } Stmt::Empty(EmptyStmt { span }) } @@ -429,11 +476,19 @@ impl VisitMut for Remover { Stmt::Block(BlockStmt { span, stmts }) => { if stmts.is_empty() { + if cfg!(feature = "debug") { + debug!("Drooping an empty block statement"); + } + Stmt::Empty(EmptyStmt { span }) } else if stmts.len() == 1 && !is_block_scoped_stuff(&stmts[0]) && stmt_depth(&stmts[0]) <= 1 { + if cfg!(feature = "debug") { + debug!("Optimizing a block statement with a single statement"); + } + let mut v = stmts.into_iter().next().unwrap(); v.visit_mut_with(self); v @@ -466,6 +521,10 @@ impl VisitMut for Remover { // If catch block is not specified and finally block is empty, fold it to simple // block. if handler.is_none() && finalizer.is_empty() { + if cfg!(feature = "debug") { + debug!("Converting a try statement to a block statement"); + } + return Stmt::Block(block); } @@ -857,6 +916,10 @@ impl VisitMut for Remover { _ => false, } => { + if cfg!(feature = "debug") { + debug!("Optimizing a for statement with a false test"); + } + let decl = s.body.extract_var_ids_as_var(); let body = if let Some(var) = decl { Stmt::Decl(Decl::Var(var)) @@ -950,14 +1013,30 @@ impl VisitMut for Remover { // match &v.name { - Pat::Object(o) if o.props.is_empty() => None, - Pat::Array(a) if a.elems.is_empty() => None, + Pat::Object(o) if o.props.is_empty() => { + if cfg!(feature = "debug") { + debug!("Dropping an object pattern in a var declaration"); + } + + None + } + Pat::Array(a) if a.elems.is_empty() => { + if cfg!(feature = "debug") { + debug!("Dropping an array pattern in a var declaration"); + } + + None + } _ => Some(v), } }); if decls.is_empty() { + if cfg!(feature = "debug") { + debug!("Dropping a useless variable declaration"); + } + return Stmt::Empty(EmptyStmt { span: v.span }); } @@ -1063,6 +1142,10 @@ impl Remover { new_stmts.extend(hoisted_fns); *stmts = new_stmts; + + if cfg!(feature = "debug") { + debug!("Dropping statements after a control keyword"); + } return; }