Skip to content

Commit

Permalink
fix(es/minifier): Abort function inliner if keep_fnames is true (#…
Browse files Browse the repository at this point in the history
…8145)

**Related issue:**

 - vercel/next.js#56408
  • Loading branch information
kdy1 authored Oct 19, 2023
1 parent 9e71134 commit 35601e4
Show file tree
Hide file tree
Showing 27 changed files with 14,988 additions and 11,040 deletions.
62 changes: 62 additions & 0 deletions crates/dbg-swc/src/es/minifier/compare_opts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::{Context, Result};
use clap::Args;
use swc_common::SourceMap;
use swc_ecma_minifier::option::{CompressOptions, MangleOptions};

use crate::util::{
all_js_files,
minifier::{get_minified, get_minified_with_opts},
print_js,
};

/// [Experimental] Internal command to check the size difference caused by
/// options.
#[derive(Debug, Args)]
pub struct CompareOptsCommand {
pub path: PathBuf,
}

impl CompareOptsCommand {
pub fn run(self, cm: Arc<SourceMap>) -> Result<()> {
let files = all_js_files(&self.path)?;

let mut default_sum = 0;
let mut new_sum = 0;

for file in files {
let default_record = get_minified(cm.clone(), &file, true, true)?;
let default_code = print_js(cm.clone(), &default_record.module, true)
.context("failed to convert ast to code")?;

eprintln!("default: {} bytes", default_code.as_bytes().len());
default_sum += default_code.as_bytes().len();

let new_record = get_minified_with_opts(
cm.clone(),
&file,
Some(CompressOptions {
keep_classnames: true,
keep_fnames: true,
..Default::default()
}),
Some(MangleOptions {
keep_class_names: true,
keep_fn_names: true,
..Default::default()
}),
)?;
let new_code = print_js(cm.clone(), &new_record.module, true)
.context("failed to convert ast to code")?;

eprintln!("new: {} bytes", new_code.as_bytes().len());
new_sum += new_code.as_bytes().len();
}

eprintln!("default (total): {} bytes", default_sum);
eprintln!("new (total): {} bytes", new_sum);

Ok(())
}
}
6 changes: 5 additions & 1 deletion crates/dbg-swc/src/es/minifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use clap::Subcommand;
use swc_common::SourceMap;

use self::{
compare::CompareCommand, ensure_size::EnsureSize, next::NextCommand, reduce::ReduceCommand,
compare::CompareCommand, compare_opts::CompareOptsCommand, ensure_size::EnsureSize,
next::NextCommand, reduce::ReduceCommand,
};

mod compare;
mod compare_opts;
mod ensure_size;
mod next;
mod reduce;
Expand All @@ -20,6 +22,7 @@ pub enum MinifierCommand {
Next(NextCommand),
Reduce(ReduceCommand),
Compare(CompareCommand),
CompareOpts(CompareOptsCommand),
EnsureSize(EnsureSize),
}

Expand All @@ -30,6 +33,7 @@ impl MinifierCommand {
MinifierCommand::Reduce(cmd) => cmd.run(cm),
MinifierCommand::EnsureSize(cmd) => cmd.run(cm),
MinifierCommand::Compare(cmd) => cmd.run(cm),
MinifierCommand::CompareOpts(cmd) => cmd.run(cm),
}
}
}
36 changes: 25 additions & 11 deletions crates/dbg-swc/src/util/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use anyhow::{bail, Context, Result};
use swc_common::{FileName, SourceMap};
use swc_ecma_ast::*;
use swc_ecma_minifier::option::MinifyOptions;
use swc_ecma_minifier::option::{CompressOptions, MangleOptions, MinifyOptions};
use swc_ecma_transforms_base::fixer::fixer;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};

Expand All @@ -18,6 +18,28 @@ pub fn get_minified(
file: &Path,
compress: bool,
mangle: bool,
) -> Result<ModuleRecord> {
get_minified_with_opts(
cm,
file,
if compress {
Some(Default::default())
} else {
None
},
if mangle {
Some(Default::default())
} else {
None
},
)
}

pub fn get_minified_with_opts(
cm: Arc<SourceMap>,
file: &Path,
compress: Option<CompressOptions>,
mangle: Option<MangleOptions>,
) -> Result<ModuleRecord> {
let fm = cm.load_file(file)?;

Expand All @@ -30,16 +52,8 @@ pub fn get_minified(
Some(&m.comments),
None,
&MinifyOptions {
compress: if compress {
Some(Default::default())
} else {
None
},
mangle: if mangle {
Some(Default::default())
} else {
None
},
compress,
mangle,
..Default::default()
},
&swc_ecma_minifier::option::ExtraOptions {
Expand Down
19 changes: 17 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,32 @@ impl Optimizer<'_> {
}
}

self.changed = true;
#[cfg(feature = "debug")]
#[allow(unused)]
match &decl {
Decl::Class(c) => {
if self.options.keep_classnames
|| self.mangle_options.map_or(false, |v| v.keep_class_names)
{
log_abort!("inline: [x] Keep fn names");
return;
}

self.changed = true;
report_change!(
"inline: Decided to inline class `{}{:?}` as it's used only once",
c.ident.sym,
c.ident.span.ctxt
);
}
Decl::Fn(f) => {
if self.options.keep_fnames
|| self.mangle_options.map_or(false, |v| v.keep_fn_names)
{
log_abort!("inline: [x] Keep fn names");
return;
}

self.changed = true;
report_change!(
"inline: Decided to inline function `{}{:?}` as it's used only once",
f.ident.sym,
Expand Down
Loading

1 comment on commit 35601e4

@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: 35601e4 Previous: 13106e0 Ratio
es/full/bugs-1 282381 ns/iter (± 8088) 284560 ns/iter (± 3390) 0.99
es/full/minify/libraries/antd 1384735159 ns/iter (± 13929748) 1383193821 ns/iter (± 13560992) 1.00
es/full/minify/libraries/d3 297732456 ns/iter (± 4893655) 295542397 ns/iter (± 3617008) 1.01
es/full/minify/libraries/echarts 1107397736 ns/iter (± 6664232) 1109847825 ns/iter (± 6939487) 1.00
es/full/minify/libraries/jquery 88852079 ns/iter (± 1369713) 89467381 ns/iter (± 771486) 0.99
es/full/minify/libraries/lodash 104605158 ns/iter (± 377377) 104195677 ns/iter (± 241040) 1.00
es/full/minify/libraries/moment 52526592 ns/iter (± 101647) 52319062 ns/iter (± 148940) 1.00
es/full/minify/libraries/react 18968471 ns/iter (± 48704) 18788957 ns/iter (± 43939) 1.01
es/full/minify/libraries/terser 229655008 ns/iter (± 974438) 230107110 ns/iter (± 944521) 1.00
es/full/minify/libraries/three 407616515 ns/iter (± 1589913) 408363859 ns/iter (± 2399893) 1.00
es/full/minify/libraries/typescript 2746192461 ns/iter (± 16143038) 2736354242 ns/iter (± 6486448) 1.00
es/full/minify/libraries/victory 585550951 ns/iter (± 5839114) 589740453 ns/iter (± 4873132) 0.99
es/full/minify/libraries/vue 126001124 ns/iter (± 432774) 125492488 ns/iter (± 193039) 1.00
es/full/codegen/es3 34564 ns/iter (± 84) 33588 ns/iter (± 151) 1.03
es/full/codegen/es5 34588 ns/iter (± 81) 33671 ns/iter (± 189) 1.03
es/full/codegen/es2015 34602 ns/iter (± 88) 33457 ns/iter (± 91) 1.03
es/full/codegen/es2016 34708 ns/iter (± 103) 33536 ns/iter (± 111) 1.03
es/full/codegen/es2017 34658 ns/iter (± 56) 33644 ns/iter (± 96) 1.03
es/full/codegen/es2018 34533 ns/iter (± 53) 33506 ns/iter (± 151) 1.03
es/full/codegen/es2019 34592 ns/iter (± 60) 33438 ns/iter (± 96) 1.03
es/full/codegen/es2020 34703 ns/iter (± 79) 33415 ns/iter (± 59) 1.04
es/full/all/es3 180175010 ns/iter (± 1267890) 176370241 ns/iter (± 1138533) 1.02
es/full/all/es5 172481674 ns/iter (± 1242607) 170723592 ns/iter (± 1033427) 1.01
es/full/all/es2015 130495456 ns/iter (± 875574) 127382600 ns/iter (± 419235) 1.02
es/full/all/es2016 128834342 ns/iter (± 1118609) 126985337 ns/iter (± 951262) 1.01
es/full/all/es2017 129391745 ns/iter (± 844362) 126074714 ns/iter (± 616334) 1.03
es/full/all/es2018 126918409 ns/iter (± 590606) 123968281 ns/iter (± 678247) 1.02
es/full/all/es2019 126524720 ns/iter (± 375931) 124023376 ns/iter (± 930039) 1.02
es/full/all/es2020 122035228 ns/iter (± 760583) 119918511 ns/iter (± 511295) 1.02
es/full/parser 585561 ns/iter (± 2693) 559131 ns/iter (± 3507) 1.05
es/full/base/fixer 20049 ns/iter (± 421) 18015 ns/iter (± 242) 1.11
es/full/base/resolver_and_hygiene 84265 ns/iter (± 257) 83844 ns/iter (± 260) 1.01

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

Please sign in to comment.