Skip to content

Commit

Permalink
refactor(minifier): fuse DCE AST passes (#8058)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 21, 2024
1 parent be2c60d commit 77d845a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
48 changes: 48 additions & 0 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub use peephole_substitute_alternate_syntax::PeepholeSubstituteAlternateSyntax;
pub use remove_syntax::RemoveSyntax;
pub use statement_fusion::StatementFusion;

use crate::CompressOptions;

pub trait CompressorPass<'a>: Traverse<'a> {
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>);
}
Expand Down Expand Up @@ -287,3 +289,49 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
self.x3_peephole_substitute_alternate_syntax.enter_binary_expression(expr, ctx);
}
}

pub struct DeadCodeElimination {
x0_remove_syntax: RemoveSyntax,
x1_peephole_fold_constants: PeepholeFoldConstants,
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode,
}

impl DeadCodeElimination {
pub fn new() -> Self {
Self {
x0_remove_syntax: RemoveSyntax::new(CompressOptions::all_false()),
x1_peephole_fold_constants: PeepholeFoldConstants::new(),
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
}
}
}

impl<'a> CompressorPass<'a> for DeadCodeElimination {
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
traverse_mut_with_ctx(self, program, ctx);
}
}

impl<'a> Traverse<'a> for DeadCodeElimination {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_remove_dead_code.enter_statement(stmt, ctx);
}

fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_remove_dead_code.exit_statement(stmt, ctx);
}

fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_remove_dead_code.exit_program(program, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_remove_dead_code.exit_statements(stmts, ctx);
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_remove_syntax.exit_expression(expr, ctx);
self.x1_peephole_fold_constants.exit_expression(expr, ctx);
self.x2_peephole_remove_dead_code.exit_expression(expr, ctx);
}
}
13 changes: 4 additions & 9 deletions crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use oxc_traverse::ReusableTraverseCtx;

use crate::{
ast_passes::{
CollapsePass, LatePeepholeOptimizations, PeepholeFoldConstants, PeepholeOptimizations,
PeepholeRemoveDeadCode, RemoveSyntax,
CollapsePass, DeadCodeElimination, LatePeepholeOptimizations, PeepholeOptimizations,
RemoveSyntax,
},
CompressOptions, CompressorPass,
};
Expand Down Expand Up @@ -44,10 +44,7 @@ impl<'a> Compressor<'a> {
pub fn dead_code_elimination(self, program: &mut Program<'a>) {
let (symbols, scopes) =
SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree();
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
RemoveSyntax::new(self.options).build(program, &mut ctx);
PeepholeFoldConstants::new().build(program, &mut ctx);
PeepholeRemoveDeadCode::new().build(program, &mut ctx);
self.dead_code_elimination_with_symbols_and_scopes(symbols, scopes, program);
}

pub fn dead_code_elimination_with_symbols_and_scopes(
Expand All @@ -57,8 +54,6 @@ impl<'a> Compressor<'a> {
program: &mut Program<'a>,
) {
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
RemoveSyntax::new(self.options).build(program, &mut ctx);
PeepholeFoldConstants::new().build(program, &mut ctx);
PeepholeRemoveDeadCode::new().build(program, &mut ctx);
DeadCodeElimination::new().build(program, &mut ctx);
}
}

0 comments on commit 77d845a

Please sign in to comment.