Skip to content

Commit

Permalink
refactor(es/minifier): Simplify analyzer context (#8164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Oct 27, 2023
1 parent cb1fa02 commit bb02cdd
Show file tree
Hide file tree
Showing 40 changed files with 665 additions and 766 deletions.
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-5xxx/5112/output/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function c(r, n) {
let e = new Uint8Array(4 * r * r), c = (r - n) / 2;
let e = new Uint8Array(4 * r * r);
return {
e,
s: Math.sqrt(1.25),
c
c: (r - n) / 2
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// https://github.com/microsoft/TypeScript/pull/41094#issuecomment-716044363
{
let b, a = 0;
[{ 1: b } = [
[{ [a = 1]: b } = [
9,
a
]] = [];
}{
let b, a = 1;
[{ [a]: b } = [
9,
0
a = 0
]] = [];
}{
let b, a = 0;
[{ 1: b } = [
[{ [a = 1]: b } = [
9,
a
]] = [
Expand All @@ -26,7 +26,7 @@
}{
let b, a = 1;
[{ [a]: b } = [
0,
a = 0,
9
]] = [
[
Expand All @@ -37,19 +37,19 @@
}// same as above but on left of a binary expression
{
let b, a = 0;
[{ 1: b } = [
[{ [a = 1]: b } = [
9,
a
]] = [], f();
}{
let b, a = 1;
[{ [a]: b } = [
9,
0
a = 0
]] = [], f();
}{
let b, a = 0;
[{ 1: b } = [
[{ [a = 1]: b } = [
9,
a
]] = [
Expand All @@ -61,7 +61,7 @@
}{
let b, a = 1;
[{ [a]: b } = [
0,
a = 0,
9
]] = [
[
Expand All @@ -72,19 +72,19 @@
}// same as above but on right of a binary expression
{
let b, a = 0;
f(), [{ 1: b } = [
f(), [{ [a = 1]: b } = [
9,
a
]] = [];
}{
let b, a = 1;
f(), [{ [a]: b } = [
9,
0
a = 0
]] = [];
}{
let b, a = 0;
f(), [{ 1: b } = [
f(), [{ [a = 1]: b } = [
9,
a
]] = [
Expand All @@ -96,7 +96,7 @@
}{
let b, a = 1;
f(), [{ [a]: b } = [
0,
a = 0,
9
]] = [
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [genericInstantiationEquivalentToObjectLiteral.ts]
var y;
f(y), f(y), f2(y), f2(y);
var x, y;
y = x = y, f(x), f(y), f2(x), f2(y);
13 changes: 6 additions & 7 deletions crates/swc/tests/tsc-references/objectRest.2.minified.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ var { removed } = removable;
_object_without_properties(removable, [
"removed"
]);
let computed = 'b', computed2 = 'a';
var { [computed]: stillNotGreat, [computed2]: soSo } = o, o = _object_without_properties(o, [
computed,
computed2
var { b: stillNotGreat, a: soSo } = o, o = _object_without_properties(o, [
'b',
'a'
].map(_to_property_key));
o = _object_without_properties(_o = o, [
computed,
computed2
].map(_to_property_key)), { [computed]: stillNotGreat, [computed2]: soSo } = _o;
'b',
'a'
].map(_to_property_key)), { b: stillNotGreat, a: soSo } = _o;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
var a, C = function C(x) {
var i, a, C = function C(x) {
return _class_call_check(this, C), x;
};
new C("a"), new C("hi"), new a("bye"), new a("hm");
new C("a"), a = i = a, new C("hi"), new i("bye"), new a("hm");
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl Optimizer<'_> {
if let Some(usage) = self.data.vars.get(&orig_params[idx].to_id()) {
if usage.ref_count == 1
&& !usage.reassigned
&& !usage.has_property_mutation
&& usage.property_mutation_count == 0
&& matches!(
&*arg,
Expr::Lit(
Expand Down
15 changes: 9 additions & 6 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl Optimizer<'_> {
&& usage.declared_count == 1
&& usage.assign_count == 1
&& !usage.reassigned
&& (usage.property_mutation_count == 0 || !usage.reassigned)
&& match init {
Expr::Ident(Ident { sym, .. }) if &**sym == "eval" => false,

Expand Down Expand Up @@ -238,7 +239,7 @@ impl Optimizer<'_> {
Expr::This(..) => usage.is_fn_local,
Expr::Arrow(arr) => {
is_arrow_simple_enough_for_copy(arr)
&& !(usage.has_property_mutation
&& !(usage.property_mutation_count > 0
|| usage.executed_multiple_time
|| usage.used_as_arg && ref_count > 1)
&& ref_count - 1 <= usage.callee_count
Expand All @@ -254,7 +255,7 @@ impl Optimizer<'_> {
indexed_with_dynamic_key,
usage_count,
has_property_access,
has_property_mutation,
property_mutation_count,
used_above_decl,
executed_multiple_time,
used_in_cond,
Expand All @@ -269,7 +270,7 @@ impl Optimizer<'_> {
u.used_as_ref |= used_as_ref;
u.indexed_with_dynamic_key |= indexed_with_dynamic_key;
u.has_property_access |= has_property_access;
u.has_property_mutation |= has_property_mutation;
u.property_mutation_count += property_mutation_count;
u.used_above_decl |= used_above_decl;
u.executed_multiple_time |= executed_multiple_time;
u.used_in_cond |= used_in_cond;
Expand Down Expand Up @@ -433,7 +434,10 @@ impl Optimizer<'_> {
_ => {
for id in idents_used_by(init) {
if let Some(v_usage) = self.data.vars.get(&id) {
if v_usage.reassigned || v_usage.has_property_mutation {
if v_usage.reassigned
|| v_usage.property_mutation_count
> usage.property_mutation_count
{
return;
}
}
Expand Down Expand Up @@ -692,8 +696,7 @@ impl Optimizer<'_> {
//
if (self.options.reduce_vars || self.options.collapse_vars || self.options.inline != 0)
&& usage.ref_count == 1
&& (usage.can_inline_fn_once())
&& !usage.inline_prevented
&& usage.can_inline_fn_once()
&& (match decl {
Decl::Class(..) => !usage.used_above_decl,
Decl::Fn(..) => true,
Expand Down
12 changes: 4 additions & 8 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,10 @@ impl VisitMut for Optimizer<'_> {
if init.is_invalid() {
var.init = None
}

if id.is_dummy() {
var.name = Pat::dummy();
}
};

self.store_var_for_prop_hoisting(var);
Expand All @@ -2902,8 +2906,6 @@ impl VisitMut for Optimizer<'_> {
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn visit_mut_var_declarators(&mut self, vars: &mut Vec<VarDeclarator>) {
vars.retain_mut(|var| {
let had_init = var.init.is_some();

if var.name.is_invalid() {
self.changed = true;
return false;
Expand All @@ -2917,12 +2919,6 @@ impl VisitMut for Optimizer<'_> {
return false;
}

// It will be inlined.
if had_init && var.init.is_none() {
self.changed = true;
return false;
}

debug_assert_valid(&*var);

true
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Optimizer<'_> {
if v.ref_count == 0
&& v.usage_count == 0
&& !v.reassigned
&& !v.has_property_mutation
&& v.property_mutation_count == 0
&& !v.declared_as_catch_param
{
self.changed = true;
Expand Down Expand Up @@ -435,7 +435,7 @@ impl Optimizer<'_> {
.data
.vars
.get(&ident.to_id())
.map(|v| v.usage_count == 0 && !v.has_property_mutation)
.map(|v| v.usage_count == 0 && v.property_mutation_count == 0)
.unwrap_or(false)
{
self.changed = true;
Expand Down Expand Up @@ -475,7 +475,7 @@ impl Optimizer<'_> {
.data
.vars
.get(&ident.to_id())
.map(|v| v.usage_count == 0 && !v.has_property_mutation)
.map(|v| v.usage_count == 0 && v.property_mutation_count == 0)
.unwrap_or(false)
{
self.changed = true;
Expand Down

1 comment on commit bb02cdd

@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: bb02cdd Previous: 7530e90 Ratio
es/full/bugs-1 298578 ns/iter (± 6088) 286968 ns/iter (± 13402) 1.04
es/full/minify/libraries/antd 1486112444 ns/iter (± 35178749) 1341778784 ns/iter (± 34555597) 1.11
es/full/minify/libraries/d3 305182569 ns/iter (± 4329440) 286306825 ns/iter (± 5170624) 1.07
es/full/minify/libraries/echarts 1147726147 ns/iter (± 30730669) 1067359691 ns/iter (± 6750095) 1.08
es/full/minify/libraries/jquery 87261083 ns/iter (± 366871) 88372825 ns/iter (± 1481513) 0.99
es/full/minify/libraries/lodash 102735490 ns/iter (± 872426) 103991719 ns/iter (± 1009148) 0.99
es/full/minify/libraries/moment 51432789 ns/iter (± 204092) 52124565 ns/iter (± 525162) 0.99
es/full/minify/libraries/react 18341540 ns/iter (± 138944) 18704977 ns/iter (± 184640) 0.98
es/full/minify/libraries/terser 225852642 ns/iter (± 2099047) 227235382 ns/iter (± 2340466) 0.99
es/full/minify/libraries/three 408145521 ns/iter (± 3077060) 399464002 ns/iter (± 6224476) 1.02
es/full/minify/libraries/typescript 2752304274 ns/iter (± 12251041) 2680478939 ns/iter (± 12629551) 1.03
es/full/minify/libraries/victory 608725327 ns/iter (± 5454452) 580038489 ns/iter (± 7410687) 1.05
es/full/minify/libraries/vue 124798748 ns/iter (± 473786) 124713445 ns/iter (± 3522768) 1.00
es/full/codegen/es3 34337 ns/iter (± 75) 32617 ns/iter (± 364) 1.05
es/full/codegen/es5 34390 ns/iter (± 146) 33320 ns/iter (± 461) 1.03
es/full/codegen/es2015 34288 ns/iter (± 58) 32898 ns/iter (± 580) 1.04
es/full/codegen/es2016 34441 ns/iter (± 96) 32808 ns/iter (± 352) 1.05
es/full/codegen/es2017 34487 ns/iter (± 63) 32618 ns/iter (± 474) 1.06
es/full/codegen/es2018 34352 ns/iter (± 88) 32810 ns/iter (± 449) 1.05
es/full/codegen/es2019 34353 ns/iter (± 28) 32484 ns/iter (± 254) 1.06
es/full/codegen/es2020 34386 ns/iter (± 126) 32670 ns/iter (± 363) 1.05
es/full/all/es3 179891236 ns/iter (± 1293795) 176786074 ns/iter (± 1902496) 1.02
es/full/all/es5 173116742 ns/iter (± 1599540) 169610556 ns/iter (± 2622552) 1.02
es/full/all/es2015 130565278 ns/iter (± 1064861) 130014048 ns/iter (± 1460973) 1.00
es/full/all/es2016 129621999 ns/iter (± 1148681) 127266324 ns/iter (± 1828597) 1.02
es/full/all/es2017 129291073 ns/iter (± 711630) 126088478 ns/iter (± 1377105) 1.03
es/full/all/es2018 126850152 ns/iter (± 977508) 124710900 ns/iter (± 1891410) 1.02
es/full/all/es2019 126210845 ns/iter (± 381354) 123453731 ns/iter (± 1249807) 1.02
es/full/all/es2020 122093603 ns/iter (± 848551) 118648506 ns/iter (± 2712879) 1.03
es/full/parser 569697 ns/iter (± 2965) 539582 ns/iter (± 11633) 1.06
es/full/base/fixer 17622 ns/iter (± 175) 17106 ns/iter (± 315) 1.03
es/full/base/resolver_and_hygiene 85456 ns/iter (± 149) 81135 ns/iter (± 1381) 1.05

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

Please sign in to comment.