mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(minifier): fuse ast passes (#8184)
This commit is contained in:
parent
bf9cafea90
commit
cfb51f2551
3 changed files with 16 additions and 137 deletions
|
|
@ -3,7 +3,7 @@ use oxc_ast::ast::*;
|
|||
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
|
||||
|
||||
mod collapse_variable_declarations;
|
||||
mod exploit_assigns;
|
||||
// mod exploit_assigns;
|
||||
mod normalize;
|
||||
mod peephole_fold_constants;
|
||||
mod peephole_minimize_conditions;
|
||||
|
|
@ -14,7 +14,7 @@ mod remove_syntax;
|
|||
mod statement_fusion;
|
||||
|
||||
pub use collapse_variable_declarations::CollapseVariableDeclarations;
|
||||
pub use exploit_assigns::ExploitAssigns;
|
||||
// pub use exploit_assigns::ExploitAssigns;
|
||||
pub use normalize::Normalize;
|
||||
pub use peephole_fold_constants::PeepholeFoldConstants;
|
||||
pub use peephole_minimize_conditions::PeepholeMinimizeConditions;
|
||||
|
|
@ -30,56 +30,20 @@ pub trait CompressorPass<'a>: Traverse<'a> {
|
|||
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>);
|
||||
}
|
||||
|
||||
// See `peepholeOptimizationsOnce`
|
||||
|
||||
// For pass:
|
||||
// ```
|
||||
// if (options.collapseVariableDeclarations) {
|
||||
// passes.maybeAdd(exploitAssign);
|
||||
// passes.maybeAdd(collapseVariableDeclarations);
|
||||
// }
|
||||
// ```
|
||||
pub struct CollapsePass {
|
||||
_x0_exploit_assigns: ExploitAssigns,
|
||||
x1_collapse_variable_declarations: CollapseVariableDeclarations,
|
||||
}
|
||||
|
||||
impl CollapsePass {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
_x0_exploit_assigns: ExploitAssigns::new(),
|
||||
x1_collapse_variable_declarations: CollapseVariableDeclarations::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CompressorPass<'a> for CollapsePass {
|
||||
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
|
||||
traverse_mut_with_ctx(self, program, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Traverse<'a> for CollapsePass {
|
||||
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x1_collapse_variable_declarations.exit_statements(stmts, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// See `latePeepholeOptimizations`
|
||||
pub struct LatePeepholeOptimizations {
|
||||
pub struct PeepholeOptimizations {
|
||||
x0_statement_fusion: StatementFusion,
|
||||
x1_collapse_variable_declarations: CollapseVariableDeclarations,
|
||||
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode,
|
||||
// TODO: MinimizeExitPoints
|
||||
x3_peephole_minimize_conditions: PeepholeMinimizeConditions,
|
||||
x4_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
|
||||
x5_peephole_replace_known_methods: PeepholeReplaceKnownMethods,
|
||||
x6_peephole_fold_constants: PeepholeFoldConstants,
|
||||
x7_collapse_variable_declarations: CollapseVariableDeclarations,
|
||||
}
|
||||
|
||||
impl LatePeepholeOptimizations {
|
||||
pub fn new(options: CompressOptions) -> Self {
|
||||
let in_fixed_loop = true;
|
||||
impl PeepholeOptimizations {
|
||||
pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self {
|
||||
Self {
|
||||
x0_statement_fusion: StatementFusion::new(),
|
||||
x1_collapse_variable_declarations: CollapseVariableDeclarations::new(),
|
||||
|
|
@ -91,6 +55,7 @@ impl LatePeepholeOptimizations {
|
|||
),
|
||||
x5_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
|
||||
x6_peephole_fold_constants: PeepholeFoldConstants::new(),
|
||||
x7_collapse_variable_declarations: CollapseVariableDeclarations::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -102,6 +67,7 @@ impl LatePeepholeOptimizations {
|
|||
self.x4_peephole_substitute_alternate_syntax.changed = false;
|
||||
self.x5_peephole_replace_known_methods.changed = false;
|
||||
self.x6_peephole_fold_constants.changed = false;
|
||||
self.x7_collapse_variable_declarations.changed = false;
|
||||
}
|
||||
|
||||
fn changed(&self) -> bool {
|
||||
|
|
@ -112,6 +78,7 @@ impl LatePeepholeOptimizations {
|
|||
|| self.x4_peephole_substitute_alternate_syntax.changed
|
||||
|| self.x5_peephole_replace_known_methods.changed
|
||||
|| self.x6_peephole_fold_constants.changed
|
||||
|| self.x7_collapse_variable_declarations.changed
|
||||
}
|
||||
|
||||
pub fn run_in_loop<'a>(
|
||||
|
|
@ -135,13 +102,13 @@ impl LatePeepholeOptimizations {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> CompressorPass<'a> for LatePeepholeOptimizations {
|
||||
impl<'a> CompressorPass<'a> for PeepholeOptimizations {
|
||||
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
|
||||
traverse_mut_with_ctx(self, program, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Traverse<'a> for LatePeepholeOptimizations {
|
||||
impl<'a> Traverse<'a> for PeepholeOptimizations {
|
||||
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x0_statement_fusion.exit_program(program, ctx);
|
||||
self.x2_peephole_remove_dead_code.exit_program(program, ctx);
|
||||
|
|
@ -155,6 +122,7 @@ impl<'a> Traverse<'a> for LatePeepholeOptimizations {
|
|||
self.x1_collapse_variable_declarations.exit_statements(stmts, ctx);
|
||||
self.x2_peephole_remove_dead_code.exit_statements(stmts, ctx);
|
||||
self.x3_peephole_minimize_conditions.exit_statements(stmts, ctx);
|
||||
self.x7_collapse_variable_declarations.exit_statements(stmts, ctx);
|
||||
}
|
||||
|
||||
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
|
|
@ -203,90 +171,6 @@ impl<'a> Traverse<'a> for LatePeepholeOptimizations {
|
|||
}
|
||||
}
|
||||
|
||||
// See `createPeepholeOptimizationsPass`
|
||||
pub struct PeepholeOptimizations {
|
||||
// TODO: MinimizeExitPoints
|
||||
x2_peephole_minimize_conditions: PeepholeMinimizeConditions,
|
||||
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
|
||||
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods,
|
||||
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode,
|
||||
x6_peephole_fold_constants: PeepholeFoldConstants,
|
||||
}
|
||||
|
||||
impl PeepholeOptimizations {
|
||||
pub fn new(options: CompressOptions) -> Self {
|
||||
let in_fixed_loop = false;
|
||||
Self {
|
||||
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
|
||||
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
|
||||
options,
|
||||
in_fixed_loop,
|
||||
),
|
||||
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
|
||||
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
|
||||
x6_peephole_fold_constants: PeepholeFoldConstants::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CompressorPass<'a> for PeepholeOptimizations {
|
||||
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
|
||||
traverse_mut_with_ctx(self, program, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Traverse<'a> for PeepholeOptimizations {
|
||||
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x5_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_minimize_conditions.exit_statements(stmts, ctx);
|
||||
self.x5_peephole_remove_dead_code.exit_statements(stmts, ctx);
|
||||
}
|
||||
|
||||
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x2_peephole_minimize_conditions.exit_statement(stmt, ctx);
|
||||
self.x5_peephole_remove_dead_code.exit_statement(stmt, ctx);
|
||||
}
|
||||
|
||||
fn exit_return_statement(&mut self, stmt: &mut ReturnStatement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x3_peephole_substitute_alternate_syntax.exit_return_statement(stmt, ctx);
|
||||
}
|
||||
|
||||
fn exit_variable_declaration(
|
||||
&mut self,
|
||||
decl: &mut VariableDeclaration<'a>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
) {
|
||||
self.x3_peephole_substitute_alternate_syntax.exit_variable_declaration(decl, ctx);
|
||||
}
|
||||
|
||||
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x2_peephole_minimize_conditions.exit_expression(expr, ctx);
|
||||
self.x3_peephole_substitute_alternate_syntax.exit_expression(expr, ctx);
|
||||
self.x4_peephole_replace_known_methods.exit_expression(expr, ctx);
|
||||
self.x5_peephole_remove_dead_code.exit_expression(expr, ctx);
|
||||
self.x6_peephole_fold_constants.exit_expression(expr, ctx);
|
||||
}
|
||||
|
||||
fn enter_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x3_peephole_substitute_alternate_syntax.enter_call_expression(expr, ctx);
|
||||
}
|
||||
|
||||
fn exit_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x3_peephole_substitute_alternate_syntax.exit_call_expression(expr, ctx);
|
||||
}
|
||||
|
||||
fn exit_property_key(&mut self, key: &mut PropertyKey<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x3_peephole_substitute_alternate_syntax.exit_property_key(key, ctx);
|
||||
}
|
||||
|
||||
fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x3_peephole_substitute_alternate_syntax.exit_catch_clause(catch, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DeadCodeElimination {
|
||||
x1_peephole_fold_constants: PeepholeFoldConstants,
|
||||
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode,
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@ use oxc_semantic::{ScopeTree, SemanticBuilder, SymbolTable};
|
|||
use oxc_traverse::ReusableTraverseCtx;
|
||||
|
||||
use crate::{
|
||||
ast_passes::{
|
||||
CollapsePass, DeadCodeElimination, LatePeepholeOptimizations, Normalize,
|
||||
PeepholeOptimizations, RemoveSyntax,
|
||||
},
|
||||
ast_passes::{DeadCodeElimination, Normalize, PeepholeOptimizations, RemoveSyntax},
|
||||
CompressOptions, CompressorPass,
|
||||
};
|
||||
|
||||
|
|
@ -36,10 +33,8 @@ impl<'a> Compressor<'a> {
|
|||
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
|
||||
RemoveSyntax::new(self.options).build(program, &mut ctx);
|
||||
Normalize::new().build(program, &mut ctx);
|
||||
PeepholeOptimizations::new(self.options).build(program, &mut ctx);
|
||||
CollapsePass::new().build(program, &mut ctx);
|
||||
LatePeepholeOptimizations::new(self.options).run_in_loop(program, &mut ctx);
|
||||
PeepholeOptimizations::new(self.options).build(program, &mut ctx);
|
||||
PeepholeOptimizations::new(true, self.options).run_in_loop(program, &mut ctx);
|
||||
PeepholeOptimizations::new(false, self.options).build(program, &mut ctx);
|
||||
}
|
||||
|
||||
pub fn dead_code_elimination(self, program: &mut Program<'a>) {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
|
||||
544.10 kB | 71.98 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
|
||||
|
||||
555.77 kB | 273.88 kB | 270.13 kB | 91.19 kB | 90.80 kB | d3.js
|
||||
555.77 kB | 273.85 kB | 270.13 kB | 91.17 kB | 90.80 kB | d3.js
|
||||
|
||||
1.01 MB | 460.99 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue