Skip to content

Commit 96b4009

Browse files
committed
perf(minifier): remove late optimization pass (#12670)
Probably an overkill?
1 parent e0e835b commit 96b4009

File tree

5 files changed

+14
-66
lines changed

5 files changed

+14
-66
lines changed

crates/oxc_minifier/src/compressor.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use oxc_traverse::ReusableTraverseCtx;
55

66
use crate::{
77
CompressOptions,
8-
peephole::{
9-
DeadCodeElimination, LatePeepholeOptimizations, Normalize, NormalizeOptions,
10-
PeepholeOptimizations,
11-
},
8+
peephole::{DeadCodeElimination, Normalize, NormalizeOptions, PeepholeOptimizations},
129
state::MinifierState,
1310
};
1411

@@ -38,7 +35,6 @@ impl<'a> Compressor<'a> {
3835
NormalizeOptions { convert_while_to_fors: true, convert_const_to_let: true };
3936
Normalize::new(normalize_options).build(program, &mut ctx);
4037
PeepholeOptimizations::new().run_in_loop(program, &mut ctx);
41-
LatePeepholeOptimizations::new().build(program, &mut ctx);
4238
}
4339

4440
pub fn dead_code_elimination(self, program: &mut Program<'a>, options: CompressOptions) {

crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use oxc_ast::ast::*;
33

44
use crate::ctx::Ctx;
55

6-
use super::LatePeepholeOptimizations;
6+
use super::PeepholeOptimizations;
77

8-
impl<'a> LatePeepholeOptimizations {
8+
impl<'a> PeepholeOptimizations {
99
/// Converts property accesses from quoted string or bracket access syntax to dot or unquoted string
1010
/// syntax, where possible. Dot syntax is more compact.
1111
///

crates/oxc_minifier/src/peephole/mod.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ impl<'a> Traverse<'a, MinifierState<'a>> for PeepholeOptimizations {
189189
}
190190
}
191191

192-
fn exit_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
192+
fn exit_call_expression(&mut self, e: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
193193
let mut ctx = Ctx::new(ctx);
194-
195-
self.substitute_call_expression(expr, &mut ctx);
194+
self.substitute_call_expression(e, &mut ctx);
195+
Self::remove_empty_spread_arguments(&mut e.arguments);
196196
}
197197

198-
fn exit_new_expression(&mut self, expr: &mut NewExpression<'a>, ctx: &mut TraverseCtx<'a>) {
198+
fn exit_new_expression(&mut self, e: &mut NewExpression<'a>, ctx: &mut TraverseCtx<'a>) {
199199
let mut ctx = Ctx::new(ctx);
200-
201-
self.substitute_new_expression(expr, &mut ctx);
200+
self.substitute_new_expression(e, &mut ctx);
201+
Self::remove_empty_spread_arguments(&mut e.arguments);
202202
}
203203

204204
fn exit_object_property(&mut self, prop: &mut ObjectProperty<'a>, ctx: &mut TraverseCtx<'a>) {
@@ -262,27 +262,7 @@ impl<'a> Traverse<'a, MinifierState<'a>> for PeepholeOptimizations {
262262

263263
self.substitute_accessor_property(prop, &mut ctx);
264264
}
265-
}
266-
267-
/// Changes that do not interfere with optimizations that are run inside the fixed-point loop,
268-
/// which can be done as a last AST pass.
269-
pub struct LatePeepholeOptimizations;
270-
271-
impl<'a> LatePeepholeOptimizations {
272-
pub fn new() -> Self {
273-
Self
274-
}
275-
276-
pub fn build(
277-
&mut self,
278-
program: &mut Program<'a>,
279-
ctx: &mut ReusableTraverseCtx<'a, MinifierState<'a>>,
280-
) {
281-
traverse_mut_with_ctx(self, program, ctx);
282-
}
283-
}
284265

285-
impl<'a> Traverse<'a, MinifierState<'a>> for LatePeepholeOptimizations {
286266
fn exit_member_expression(
287267
&mut self,
288268
expr: &mut MemberExpression<'a>,
@@ -297,23 +277,10 @@ impl<'a> Traverse<'a, MinifierState<'a>> for LatePeepholeOptimizations {
297277
Self::remove_dead_code_exit_class_body(body, &mut ctx);
298278
}
299279

300-
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
301-
let mut ctx = Ctx::new(ctx);
302-
Self::substitute_exit_expression(expr, &mut ctx);
303-
}
304-
305280
fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
306281
let mut ctx = Ctx::new(ctx);
307282
self.substitute_catch_clause(catch, &mut ctx);
308283
}
309-
310-
fn exit_call_expression(&mut self, e: &mut CallExpression<'a>, _ctx: &mut TraverseCtx<'a>) {
311-
Self::remove_empty_spread_arguments(&mut e.arguments);
312-
}
313-
314-
fn exit_new_expression(&mut self, e: &mut NewExpression<'a>, _ctx: &mut TraverseCtx<'a>) {
315-
Self::remove_empty_spread_arguments(&mut e.arguments);
316-
}
317284
}
318285

319286
pub struct DeadCodeElimination {

crates/oxc_minifier/src/peephole/remove_dead_code.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use oxc_traverse::Ancestor;
77

88
use crate::{ctx::Ctx, keep_var::KeepVar};
99

10-
use super::{LatePeepholeOptimizations, PeepholeOptimizations};
10+
use super::PeepholeOptimizations;
1111

1212
/// Remove Dead Code from the AST.
1313
///
@@ -565,9 +565,7 @@ impl<'a> PeepholeOptimizations {
565565
_ => false,
566566
}
567567
}
568-
}
569568

570-
impl<'a> LatePeepholeOptimizations {
571569
pub fn remove_dead_code_exit_class_body(body: &mut ClassBody<'a>, _ctx: &mut Ctx<'a, '_>) {
572570
body.body.retain(|e| !matches!(e, ClassElement::StaticBlock(s) if s.body.is_empty()));
573571
}

crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use oxc_traverse::Ancestor;
1515

1616
use crate::ctx::Ctx;
1717

18-
use super::{LatePeepholeOptimizations, PeepholeOptimizations};
18+
use super::PeepholeOptimizations;
1919

2020
/// A peephole optimization that minimizes code by simplifying conditional
2121
/// expressions, replacing IFs with HOOKs, replacing object constructors
@@ -164,6 +164,7 @@ impl<'a> PeepholeOptimizations {
164164
Expression::BinaryExpression(e) => Self::swap_binary_expressions(e),
165165
Expression::FunctionExpression(e) => self.try_remove_name_from_functions(e, ctx),
166166
Expression::ClassExpression(e) => self.try_remove_name_from_classes(e, ctx),
167+
Expression::NewExpression(e) => Self::try_compress_typed_array_constructor(e, ctx),
167168
_ => {}
168169
}
169170

@@ -185,6 +186,8 @@ impl<'a> PeepholeOptimizations {
185186
Self::try_fold_object_or_array_constructor(e.span, name, &mut e.arguments, ctx)
186187
})
187188
.or_else(|| self.try_fold_simple_function_call(e, ctx)),
189+
Expression::BooleanLiteral(_) => Self::try_compress_boolean(expr, ctx),
190+
Expression::ArrayExpression(_) => Self::try_compress_array_expression(expr, ctx),
188191
_ => None,
189192
} {
190193
*expr = folded_expr;
@@ -993,22 +996,6 @@ impl<'a> PeepholeOptimizations {
993996
ctx.state.changed = true;
994997
}
995998
}
996-
}
997-
998-
impl<'a> LatePeepholeOptimizations {
999-
pub fn substitute_exit_expression(expr: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) {
1000-
if let Expression::NewExpression(e) = expr {
1001-
Self::try_compress_typed_array_constructor(e, ctx);
1002-
}
1003-
1004-
if let Some(folded_expr) = match expr {
1005-
Expression::BooleanLiteral(_) => Self::try_compress_boolean(expr, ctx),
1006-
Expression::ArrayExpression(_) => Self::try_compress_array_expression(expr, ctx),
1007-
_ => None,
1008-
} {
1009-
*expr = folded_expr;
1010-
}
1011-
}
1012999

10131000
/// `new Int8Array(0)` -> `new Int8Array()` (also for other TypedArrays)
10141001
fn try_compress_typed_array_constructor(e: &mut NewExpression<'a>, ctx: &mut Ctx<'a, '_>) {

0 commit comments

Comments
 (0)