From 0f5e982d19a8c7c21e12e3d66e4be200d5e28507 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 6 Aug 2024 04:20:41 +0000 Subject: [PATCH] perf(minifier): only visit arrow expression after dropping `console.log` (#4677) --- Cargo.lock | 1 - .../src/ast_passes/remove_syntax.rs | 13 ++++---- tasks/benchmark/Cargo.toml | 4 +-- tasks/benchmark/benches/minifier.rs | 32 +++---------------- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88240fb6bd0b3..b1d8917bb35e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1367,7 +1367,6 @@ version = "0.0.0" dependencies = [ "criterion2", "oxc_allocator", - "oxc_ast", "oxc_codegen", "oxc_isolated_declarations", "oxc_linter", diff --git a/crates/oxc_minifier/src/ast_passes/remove_syntax.rs b/crates/oxc_minifier/src/ast_passes/remove_syntax.rs index 347ad07f4d484..019da99db5af0 100644 --- a/crates/oxc_minifier/src/ast_passes/remove_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/remove_syntax.rs @@ -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); } } @@ -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; } } diff --git a/tasks/benchmark/Cargo.toml b/tasks/benchmark/Cargo.toml index c011217d7a9a7..925a950a9268a 100644 --- a/tasks/benchmark/Cargo.toml +++ b/tasks/benchmark/Cargo.toml @@ -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 } @@ -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", diff --git a/tasks/benchmark/benches/minifier.rs b/tasks/benchmark/benches/minifier.rs index 849910016e79f..c316839277a88 100644 --- a/tasks/benchmark/benches/minifier.rs +++ b/tasks/benchmark/benches/minifier.rs @@ -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; @@ -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 }); }, @@ -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);