Skip to content

Commit

Permalink
refactor(minifier): cleanup peephole_minimize_conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 25, 2024
1 parent 2331ea8 commit 9658680
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
10 changes: 6 additions & 4 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ pub struct LatePeepholeOptimizations {

impl LatePeepholeOptimizations {
pub fn new() -> Self {
let in_fixed_loop = true;
Self {
x0_statement_fusion: StatementFusion::new(),
x1_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ true,
in_fixed_loop,
),
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
x5_peephole_fold_constants: PeepholeFoldConstants::new(),
Expand Down Expand Up @@ -195,10 +196,11 @@ pub struct PeepholeOptimizations {

impl PeepholeOptimizations {
pub fn new() -> Self {
let in_fixed_loop = false;
Self {
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ false,
in_fixed_loop,
),
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
Expand Down
26 changes: 15 additions & 11 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use crate::CompressorPass;
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
pub struct PeepholeMinimizeConditions {
/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
#[allow(unused)]
in_fixed_loop: bool,

pub(crate) changed: bool,
}

Expand All @@ -24,7 +28,7 @@ impl<'a> CompressorPass<'a> for PeepholeMinimizeConditions {
impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(folded_expr) = match expr {
Expression::UnaryExpression(e) if e.operator.is_not() => Self::try_minimize_not(e, ctx),
Expression::UnaryExpression(e) => Self::try_minimize_not(e, ctx),
_ => None,
} {
*expr = folded_expr;
Expand All @@ -34,23 +38,23 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
}

impl<'a> PeepholeMinimizeConditions {
pub fn new() -> Self {
Self { changed: false }
pub fn new(in_fixed_loop: bool) -> Self {
Self { in_fixed_loop, changed: false }
}

/// Try to minimize NOT nodes such as `!(x==y)`.
fn try_minimize_not(
expr: &mut UnaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
debug_assert!(expr.operator.is_not());
if let Expression::BinaryExpression(binary_expr) = &mut expr.argument {
if let Some(new_op) = binary_expr.operator.equality_inverse_operator() {
binary_expr.operator = new_op;
return Some(ctx.ast.move_expression(&mut expr.argument));
}
// TODO: tryMinimizeCondition(node.getFirstChild());
if !expr.operator.is_not() {
return None;
}
None
let Expression::BinaryExpression(binary_expr) = &mut expr.argument else { return None };
let new_op = binary_expr.operator.equality_inverse_operator()?;
binary_expr.operator = new_op;
Some(ctx.ast.move_expression(&mut expr.argument))
}
}

Expand All @@ -63,7 +67,7 @@ mod test {

fn test(source_text: &str, positive: &str) {
let allocator = Allocator::default();
let mut pass = super::PeepholeMinimizeConditions::new();
let mut pass = super::PeepholeMinimizeConditions::new(true);
tester::test(&allocator, source_text, positive, &mut pass);
}

Expand Down

0 comments on commit 9658680

Please sign in to comment.