Skip to content

Commit

Permalink
fix(es/minifier): Don't inline functions if keep_fnames is enabled (#…
Browse files Browse the repository at this point in the history
…8093)

**Related issue:**

 - vercel/next.js#56550
  • Loading branch information
kdy1 authored Oct 12, 2023
1 parent 372f78c commit 94bb42e
Show file tree
Hide file tree
Showing 20 changed files with 3,031 additions and 2,732 deletions.
6 changes: 5 additions & 1 deletion crates/swc_ecma_minifier/src/compress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
compress::hoist_decls::decl_hoister,
debug::{dump, AssertValid},
mode::Mode,
option::CompressOptions,
option::{CompressOptions, MangleOptions},
program_data::{analyze, ProgramData},
util::{now, unit::CompileUnit},
};
Expand All @@ -41,6 +41,7 @@ mod util;
pub(crate) fn compressor<'a, M>(
marks: Marks,
options: &'a CompressOptions,
mangle_options: Option<&'a MangleOptions>,
mode: &'a M,
) -> impl 'a + VisitMut
where
Expand All @@ -49,6 +50,7 @@ where
let compressor = Compressor {
marks,
options,
mangle_options,
changed: false,
pass: 1,
dump_for_infinite_loop: Default::default(),
Expand All @@ -70,6 +72,7 @@ where
struct Compressor<'a> {
marks: Marks,
options: &'a CompressOptions,
mangle_options: Option<&'a MangleOptions>,
changed: bool,
pass: usize,

Expand Down Expand Up @@ -269,6 +272,7 @@ impl Compressor<'_> {
let mut visitor = optimizer(
self.marks,
self.options,
self.mangle_options,
&mut data,
self.mode,
!self.dump_for_infinite_loop.is_empty(),
Expand Down
16 changes: 16 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ impl Optimizer<'_> {
)
}

if u.declared_as_fn_decl || u.declared_as_fn_expr {
if self.options.keep_fnames
|| self.mangle_options.map_or(false, |v| v.keep_fn_names)
{
should_inline = false
}
}

should_inline
} else {
false
Expand Down Expand Up @@ -411,6 +419,14 @@ impl Optimizer<'_> {
if init_usage.reassigned || !init_usage.declared {
return;
}

if init_usage.declared_as_fn_decl || init_usage.declared_as_fn_expr {
if self.options.keep_fnames
|| self.mangle_options.map_or(false, |v| v.keep_fn_names)
{
return;
}
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
debug::AssertValid,
maybe_par,
mode::Mode,
option::CompressOptions,
option::{CompressOptions, MangleOptions},
program_data::ProgramData,
util::{
contains_eval, contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt,
Expand Down Expand Up @@ -59,6 +59,7 @@ mod util;
pub(super) fn optimizer<'a>(
marks: Marks,
options: &'a CompressOptions,
mangle_options: Option<&'a MangleOptions>,
data: &'a mut ProgramData,
mode: &'a dyn Mode,
debug_infinite_loop: bool,
Expand All @@ -82,6 +83,7 @@ pub(super) fn optimizer<'a>(
},
changed: false,
options,
mangle_options,
prepend_stmts: Default::default(),
append_stmts: Default::default(),
vars: Default::default(),
Expand Down Expand Up @@ -197,6 +199,7 @@ struct Optimizer<'a> {

changed: bool,
options: &'a CompressOptions,
mangle_options: Option<&'a MangleOptions>,
/// Statements prepended to the current statement.
prepend_stmts: SynthesizedStmts,
/// Statements appended to the current statement.
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Evaluator {
top_level: Some(TopLevelOptions { functions: true }),
..Default::default()
},
None,
&data,
));
}
Expand Down
15 changes: 10 additions & 5 deletions crates/swc_ecma_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,30 @@ pub fn optimize(
if let Some(ref mut t) = timings {
t.section("compress");
}
if let Some(options) = &options.compress {
if let Some(c) = &options.compress {
{
let _timer = timer!("compress ast");

n.visit_mut_with(&mut compressor(marks, options, &Minification))
n.visit_mut_with(&mut compressor(
marks,
c,
options.mangle.as_ref(),
&Minification,
))
}

// Again, we don't need to validate ast

let _timer = timer!("postcompress");

n.visit_mut_with(&mut postcompress_optimizer(options));
n.visit_mut_with(&mut postcompress_optimizer(c));

let mut pass = 0;
loop {
pass += 1;

let mut v = pure_optimizer(
options,
c,
None,
marks,
PureOptimizerConfig {
Expand All @@ -207,7 +212,7 @@ pub fn optimize(
},
);
n.visit_mut_with(&mut v);
if !v.changed() || options.passes <= pass {
if !v.changed() || c.passes <= pass {
break;
}
}
Expand Down
Loading

1 comment on commit 94bb42e

@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: 94bb42e Previous: 372f78c Ratio
es/full/bugs-1 285547 ns/iter (± 4968) 281508 ns/iter (± 7199) 1.01
es/full/minify/libraries/antd 1384019172 ns/iter (± 10235460) 1375407336 ns/iter (± 9718592) 1.01
es/full/minify/libraries/d3 293629949 ns/iter (± 3237642) 293331337 ns/iter (± 5077896) 1.00
es/full/minify/libraries/echarts 1107905754 ns/iter (± 6432594) 1096627249 ns/iter (± 8269928) 1.01
es/full/minify/libraries/jquery 89193082 ns/iter (± 72690) 88715638 ns/iter (± 131183) 1.01
es/full/minify/libraries/lodash 104083656 ns/iter (± 401553) 103771800 ns/iter (± 375523) 1.00
es/full/minify/libraries/moment 52654283 ns/iter (± 59917) 52402029 ns/iter (± 48955) 1.00
es/full/minify/libraries/react 19015705 ns/iter (± 311485) 18848957 ns/iter (± 21852) 1.01
es/full/minify/libraries/terser 229947921 ns/iter (± 1545363) 228930594 ns/iter (± 1215580) 1.00
es/full/minify/libraries/three 406409221 ns/iter (± 2216350) 406492492 ns/iter (± 2765168) 1.00
es/full/minify/libraries/typescript 2773681440 ns/iter (± 8354895) 2753892662 ns/iter (± 7739949) 1.01
es/full/minify/libraries/victory 593876480 ns/iter (± 4842106) 596043779 ns/iter (± 3289743) 1.00
es/full/minify/libraries/vue 127056849 ns/iter (± 399339) 125875046 ns/iter (± 469057) 1.01
es/full/codegen/es3 33686 ns/iter (± 271) 34094 ns/iter (± 97) 0.99
es/full/codegen/es5 33703 ns/iter (± 54) 33852 ns/iter (± 101) 1.00
es/full/codegen/es2015 33658 ns/iter (± 121) 33968 ns/iter (± 77) 0.99
es/full/codegen/es2016 33665 ns/iter (± 69) 33937 ns/iter (± 117) 0.99
es/full/codegen/es2017 33661 ns/iter (± 73) 33943 ns/iter (± 101) 0.99
es/full/codegen/es2018 33744 ns/iter (± 184) 33921 ns/iter (± 53) 0.99
es/full/codegen/es2019 33714 ns/iter (± 164) 33909 ns/iter (± 91) 0.99
es/full/codegen/es2020 33608 ns/iter (± 97) 33878 ns/iter (± 116) 0.99
es/full/all/es3 178537344 ns/iter (± 986182) 176920780 ns/iter (± 1133187) 1.01
es/full/all/es5 171869923 ns/iter (± 1152207) 169608886 ns/iter (± 1541830) 1.01
es/full/all/es2015 130204576 ns/iter (± 1020594) 127833562 ns/iter (± 852101) 1.02
es/full/all/es2016 128721904 ns/iter (± 805201) 126792210 ns/iter (± 816048) 1.02
es/full/all/es2017 127961082 ns/iter (± 789263) 126771693 ns/iter (± 454939) 1.01
es/full/all/es2018 126026218 ns/iter (± 610273) 124759234 ns/iter (± 673419) 1.01
es/full/all/es2019 125559423 ns/iter (± 553561) 123976104 ns/iter (± 607337) 1.01
es/full/all/es2020 120819025 ns/iter (± 793601) 119629586 ns/iter (± 810818) 1.01
es/full/parser 564180 ns/iter (± 3878) 562052 ns/iter (± 3153) 1.00
es/full/base/fixer 17465 ns/iter (± 33) 18615 ns/iter (± 114) 0.94
es/full/base/resolver_and_hygiene 84589 ns/iter (± 2889) 82930 ns/iter (± 223) 1.02

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

Please sign in to comment.