Skip to content

Commit

Permalink
fix(es/minifier): Fix minifier (#2477)
Browse files Browse the repository at this point in the history
swc_ecma_minifier:
 - `MultiReplacer`: Handle inlining into shorthands.
 - `ExprReplacer`: Handle inlining into shorthands.
  • Loading branch information
kdy1 authored Oct 19, 2021
1 parent 2ca6e5d commit 0020e16
Show file tree
Hide file tree
Showing 9 changed files with 1,305 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "src/lists/*.json"]
license = "Apache-2.0/MIT"
name = "swc_ecma_minifier"
repository = "https://github.com/swc-project/swc.git"
version = "0.44.0"
version = "0.44.1"

[features]
debug = ["backtrace"]
Expand Down
27 changes: 1 addition & 26 deletions ecmascript/minifier/src/compress/optimize/collapse_vars.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::Optimizer;
use crate::mode::Mode;
use swc_common::collections::AHashMap;
use swc_ecma_ast::*;
use swc_ecma_utils::{ident::IdentLike, Id};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_utils::ident::IdentLike;

/// Methods related to the option `collapse_vars`.
impl<M> Optimizer<'_, M>
Expand Down Expand Up @@ -78,26 +76,3 @@ where
}
}
}

struct Inliner<'a> {
values: &'a mut AHashMap<Id, Option<Box<Expr>>>,
}

impl VisitMut for Inliner<'_> {
noop_visit_mut_type!();

fn visit_mut_expr(&mut self, e: &mut Expr) {
e.visit_mut_children_with(self);

match e {
Expr::Ident(i) => {
if let Some(value) = self.values.remove(&i.to_id()) {
tracing::debug!("collapse_vars: Inlining {}{:?}", i.sym, i.span.ctxt);

*e = *value.expect("should be used only once");
}
}
_ => {}
}
}
}
44 changes: 44 additions & 0 deletions ecmascript/minifier/src/compress/optimize/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ impl VisitMut for MultiReplacer {
}
}
}

fn visit_mut_prop(&mut self, p: &mut Prop) {
p.visit_mut_children_with(self);

match p {
Prop::Shorthand(i) => {
if let Some(value) = self.vars.remove(&i.to_id()) {
debug!("multi-replacer: Replaced `{}` as shorthand", i);
self.changed = true;

*p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone()),
value,
});

return;
}
}
_ => {}
}
}
}

pub(crate) fn replace_id_with_expr<N>(node: &mut N, from: Id, to: Box<Expr>) -> Option<Box<Expr>>
Expand Down Expand Up @@ -266,4 +287,27 @@ impl VisitMut for ExprReplacer {
e.prop.visit_mut_with(self);
}
}

fn visit_mut_prop(&mut self, p: &mut Prop) {
p.visit_mut_children_with(self);

match p {
Prop::Shorthand(i) => {
if self.from.0 == i.sym && self.from.1 == i.span.ctxt {
let value = if let Some(new) = self.to.take() {
new
} else {
unreachable!("`{}` is already taken", i)
};
*p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone()),
value,
});

return;
}
}
_ => {}
}
}
}
8 changes: 8 additions & 0 deletions ecmascript/minifier/src/compress/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,14 @@ where

(self.op)(e);
}

fn visit_mut_member_expr(&mut self, e: &mut MemberExpr) {
e.obj.visit_mut_with(self);

if e.computed {
e.prop.visit_mut_with(self);
}
}
}

pub fn replace_expr<N, F>(node: &mut N, op: F)
Expand Down
Loading

1 comment on commit 0020e16

@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: 0020e16 Previous: 2ca6e5d Ratio
base_tr_fixer 33849 ns/iter (± 5205) 27732 ns/iter (± 2743) 1.22
base_tr_resolver_and_hygiene 128496 ns/iter (± 27046) 107252 ns/iter (± 11262) 1.20
codegen_es2015 70417 ns/iter (± 11072) 53897 ns/iter (± 19060) 1.31
codegen_es2016 73105 ns/iter (± 17546) 54988 ns/iter (± 9919) 1.33
codegen_es2017 70799 ns/iter (± 14896) 56570 ns/iter (± 4397) 1.25
codegen_es2018 71849 ns/iter (± 17135) 54630 ns/iter (± 13356) 1.32
codegen_es2019 72160 ns/iter (± 17515) 56623 ns/iter (± 4185) 1.27
codegen_es2020 71626 ns/iter (± 16054) 54864 ns/iter (± 7321) 1.31
codegen_es3 72402 ns/iter (± 13291) 56373 ns/iter (± 5828) 1.28
codegen_es5 70716 ns/iter (± 15944) 56184 ns/iter (± 29341) 1.26
config_for_file 23568 ns/iter (± 8629) 19086 ns/iter (± 3792) 1.23
full_es2015 229532041 ns/iter (± 13926441) 187136271 ns/iter (± 11048541) 1.23
full_es2016 171675621 ns/iter (± 18142194) 145324479 ns/iter (± 12702722) 1.18
full_es2017 182242178 ns/iter (± 19054693) 152404811 ns/iter (± 13695258) 1.20
full_es2018 174706756 ns/iter (± 12608764) 148359629 ns/iter (± 11332409) 1.18
full_es2019 174429033 ns/iter (± 18909785) 148244368 ns/iter (± 18565572) 1.18
full_es2020 170885905 ns/iter (± 11588651) 145515580 ns/iter (± 24699061) 1.17
full_es3 261544626 ns/iter (± 27164259) 221010248 ns/iter (± 44594966) 1.18
full_es5 236735120 ns/iter (± 18481658) 205131265 ns/iter (± 34439773) 1.15
parser 814665 ns/iter (± 118640) 737318 ns/iter (± 158855) 1.10
transforms_es2015 1282067 ns/iter (± 291239) 1015780 ns/iter (± 152613) 1.26
transforms_es2016 823340 ns/iter (± 211374) 599882 ns/iter (± 87007) 1.37
transforms_es2017 721313 ns/iter (± 191524) 609299 ns/iter (± 136281) 1.18
transforms_es2018 755063 ns/iter (± 221525) 598979 ns/iter (± 76260) 1.26
transforms_es2019 706509 ns/iter (± 193428) 594041 ns/iter (± 146936) 1.19
transforms_es2020 701131 ns/iter (± 182613) 573724 ns/iter (± 133589) 1.22
transforms_es3 1495490 ns/iter (± 628115) 1177417 ns/iter (± 194057) 1.27
transforms_es5 1225006 ns/iter (± 282300) 1007866 ns/iter (± 148396) 1.22
ser_ast_node 204 ns/iter (± 28) 170 ns/iter (± 19) 1.20
ser_serde 195 ns/iter (± 32) 163 ns/iter (± 30) 1.20
emit_colors 14680311 ns/iter (± 20998541) 15397342 ns/iter (± 20688528) 0.95
emit_large 79344918 ns/iter (± 104896694) 86070400 ns/iter (± 133753367) 0.92
base_clone 2657059 ns/iter (± 801608) 2523244 ns/iter (± 343022) 1.05
fold_span 4309329 ns/iter (± 851600) 4153555 ns/iter (± 434686) 1.04
fold_span_panic 4710664 ns/iter (± 1114486) 4474604 ns/iter (± 596246) 1.05
visit_mut_span 3169001 ns/iter (± 878929) 3034976 ns/iter (± 423561) 1.04
visit_mut_span_panic 3296839 ns/iter (± 849839) 3086534 ns/iter (± 355958) 1.07
boxing_boxed 164 ns/iter (± 35) 146 ns/iter (± 26) 1.12
boxing_boxed_clone 75 ns/iter (± 22) 65 ns/iter (± 13) 1.15
boxing_unboxed 153 ns/iter (± 37) 138 ns/iter (± 19) 1.11
boxing_unboxed_clone 70 ns/iter (± 17) 64 ns/iter (± 13) 1.09

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

Please sign in to comment.