Skip to content

Commit

Permalink
refactor(es/minifier): Use Program instead of Module (#4969)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jun 14, 2022
1 parent 164bf9a commit 99a474c
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 121 deletions.
10 changes: 6 additions & 4 deletions crates/dbg-swc/src/minify/diff_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl DiffOptionCommand {
let orig = {
let m = m.clone();
let mut m = swc_ecma_minifier::optimize(
m.module,
m.module.into(),
cm.clone(),
Some(&m.comments),
None,
Expand All @@ -77,14 +77,15 @@ impl DiffOptionCommand {
unresolved_mark: m.unresolved_mark,
top_level_mark: m.top_level_mark,
},
);
)
.expect_module();
m.visit_mut_with(&mut fixer(None));
print_js(cm.clone(), &m, false)?
};

let new = {
let mut m = swc_ecma_minifier::optimize(
m.module,
m.module.into(),
cm.clone(),
Some(&m.comments),
None,
Expand All @@ -100,7 +101,8 @@ impl DiffOptionCommand {
unresolved_mark: m.unresolved_mark,
top_level_mark: m.top_level_mark,
},
);
)
.expect_module();
m.visit_mut_with(&mut fixer(None));
print_js(cm, &m, false)?
};
Expand Down
3 changes: 2 additions & 1 deletion crates/dbg-swc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl TestMinifiedBundleCommand {
let mut minified = {
let _timer = timer!("minify");
swc_ecma_minifier::optimize(
bundle.module,
bundle.module.into(),
cm.clone(),
None,
None,
Expand All @@ -74,6 +74,7 @@ impl TestMinifiedBundleCommand {
top_level_mark: bundle.top_level_mark,
},
)
.expect_module()
};

minified.visit_mut_with(&mut fixer(None));
Expand Down
3 changes: 2 additions & 1 deletion crates/dbg-swc/src/util/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn get_minified(

let mut module = {
swc_ecma_minifier::optimize(
m.module,
m.module.into(),
cm,
Some(&m.comments),
None,
Expand All @@ -47,6 +47,7 @@ pub fn get_minified(
top_level_mark: m.top_level_mark,
},
)
.expect_module()
};

module.visit_mut_with(&mut Normalizer {});
Expand Down
3 changes: 2 additions & 1 deletion crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl VisitMut for MinifierPass {

m.map_with_mut(|m| {
swc_ecma_minifier::optimize(
m,
m.into(),
self.cm.clone(),
self.comments.as_ref().map(|v| v as &dyn Comments),
None,
Expand All @@ -394,6 +394,7 @@ impl VisitMut for MinifierPass {
top_level_mark: self.top_level_mark,
},
)
.expect_module()
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,7 @@ impl Compiler {
IsModule::Bool(true),
Some(&comments),
)
.context("failed to parse input file")?
.expect_module();
.context("failed to parse input file")?;

let source_map_names = if source_map.enabled() {
let mut v = IdentCollector {
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_bundler/examples/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn do_test(_entry: &Path, entries: HashMap<String, FileName>, inline: bool, mini
.map(|mut b| {
GLOBALS.set(globals, || {
b.module = swc_ecma_minifier::optimize(
b.module,
b.module.into(),
cm.clone(),
None,
None,
Expand All @@ -137,7 +137,8 @@ fn do_test(_entry: &Path, entries: HashMap<String, FileName>, inline: bool, mini
unresolved_mark: Mark::new(),
top_level_mark: Mark::new(),
},
);
)
.expect_module();
b.module.visit_mut_with(&mut fixer(None));
b
})
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_bundler/tests/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ fn bundle(url: &str, minify: bool) -> String {
module.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));

module = swc_ecma_minifier::optimize(
module,
module.into(),
cm.clone(),
None,
None,
Expand All @@ -1069,7 +1069,8 @@ fn bundle(url: &str, minify: bool) -> String {
unresolved_mark,
top_level_mark,
},
);
)
.expect_module();
module.visit_mut_with(&mut fixer(None));
}

Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_minifier/benches/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn run(src: &str) {
.unwrap();

let output = optimize(
program,
program.into(),
cm.clone(),
None,
None,
Expand All @@ -94,7 +94,8 @@ fn run(src: &str) {
unresolved_mark,
top_level_mark,
},
);
)
.expect_module();

let output = output.fold_with(&mut fixer(None));

Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_minifier/examples/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {
.unwrap();

let output = optimize(
program,
program.into(),
cm.clone(),
None,
None,
Expand All @@ -56,7 +56,8 @@ fn main() {
unresolved_mark,
top_level_mark,
},
);
)
.expect_module();

let output = output.fold_with(&mut fixer(None));

Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() {
.unwrap();

let output = optimize(
program,
program.into(),
cm.clone(),
None,
None,
Expand All @@ -55,7 +55,8 @@ fn main() {
unresolved_mark,
top_level_mark,
},
);
)
.expect_module();

let output = output.fold_with(&mut fixer(None));

Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_minifier/examples/minify-all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() {
.unwrap();

let output = optimize(
program,
program.into(),
cm.clone(),
None,
None,
Expand All @@ -66,7 +66,8 @@ fn main() {
unresolved_mark,
top_level_mark,
},
);
)
.expect_module();

let output = output.fold_with(&mut fixer(None));

Expand Down
12 changes: 7 additions & 5 deletions crates/swc_ecma_minifier/src/compress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use pretty_assertions::assert_eq;
use rayon::prelude::*;
use swc_common::{
chain,
pass::{CompilerPass, Repeated},
pass::{CompilerPass, Optional, Repeated},
Globals,
};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::pass::JsPass;
use swc_ecma_transforms_optimization::simplify::{
dead_branch_remover, expr_simplifier, ExprSimplifierConfig,
};
Expand All @@ -35,7 +34,7 @@ use crate::{
maybe_par,
mode::Mode,
option::CompressOptions,
util::{now, unit::CompileUnit, Optional},
util::{now, unit::CompileUnit},
DISABLE_BUGGY_PASSES, MAX_PAR_DEPTH,
};

Expand All @@ -49,7 +48,7 @@ pub(crate) fn compressor<'a, M>(
marks: Marks,
options: &'a CompressOptions,
mode: &'a M,
) -> impl 'a + JsPass
) -> impl 'a + VisitMut
where
M: Mode,
{
Expand All @@ -68,7 +67,10 @@ where
as_folder(compressor),
Optional {
enabled: options.evaluate || options.side_effects,
visitor: expr_simplifier(marks.unresolved_mark, ExprSimplifierConfig {})
visitor: as_folder(expr_simplifier(
marks.unresolved_mark,
ExprSimplifierConfig {}
))
}
)
}
Expand Down
23 changes: 10 additions & 13 deletions crates/swc_ecma_minifier/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::sync::Arc;

use parking_lot::Mutex;
use swc_atoms::js_word;
use swc_common::{collections::AHashMap, util::take::Take, SyntaxContext, DUMMY_SP};
use swc_common::{collections::AHashMap, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::simplify::{expr_simplifier, ExprSimplifierConfig};
use swc_ecma_utils::{undefined, ExprCtx, ExprExt};
use swc_ecma_visit::{FoldWith, VisitMutWith};
use swc_ecma_visit::VisitMutWith;

use crate::{
compress::{compressor, pure_optimizer, PureOptimizerConfig},
Expand Down Expand Up @@ -74,17 +74,14 @@ impl Evaluator {

let marks = self.marks;
let data = self.data.clone();
self.module.map_with_mut(|m| {
//
swc_common::GLOBALS.with(|globals| {
//
m.fold_with(&mut compressor(
globals,
marks,
&serde_json::from_str("{ \"hoist_props\": true }").unwrap(),
&data,
))
})
//
swc_common::GLOBALS.with(|globals| {
self.module.visit_mut_with(&mut compressor(
globals,
marks,
&serde_json::from_str("{ \"hoist_props\": true }").unwrap(),
&data,
));
});
}
}
Expand Down
16 changes: 6 additions & 10 deletions crates/swc_ecma_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
//! If you enable this cargo feature and set the environment variable named
//! `SWC_RUN` to `1`, the minifier will validate the code using node before each
//! step.
//!
//! Note: Passes should be visited only with [Module] and it's an error to feed
//! them something other. Don't call methods like `visit_mut_script` nor
//! `visit_mut_module_items`.
#![deny(clippy::all)]
#![deny(unused)]
#![allow(clippy::blocks_in_if_conditions)]
Expand All @@ -30,8 +26,8 @@ use compress::{pure_optimizer, PureOptimizerConfig};
use mode::Mode;
use once_cell::sync::Lazy;
use swc_common::{comments::Comments, pass::Repeat, sync::Lrc, SourceMap, GLOBALS};
use swc_ecma_ast::Module;
use swc_ecma_visit::{FoldWith, VisitMutWith};
use swc_ecma_ast::Program;
use swc_ecma_visit::VisitMutWith;
use swc_timer::timer;
use timing::Timings;

Expand Down Expand Up @@ -73,13 +69,13 @@ pub(crate) static HEAVY_TASK_PARALLELS: Lazy<usize> = Lazy::new(|| *CPU_COUNT *
pub(crate) static LIGHT_TASK_PARALLELS: Lazy<usize> = Lazy::new(|| *CPU_COUNT * 100);

pub fn optimize(
mut m: Module,
mut m: Program,
_cm: Lrc<SourceMap>,
comments: Option<&dyn Comments>,
mut timings: Option<&mut Timings>,
options: &MinifyOptions,
extra: &ExtraOptions,
) -> Module {
) -> Program {
let _timer = timer!("minify");

let mut marks = Marks::new();
Expand Down Expand Up @@ -150,8 +146,8 @@ pub fn optimize(
{
let _timer = timer!("compress ast");

m = GLOBALS.with(|globals| {
m.fold_with(&mut compressor(globals, marks, options, &Minification))
GLOBALS.with(|globals| {
m.visit_mut_with(&mut compressor(globals, marks, options, &Minification))
});
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/src/pass/mangle_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
use swc_atoms::{js_word, JsWord};
use swc_common::collections::{AHashMap, AHashSet};
use swc_ecma_ast::{
CallExpr, Callee, Expr, Ident, KeyValueProp, Lit, MemberExpr, MemberProp, Module, Prop,
CallExpr, Callee, Expr, Ident, KeyValueProp, Lit, MemberExpr, MemberProp, Program, Prop,
PropName, Str, SuperProp, SuperPropExpr,
};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl ManglePropertiesState {
}
}

pub(crate) fn mangle_properties(m: &mut Module, options: ManglePropertiesOptions) {
pub(crate) fn mangle_properties(m: &mut Program, options: ManglePropertiesOptions) {
let mut state = ManglePropertiesState {
options,
..Default::default()
Expand Down

1 comment on commit 99a474c

@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: 99a474c Previous: db9f7a5 Ratio
es/full/minify/libraries/antd 1691462500 ns/iter (± 129401143) 1663227641 ns/iter (± 52117482) 1.02
es/full/minify/libraries/d3 419320490 ns/iter (± 31229306) 424314646 ns/iter (± 8855939) 0.99
es/full/minify/libraries/echarts 1686219257 ns/iter (± 129701982) 1790545232 ns/iter (± 84705966) 0.94
es/full/minify/libraries/jquery 93456912 ns/iter (± 4678203) 94361406 ns/iter (± 2774214) 0.99
es/full/minify/libraries/lodash 123690029 ns/iter (± 4377623) 126398544 ns/iter (± 10301673) 0.98
es/full/minify/libraries/moment 50365134 ns/iter (± 1898019) 50657027 ns/iter (± 1165349) 0.99
es/full/minify/libraries/react 17459030 ns/iter (± 279553) 17613172 ns/iter (± 425512) 0.99
es/full/minify/libraries/terser 595152737 ns/iter (± 13464018) 586141434 ns/iter (± 14980546) 1.02
es/full/minify/libraries/three 534483408 ns/iter (± 9158565) 534960323 ns/iter (± 6192283) 1.00
es/full/minify/libraries/typescript 3445208202 ns/iter (± 119484355) 3561040365 ns/iter (± 111000986) 0.97
es/full/minify/libraries/victory 720986137 ns/iter (± 27180985) 839478439 ns/iter (± 47805612) 0.86
es/full/minify/libraries/vue 133950651 ns/iter (± 9056002) 146634901 ns/iter (± 11632843) 0.91
es/full/codegen/es3 29770 ns/iter (± 764) 33210 ns/iter (± 5451) 0.90
es/full/codegen/es5 29516 ns/iter (± 799) 32139 ns/iter (± 5008) 0.92
es/full/codegen/es2015 29422 ns/iter (± 842) 32141 ns/iter (± 2212) 0.92
es/full/codegen/es2016 29444 ns/iter (± 924) 31688 ns/iter (± 3351) 0.93
es/full/codegen/es2017 29719 ns/iter (± 1201) 32030 ns/iter (± 3799) 0.93
es/full/codegen/es2018 29568 ns/iter (± 4976) 31714 ns/iter (± 2505) 0.93
es/full/codegen/es2019 29368 ns/iter (± 633) 32169 ns/iter (± 3088) 0.91
es/full/codegen/es2020 29357 ns/iter (± 562) 32065 ns/iter (± 2310) 0.92
es/full/all/es3 185820422 ns/iter (± 9177624) 197998003 ns/iter (± 7021100) 0.94
es/full/all/es5 174330099 ns/iter (± 7791519) 186891110 ns/iter (± 11040353) 0.93
es/full/all/es2015 141269558 ns/iter (± 4746128) 149739736 ns/iter (± 7772135) 0.94
es/full/all/es2016 140943666 ns/iter (± 3483215) 150871858 ns/iter (± 6697896) 0.93
es/full/all/es2017 140891688 ns/iter (± 15358776) 147133427 ns/iter (± 5766240) 0.96
es/full/all/es2018 138631226 ns/iter (± 6570673) 156607692 ns/iter (± 10506533) 0.89
es/full/all/es2019 138247463 ns/iter (± 6890765) 150596085 ns/iter (± 78434503) 0.92
es/full/all/es2020 133640818 ns/iter (± 7387978) 147413363 ns/iter (± 9353374) 0.91
es/full/parser 678545 ns/iter (± 56706) 734037 ns/iter (± 38718) 0.92
es/full/base/fixer 28716 ns/iter (± 1304) 30611 ns/iter (± 2634) 0.94
es/full/base/resolver_and_hygiene 131749 ns/iter (± 5819) 140778 ns/iter (± 12228) 0.94
serialization of ast node 202 ns/iter (± 5) 210 ns/iter (± 11) 0.96
serialization of serde 222 ns/iter (± 6) 220 ns/iter (± 6) 1.01

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

Please sign in to comment.