Skip to content

Commit

Permalink
feat(minifier): minimize logical exprs (#8209)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 authored Jan 2, 2025
1 parent cd349a3 commit cd274ee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
61 changes: 57 additions & 4 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ 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) => Self::try_minimize_not(e, ctx),
Expression::LogicalExpression(logical_expr) => {
Self::try_minimize_logical(logical_expr, ctx)
}
Expression::ConditionalExpression(conditional_expr) => {
Self::try_minimize_conditional(conditional_expr, ctx)
}
Expand Down Expand Up @@ -257,6 +260,51 @@ impl<'a> PeepholeMinimizeConditions {
}
}

fn try_minimize_logical(
expr: &mut LogicalExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
// `a && true` -> `a`
// `a && false` -> `false`
if expr.operator == LogicalOperator::And {
if let (
Expression::Identifier(test_ident),
Expression::BooleanLiteral(consequent_lit),
) = (&expr.left, &expr.right)
{
if consequent_lit.value {
return Some(ctx.ast.move_expression(&mut expr.left));
}
if ctx.scopes().find_binding(ctx.current_scope_id(), &test_ident.name).is_some() {
return Some(ctx.ast.expression_boolean_literal(expr.span, false));
}
return None;
}
}

// `a || true` -> `true`
// `a || false` -> `a`
if expr.operator == LogicalOperator::Or {
if let (
Expression::Identifier(test_ident),
Expression::BooleanLiteral(consequent_lit),
) = (&expr.left, &expr.right)
{
if consequent_lit.value {
if ctx.scopes().find_binding(ctx.current_scope_id(), &test_ident.name).is_some()
{
return Some(ctx.ast.expression_boolean_literal(expr.span, true));
}
} else {
return Some(ctx.ast.move_expression(&mut expr.left));
}
return None;
}
}

None
}

fn try_minimize_conditional(
expr: &mut ConditionalExpression<'a>,
ctx: &mut TraverseCtx<'a>,
Expand Down Expand Up @@ -669,10 +717,15 @@ mod test {
fold("(x ? false : true) && y()", "!x && y()");
fold("(x ? true : y) && y()", "(x || y) && y()");
fold("(x ? y : false) && y()", "(x && y) && y()");
// fold("(x && true) && y()", "x && y()");
// fold("(x && false) && y()", "0&&y()");
// fold("(x || true) && y()", "1&&y()");
// fold("(x || false) && y()", "x&&y()");
fold("var x; (x && true) && y()", "var x; x && y()");
fold("var x; (x && false) && y()", "var x; false && y()");
fold("(x && true) && y()", "x && y()");
fold("(x && false) && y()", "x && false && y()");
fold("var x; (x || true) && y()", "var x; true && y()");
fold("var x; (x || false) && y()", "var x; x && y()");

fold_same("(x || true) && y()");
fold("(x || false) && y()", "x && y()");
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Original | minified | minified | gzip | gzip | Fixture

1.01 MB | 460.75 kB | 458.89 kB | 126.88 kB | 126.71 kB | bundle.min.js

1.25 MB | 653.17 kB | 646.76 kB | 163.58 kB | 163.73 kB | three.js
1.25 MB | 653.17 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js

2.14 MB | 726.71 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js
2.14 MB | 726.70 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.14 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.13 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 493.00 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 492.99 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.09 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.06 kB | 915.50 kB | typescript.js

0 comments on commit cd274ee

Please sign in to comment.