Skip to content

Commit

Permalink
feat(es/minifier): Ignore closure in initializer from sequential inli…
Browse files Browse the repository at this point in the history
…ner (#5702)
  • Loading branch information
kdy1 authored Aug 31, 2022
1 parent 32f6b67 commit 217f519
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ var Private = function Private() {
"use strict";
for(var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++)args[_key] = arguments[_key];
_class_call_check(this, Private);
}, Private2 = function Private2() {
"use strict";
for(var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++)args[_key] = arguments[_key];
_class_call_check(this, Private2);
}, Protected = function Protected() {
"use strict";
for(var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++)args[_key] = arguments[_key];
Expand All @@ -27,7 +23,11 @@ var Private = function Private() {
for(var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++)args[_key] = arguments[_key];
_class_call_check(this, Public2);
};
Mix(Private, Private2), Mix(Private, Protected), Mix(Private, Public);
Mix(Private, function Private2() {
"use strict";
for(var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++)args[_key] = arguments[_key];
_class_call_check(this, Private2);
}), Mix(Private, Protected), Mix(Private, Public);
var C4 = function(_superClass) {
"use strict";
_inherits(C4, _superClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import _get_prototype_of from "@swc/helpers/src/_get_prototype_of.mjs";
import _inherits from "@swc/helpers/src/_inherits.mjs";
import _create_super from "@swc/helpers/src/_create_super.mjs";
import _ts_decorate from "@swc/helpers/src/_ts_decorate.mjs";
var _C, _D, C = (_C = function C() {
var _C, _D, C = (_define_property(_C = function C() {
"use strict";
_class_call_check(this, C);
}, _define_property(_C, "a", 1), _define_property(_C, "b", _C.a + 1), _C), D = (_D = function(C) {
}, "a", 1), _define_property(_C, "b", _C.a + 1), _C), D = (_D = function(C) {
"use strict";
_inherits(D, C);
var _super = _create_super(D);
Expand Down
39 changes: 37 additions & 2 deletions crates/swc_ecma_minifier/src/alias/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,50 @@ use crate::marks::Marks;

mod ctx;

#[derive(Default)]
pub(crate) struct AliasConfig {
pub marks: Option<Marks>,
pub ignore_nested: bool,
}

pub(crate) trait InfectableNode {
fn is_fn_or_arrow_expr(&self) -> bool;
}

impl InfectableNode for Function {
fn is_fn_or_arrow_expr(&self) -> bool {
false
}
}

impl InfectableNode for Expr {
fn is_fn_or_arrow_expr(&self) -> bool {
match self {
Expr::Arrow(..) | Expr::Fn(..) => true,
_ => false,
}
}
}

impl<T> InfectableNode for Box<T>
where
T: InfectableNode,
{
fn is_fn_or_arrow_expr(&self) -> bool {
(**self).is_fn_or_arrow_expr()
}
}

pub(crate) fn collect_infects_from<N>(node: &N, config: AliasConfig) -> FxHashSet<Id>
where
N: for<'aa> VisitWith<InfectionCollector<'aa>>,
N: VisitWith<BindingCollector<Id>>,
N: InfectableNode
+ VisitWith<BindingCollector<Id>>
+ for<'aa> VisitWith<InfectionCollector<'aa>>,
{
if config.ignore_nested && node.is_fn_or_arrow_expr() {
return Default::default();
}

let unresolved_ctxt = config
.marks
.map(|m| SyntaxContext::empty().apply_mark(m.unresolved_mark));
Expand Down
24 changes: 21 additions & 3 deletions crates/swc_ecma_minifier/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,13 @@ where
self.used_recursively.remove(&id);

{
for id in collect_infects_from(&n.function, AliasConfig { marks: self.marks }) {
for id in collect_infects_from(
&n.function,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
self.data
.var_or_default(n.ident.to_id())
.add_infects_to(id.clone());
Expand All @@ -740,7 +746,13 @@ where
.mark_declared_as_fn_expr();

{
for id in collect_infects_from(&n.function, AliasConfig { marks: self.marks }) {
for id in collect_infects_from(
&n.function,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
self.data
.var_or_default(n_id.to_id())
.add_infects_to(id.to_id());
Expand Down Expand Up @@ -1100,7 +1112,13 @@ where

for decl in &n.decls {
if let (Pat::Ident(var), Some(init)) = (&decl.name, decl.init.as_deref()) {
for id in collect_infects_from(init, AliasConfig { marks: self.marks }) {
for id in collect_infects_from(
init,
AliasConfig {
marks: self.marks,
..Default::default()
},
) {
self.data
.var_or_default(var.to_id())
.add_infects_to(id.clone());
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ where
init,
AliasConfig {
marks: Some(self.marks),
ignore_nested: true,
},
)
}),
Expand All @@ -992,6 +993,7 @@ where
right,
AliasConfig {
marks: Some(self.marks),
ignore_nested: true,
},
))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7795,7 +7795,7 @@
has(AllSymbols, key) && (!IS_OBJECT_PROTOTYPE || has(ObjectPrototype, key)) && result.push(AllSymbols[key]);
}), result;
};
if (NATIVE_SYMBOL || ($Symbol = function() {
if (NATIVE_SYMBOL || (redefine(($Symbol = function() {
if (this instanceof $Symbol) throw TypeError("Symbol is not a constructor");
var description = arguments.length && void 0 !== arguments[0] ? $toString(arguments[0]) : void 0, tag = uid(description), setter = function(value) {
this === ObjectPrototype && setter.call(ObjectPrototypeSymbols, value), has(this, HIDDEN) && has(this[HIDDEN], tag) && (this[HIDDEN][tag] = !1), setSymbolDescriptor(this, tag, createPropertyDescriptor(1, value));
Expand All @@ -7804,7 +7804,7 @@
configurable: !0,
set: setter
}), wrap(tag, description);
}, redefine($Symbol[PROTOTYPE], "toString", function() {
})[PROTOTYPE], "toString", function() {
return getInternalState(this).tag;
}), redefine($Symbol, "withoutSetter", function(description) {
return wrap(uid(description), description);
Expand Down
Loading

1 comment on commit 217f519

@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: 217f519 Previous: fc28242 Ratio
es/full/minify/libraries/antd 1677891530 ns/iter (± 41356638) 1775700084 ns/iter (± 31553744) 0.94
es/full/minify/libraries/d3 419187061 ns/iter (± 5636473) 411784370 ns/iter (± 14081908) 1.02
es/full/minify/libraries/echarts 1498701275 ns/iter (± 47655445) 1588564561 ns/iter (± 32259047) 0.94
es/full/minify/libraries/jquery 106195833 ns/iter (± 7582977) 95898995 ns/iter (± 7927617) 1.11
es/full/minify/libraries/lodash 123029401 ns/iter (± 3000002) 121222720 ns/iter (± 8310119) 1.01
es/full/minify/libraries/moment 55787501 ns/iter (± 1133520) 55245259 ns/iter (± 2893441) 1.01
es/full/minify/libraries/react 20107229 ns/iter (± 703480) 18677087 ns/iter (± 565625) 1.08
es/full/minify/libraries/terser 325413688 ns/iter (± 11135578) 322849683 ns/iter (± 8777172) 1.01
es/full/minify/libraries/three 582426714 ns/iter (± 15984180) 591065098 ns/iter (± 12175082) 0.99
es/full/minify/libraries/typescript 3582327869 ns/iter (± 72147446) 3781556635 ns/iter (± 61208053) 0.95
es/full/minify/libraries/victory 783705825 ns/iter (± 29957498) 832560194 ns/iter (± 20974869) 0.94
es/full/minify/libraries/vue 137690572 ns/iter (± 5411997) 139786234 ns/iter (± 9893646) 0.99
es/full/codegen/es3 32773 ns/iter (± 2431) 32359 ns/iter (± 934) 1.01
es/full/codegen/es5 32641 ns/iter (± 1405) 32366 ns/iter (± 1184) 1.01
es/full/codegen/es2015 32271 ns/iter (± 2854) 32334 ns/iter (± 2248) 1.00
es/full/codegen/es2016 32630 ns/iter (± 1324) 32446 ns/iter (± 1616) 1.01
es/full/codegen/es2017 32346 ns/iter (± 831) 32370 ns/iter (± 1040) 1.00
es/full/codegen/es2018 32603 ns/iter (± 1371) 32348 ns/iter (± 283) 1.01
es/full/codegen/es2019 32666 ns/iter (± 769) 32338 ns/iter (± 991) 1.01
es/full/codegen/es2020 32608 ns/iter (± 791) 32285 ns/iter (± 2225) 1.01
es/full/all/es3 193313101 ns/iter (± 7248931) 189174226 ns/iter (± 12082985) 1.02
es/full/all/es5 179992260 ns/iter (± 11420472) 177705471 ns/iter (± 9479902) 1.01
es/full/all/es2015 144716500 ns/iter (± 3796720) 143842732 ns/iter (± 5140704) 1.01
es/full/all/es2016 143907593 ns/iter (± 3698600) 143123754 ns/iter (± 3452293) 1.01
es/full/all/es2017 144523676 ns/iter (± 6275469) 142817348 ns/iter (± 4478345) 1.01
es/full/all/es2018 147395625 ns/iter (± 7190814) 141141273 ns/iter (± 3966774) 1.04
es/full/all/es2019 142517484 ns/iter (± 90797611) 140371274 ns/iter (± 4537945) 1.02
es/full/all/es2020 137680017 ns/iter (± 6775815) 135525474 ns/iter (± 3843656) 1.02
es/full/parser 743527 ns/iter (± 25459) 738117 ns/iter (± 145474) 1.01
es/full/base/fixer 29435 ns/iter (± 2097) 28765 ns/iter (± 596) 1.02
es/full/base/resolver_and_hygiene 88573 ns/iter (± 12171) 86516 ns/iter (± 5589) 1.02
serialization of ast node 213 ns/iter (± 3) 212 ns/iter (± 3) 1.00
serialization of serde 229 ns/iter (± 1) 227 ns/iter (± 3) 1.01

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

Please sign in to comment.