Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Don't inline functions if keep_fnames is enabled #8093

Merged
merged 31 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading