Skip to content

Commit

Permalink
perf(minifier): only visit arrow expression after dropping console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 6, 2024
1 parent 4a56954 commit 259909d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions crates/oxc_minifier/src/ast_passes/remove_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ impl<'a> VisitMut<'a> for RemoveSyntax<'a> {
self.strip_parenthesized_expression(expr);
self.compress_console(expr);
walk_mut::walk_expression(self, expr);
}

fn visit_arrow_function_expression(&mut self, expr: &mut ArrowFunctionExpression<'a>) {
self.recover_arrow_expression_after_drop_console(expr);
}
}
Expand Down Expand Up @@ -68,13 +71,9 @@ impl<'a> RemoveSyntax<'a> {
}
}

fn recover_arrow_expression_after_drop_console(&self, expr: &mut Expression<'a>) {
if self.options.drop_console {
if let Expression::ArrowFunctionExpression(arrow_expr) = expr {
if arrow_expr.expression && arrow_expr.body.is_empty() {
arrow_expr.expression = false;
}
}
fn recover_arrow_expression_after_drop_console(&self, expr: &mut ArrowFunctionExpression<'a>) {
if self.options.drop_console && expr.expression && expr.body.is_empty() {
expr.expression = false;
}
}

Expand Down
4 changes: 1 addition & 3 deletions tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ name = "parser_napi"
harness = false
bench = false

[dependencies]
# All `oxc_*` dependencies optional as on CI we build each benchmark separately
# with only the crates it needs, to speed up the builds
oxc_ast = { workspace = true, optional = true }
[dependencies]
oxc_allocator = { workspace = true, optional = true }
oxc_linter = { workspace = true, optional = true }
oxc_minifier = { workspace = true, optional = true }
Expand Down Expand Up @@ -114,7 +113,6 @@ transformer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_t
semantic = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_semantic", "dep:oxc_span", "dep:oxc_tasks_common"]
minifier = [
"dep:oxc_allocator",
"dep:oxc_ast",
"dep:oxc_minifier",
"dep:oxc_parser",
"dep:oxc_span",
Expand Down
32 changes: 4 additions & 28 deletions tasks/benchmark/benches/minifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use oxc_allocator::Allocator;
use oxc_ast::AstBuilder;
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oxc_minifier::{CompressOptions, Minifier, MinifierOptions, RemoveSyntax};
use oxc_minifier::{CompressOptions, Compressor};
use oxc_parser::Parser;
use oxc_span::SourceType;
use oxc_tasks_common::TestFiles;
Expand All @@ -14,12 +13,12 @@ fn bench_minifier(criterion: &mut Criterion) {
BenchmarkId::from_parameter(&file.file_name),
&file.source_text,
|b, source_text| {
let options = MinifierOptions::default();
let options = CompressOptions::all_true();
b.iter_with_large_drop(|| {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
Minifier::new(options).build(&allocator, program);
Compressor::new(&allocator, options).build(program);
allocator
});
},
Expand All @@ -28,28 +27,5 @@ fn bench_minifier(criterion: &mut Criterion) {
group.finish();
}

fn bench_passes(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("prepass");

for file in TestFiles::minimal().files() {
let source_type = SourceType::from_path(&file.file_name).unwrap();
group.bench_with_input(
BenchmarkId::from_parameter(&file.file_name),
&file.source_text,
|b, source_text| {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
b.iter(|| {
RemoveSyntax::new(AstBuilder::new(&allocator), CompressOptions::all_true())
.build(program);
});
},
);
}

group.finish();
}

criterion_group!(minifier, bench_minifier, bench_passes);
criterion_group!(minifier, bench_minifier);
criterion_main!(minifier);

0 comments on commit 259909d

Please sign in to comment.