Skip to content

Commit cfb51f2

Browse files
committed
refactor(minifier): fuse ast passes (#8184)
1 parent bf9cafe commit cfb51f2

File tree

3 files changed

+16
-137
lines changed

3 files changed

+16
-137
lines changed

crates/oxc_minifier/src/ast_passes/mod.rs

Lines changed: 12 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use oxc_ast::ast::*;
33
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
44

55
mod collapse_variable_declarations;
6-
mod exploit_assigns;
6+
// mod exploit_assigns;
77
mod normalize;
88
mod peephole_fold_constants;
99
mod peephole_minimize_conditions;
@@ -14,7 +14,7 @@ mod remove_syntax;
1414
mod statement_fusion;
1515

1616
pub use collapse_variable_declarations::CollapseVariableDeclarations;
17-
pub use exploit_assigns::ExploitAssigns;
17+
// pub use exploit_assigns::ExploitAssigns;
1818
pub use normalize::Normalize;
1919
pub use peephole_fold_constants::PeepholeFoldConstants;
2020
pub use peephole_minimize_conditions::PeepholeMinimizeConditions;
@@ -30,56 +30,20 @@ pub trait CompressorPass<'a>: Traverse<'a> {
3030
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>);
3131
}
3232

33-
// See `peepholeOptimizationsOnce`
34-
35-
// For pass:
36-
// ```
37-
// if (options.collapseVariableDeclarations) {
38-
// passes.maybeAdd(exploitAssign);
39-
// passes.maybeAdd(collapseVariableDeclarations);
40-
// }
41-
// ```
42-
pub struct CollapsePass {
43-
_x0_exploit_assigns: ExploitAssigns,
44-
x1_collapse_variable_declarations: CollapseVariableDeclarations,
45-
}
46-
47-
impl CollapsePass {
48-
pub fn new() -> Self {
49-
Self {
50-
_x0_exploit_assigns: ExploitAssigns::new(),
51-
x1_collapse_variable_declarations: CollapseVariableDeclarations::new(),
52-
}
53-
}
54-
}
55-
56-
impl<'a> CompressorPass<'a> for CollapsePass {
57-
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
58-
traverse_mut_with_ctx(self, program, ctx);
59-
}
60-
}
61-
62-
impl<'a> Traverse<'a> for CollapsePass {
63-
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
64-
self.x1_collapse_variable_declarations.exit_statements(stmts, ctx);
65-
}
66-
}
67-
6833
// See `latePeepholeOptimizations`
69-
pub struct LatePeepholeOptimizations {
34+
pub struct PeepholeOptimizations {
7035
x0_statement_fusion: StatementFusion,
7136
x1_collapse_variable_declarations: CollapseVariableDeclarations,
7237
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode,
73-
// TODO: MinimizeExitPoints
7438
x3_peephole_minimize_conditions: PeepholeMinimizeConditions,
7539
x4_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
7640
x5_peephole_replace_known_methods: PeepholeReplaceKnownMethods,
7741
x6_peephole_fold_constants: PeepholeFoldConstants,
42+
x7_collapse_variable_declarations: CollapseVariableDeclarations,
7843
}
7944

80-
impl LatePeepholeOptimizations {
81-
pub fn new(options: CompressOptions) -> Self {
82-
let in_fixed_loop = true;
45+
impl PeepholeOptimizations {
46+
pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self {
8347
Self {
8448
x0_statement_fusion: StatementFusion::new(),
8549
x1_collapse_variable_declarations: CollapseVariableDeclarations::new(),
@@ -91,6 +55,7 @@ impl LatePeepholeOptimizations {
9155
),
9256
x5_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
9357
x6_peephole_fold_constants: PeepholeFoldConstants::new(),
58+
x7_collapse_variable_declarations: CollapseVariableDeclarations::new(),
9459
}
9560
}
9661

@@ -102,6 +67,7 @@ impl LatePeepholeOptimizations {
10267
self.x4_peephole_substitute_alternate_syntax.changed = false;
10368
self.x5_peephole_replace_known_methods.changed = false;
10469
self.x6_peephole_fold_constants.changed = false;
70+
self.x7_collapse_variable_declarations.changed = false;
10571
}
10672

10773
fn changed(&self) -> bool {
@@ -112,6 +78,7 @@ impl LatePeepholeOptimizations {
11278
|| self.x4_peephole_substitute_alternate_syntax.changed
11379
|| self.x5_peephole_replace_known_methods.changed
11480
|| self.x6_peephole_fold_constants.changed
81+
|| self.x7_collapse_variable_declarations.changed
11582
}
11683

11784
pub fn run_in_loop<'a>(
@@ -135,13 +102,13 @@ impl LatePeepholeOptimizations {
135102
}
136103
}
137104

138-
impl<'a> CompressorPass<'a> for LatePeepholeOptimizations {
105+
impl<'a> CompressorPass<'a> for PeepholeOptimizations {
139106
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
140107
traverse_mut_with_ctx(self, program, ctx);
141108
}
142109
}
143110

144-
impl<'a> Traverse<'a> for LatePeepholeOptimizations {
111+
impl<'a> Traverse<'a> for PeepholeOptimizations {
145112
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
146113
self.x0_statement_fusion.exit_program(program, ctx);
147114
self.x2_peephole_remove_dead_code.exit_program(program, ctx);
@@ -155,6 +122,7 @@ impl<'a> Traverse<'a> for LatePeepholeOptimizations {
155122
self.x1_collapse_variable_declarations.exit_statements(stmts, ctx);
156123
self.x2_peephole_remove_dead_code.exit_statements(stmts, ctx);
157124
self.x3_peephole_minimize_conditions.exit_statements(stmts, ctx);
125+
self.x7_collapse_variable_declarations.exit_statements(stmts, ctx);
158126
}
159127

160128
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
@@ -203,90 +171,6 @@ impl<'a> Traverse<'a> for LatePeepholeOptimizations {
203171
}
204172
}
205173

206-
// See `createPeepholeOptimizationsPass`
207-
pub struct PeepholeOptimizations {
208-
// TODO: MinimizeExitPoints
209-
x2_peephole_minimize_conditions: PeepholeMinimizeConditions,
210-
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
211-
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods,
212-
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode,
213-
x6_peephole_fold_constants: PeepholeFoldConstants,
214-
}
215-
216-
impl PeepholeOptimizations {
217-
pub fn new(options: CompressOptions) -> Self {
218-
let in_fixed_loop = false;
219-
Self {
220-
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
221-
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
222-
options,
223-
in_fixed_loop,
224-
),
225-
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
226-
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
227-
x6_peephole_fold_constants: PeepholeFoldConstants::new(),
228-
}
229-
}
230-
}
231-
232-
impl<'a> CompressorPass<'a> for PeepholeOptimizations {
233-
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
234-
traverse_mut_with_ctx(self, program, ctx);
235-
}
236-
}
237-
238-
impl<'a> Traverse<'a> for PeepholeOptimizations {
239-
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
240-
self.x5_peephole_remove_dead_code.exit_program(program, ctx);
241-
}
242-
243-
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
244-
self.x2_peephole_minimize_conditions.exit_statements(stmts, ctx);
245-
self.x5_peephole_remove_dead_code.exit_statements(stmts, ctx);
246-
}
247-
248-
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
249-
self.x2_peephole_minimize_conditions.exit_statement(stmt, ctx);
250-
self.x5_peephole_remove_dead_code.exit_statement(stmt, ctx);
251-
}
252-
253-
fn exit_return_statement(&mut self, stmt: &mut ReturnStatement<'a>, ctx: &mut TraverseCtx<'a>) {
254-
self.x3_peephole_substitute_alternate_syntax.exit_return_statement(stmt, ctx);
255-
}
256-
257-
fn exit_variable_declaration(
258-
&mut self,
259-
decl: &mut VariableDeclaration<'a>,
260-
ctx: &mut TraverseCtx<'a>,
261-
) {
262-
self.x3_peephole_substitute_alternate_syntax.exit_variable_declaration(decl, ctx);
263-
}
264-
265-
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
266-
self.x2_peephole_minimize_conditions.exit_expression(expr, ctx);
267-
self.x3_peephole_substitute_alternate_syntax.exit_expression(expr, ctx);
268-
self.x4_peephole_replace_known_methods.exit_expression(expr, ctx);
269-
self.x5_peephole_remove_dead_code.exit_expression(expr, ctx);
270-
self.x6_peephole_fold_constants.exit_expression(expr, ctx);
271-
}
272-
273-
fn enter_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
274-
self.x3_peephole_substitute_alternate_syntax.enter_call_expression(expr, ctx);
275-
}
276-
277-
fn exit_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
278-
self.x3_peephole_substitute_alternate_syntax.exit_call_expression(expr, ctx);
279-
}
280-
281-
fn exit_property_key(&mut self, key: &mut PropertyKey<'a>, ctx: &mut TraverseCtx<'a>) {
282-
self.x3_peephole_substitute_alternate_syntax.exit_property_key(key, ctx);
283-
}
284-
285-
fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
286-
self.x3_peephole_substitute_alternate_syntax.exit_catch_clause(catch, ctx);
287-
}
288-
}
289-
290174
pub struct DeadCodeElimination {
291175
x1_peephole_fold_constants: PeepholeFoldConstants,
292176
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode,

crates/oxc_minifier/src/compressor.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use oxc_semantic::{ScopeTree, SemanticBuilder, SymbolTable};
44
use oxc_traverse::ReusableTraverseCtx;
55

66
use crate::{
7-
ast_passes::{
8-
CollapsePass, DeadCodeElimination, LatePeepholeOptimizations, Normalize,
9-
PeepholeOptimizations, RemoveSyntax,
10-
},
7+
ast_passes::{DeadCodeElimination, Normalize, PeepholeOptimizations, RemoveSyntax},
118
CompressOptions, CompressorPass,
129
};
1310

@@ -36,10 +33,8 @@ impl<'a> Compressor<'a> {
3633
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
3734
RemoveSyntax::new(self.options).build(program, &mut ctx);
3835
Normalize::new().build(program, &mut ctx);
39-
PeepholeOptimizations::new(self.options).build(program, &mut ctx);
40-
CollapsePass::new().build(program, &mut ctx);
41-
LatePeepholeOptimizations::new(self.options).run_in_loop(program, &mut ctx);
42-
PeepholeOptimizations::new(self.options).build(program, &mut ctx);
36+
PeepholeOptimizations::new(true, self.options).run_in_loop(program, &mut ctx);
37+
PeepholeOptimizations::new(false, self.options).build(program, &mut ctx);
4338
}
4439

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

tasks/minsize/minsize.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Original | minified | minified | gzip | gzip | Fixture
1111

1212
544.10 kB | 71.98 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
1313

14-
555.77 kB | 273.88 kB | 270.13 kB | 91.19 kB | 90.80 kB | d3.js
14+
555.77 kB | 273.85 kB | 270.13 kB | 91.17 kB | 90.80 kB | d3.js
1515

1616
1.01 MB | 460.99 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js
1717

0 commit comments

Comments
 (0)