Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(minifier): temporary remove try_fold_if_block_one and try_fold_if_one_child #7536

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn minify(
) -> String {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;
let options = MinifierOptions { mangle, compress: CompressOptions::default() };
let options = MinifierOptions { mangle, compress: CompressOptions::dead_code_elimination() };
let ret = Minifier::new(options).build(allocator, &mut program);
CodeGenerator::new()
.with_options(CodegenOptions { minify: nospace, ..CodegenOptions::default() })
Expand Down
97 changes: 2 additions & 95 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use oxc_ast::ast::*;
use oxc_ecmascript::ToBoolean;
use oxc_span::SPAN;
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::CompressorPass;
Expand Down Expand Up @@ -33,16 +31,6 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
self.changed = true;
};
}

fn exit_statement(&mut self, node: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
if let Statement::IfStatement(if_stmt) = node {
self.try_fold_if_block_one(if_stmt, ctx);
if let Some(new_stmt) = Self::try_fold_if_one_child(if_stmt, ctx) {
*node = new_stmt;
self.changed = true;
}
}
}
}

impl<'a> PeepholeMinimizeConditions {
Expand All @@ -64,89 +52,6 @@ impl<'a> PeepholeMinimizeConditions {
}
None
}

/// Duplicate logic to DCE part.
fn try_fold_if_block_one(&mut self, if_stmt: &mut IfStatement<'a>, ctx: &mut TraverseCtx<'a>) {
if let Statement::BlockStatement(block) = &mut if_stmt.consequent {
if block.body.len() == 1
&& !matches!(&block.body[0], Statement::VariableDeclaration(decl) if !decl.kind.is_var())
{
self.changed = true;
if_stmt.consequent = ctx.ast.move_statement(&mut block.body[0]);
}
}
if let Some(Statement::BlockStatement(block)) = &mut if_stmt.alternate {
if block.body.len() == 1
&& !matches!(&block.body[0], Statement::VariableDeclaration(decl) if !decl.kind.is_var())
{
self.changed = true;
if_stmt.alternate = Some(ctx.ast.move_statement(&mut block.body[0]));
}
}
}

fn try_fold_if_one_child(
if_stmt: &mut IfStatement<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Statement<'a>> {
if let Statement::ExpressionStatement(expr) = &mut if_stmt.consequent {
// The rest of things for known boolean are tasks for dce instead of here.
if_stmt
.test
.to_boolean()
.is_none()
.then(|| {
if !matches!(if_stmt.alternate, None | Some(Statement::ExpressionStatement(_)))
{
return None;
}
// Make if (x) y; => x && y;
let (reverse, mut test) = match &mut if_stmt.test {
Expression::UnaryExpression(unary) if unary.operator.is_not() => {
let arg = ctx.ast.move_expression(&mut unary.argument);
(true, arg)
}
_ => (false, ctx.ast.move_expression(&mut if_stmt.test)),
};
match &mut test {
Expression::BinaryExpression(bin) if bin.operator.is_equality() => {
if !bin.left.is_literal() && bin.right.is_literal() {
test = ctx.ast.expression_binary(
SPAN,
ctx.ast.move_expression(&mut bin.right),
bin.operator,
ctx.ast.move_expression(&mut bin.left),
);
}
}
_ => {}
}
if let Some(Statement::ExpressionStatement(alt)) = &mut if_stmt.alternate {
let left = ctx.ast.move_expression(&mut expr.expression);
let right = ctx.ast.move_expression(&mut alt.expression);
let cond = if reverse {
ctx.ast.expression_conditional(SPAN, test, right, left)
} else {
ctx.ast.expression_conditional(SPAN, test, left, right)
};
Some(ctx.ast.statement_expression(SPAN, cond))
} else if if_stmt.alternate.is_none() {
let new_expr = ctx.ast.expression_logical(
SPAN,
test,
if reverse { LogicalOperator::Or } else { LogicalOperator::And },
ctx.ast.move_expression(&mut expr.expression),
);
Some(ctx.ast.statement_expression(SPAN, new_expr))
} else {
None
}
})
.unwrap_or(None)
} else {
None
}
}
}

/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeMinimizeConditionsTest.java>
Expand Down Expand Up @@ -176,6 +81,7 @@ mod test {

/** Check that removing blocks with 1 child works */
#[test]
#[ignore]
fn test_fold_one_child_blocks() {
// late = false;
fold("function f(){if(x)a();x=3}", "function f(){x&&a();x=3}");
Expand Down Expand Up @@ -402,6 +308,7 @@ mod test {
}

#[test]
#[ignore]
fn test_not_cond() {
fold("function f(){if(!x)foo()}", "function f(){x||foo()}");
fold("function f(){if(!x)b=1}", "function f(){x||(b=1)}");
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ fn dce_if_statement() {
test("if (!false) { foo }", "foo");
test("if (!true) { foo } else { bar }", "bar");

test("if (!false && xxx) { foo }", "xxx && foo");
test("if (!false && xxx) { foo }", "if (xxx) foo");
test("if (!true && yyy) { foo } else { bar }", "bar");

test("if (true || xxx) { foo }", "foo");
test("if (false || xxx) { foo }", "xxx && foo");
test("if (false || xxx) { foo }", "if (xxx) foo");

test("if ('production' == 'production') { foo } else { bar }", "foo");
test("if ('development' == 'production') { foo } else { bar }", "bar");
Expand Down
24 changes: 12 additions & 12 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Original | Minified | esbuild | Gzip | esbuild

72.14 kB | 23.98 kB | 23.70 kB | 8.67 kB | 8.54 kB | react.development.js
72.14 kB | 24.12 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js

173.90 kB | 61.16 kB | 59.82 kB | 19.62 kB | 19.33 kB | moment.js
173.90 kB | 61.67 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js

287.63 kB | 91.70 kB | 90.07 kB | 32.34 kB | 31.95 kB | jquery.js
287.63 kB | 92.70 kB | 90.07 kB | 32.26 kB | 31.95 kB | jquery.js

342.15 kB | 120.23 kB | 118.14 kB | 44.72 kB | 44.37 kB | vue.js
342.15 kB | 121.90 kB | 118.14 kB | 44.59 kB | 44.37 kB | vue.js

544.10 kB | 73.03 kB | 72.48 kB | 26.16 kB | 26.20 kB | lodash.js
544.10 kB | 73.48 kB | 72.48 kB | 26.12 kB | 26.20 kB | lodash.js

555.77 kB | 275.23 kB | 270.13 kB | 91.33 kB | 90.80 kB | d3.js
555.77 kB | 276.48 kB | 270.13 kB | 91.15 kB | 90.80 kB | d3.js

1.01 MB | 464.89 kB | 458.89 kB | 127.04 kB | 126.71 kB | bundle.min.js
1.01 MB | 467.59 kB | 458.89 kB | 126.73 kB | 126.71 kB | bundle.min.js

1.25 MB | 660.45 kB | 646.76 kB | 164.46 kB | 163.73 kB | three.js
1.25 MB | 662.83 kB | 646.76 kB | 164.00 kB | 163.73 kB | three.js

2.14 MB | 739.98 kB | 724.14 kB | 181.63 kB | 181.07 kB | victory.js
2.14 MB | 741.55 kB | 724.14 kB | 181.45 kB | 181.07 kB | victory.js

3.20 MB | 1.02 MB | 1.01 MB | 333.04 kB | 331.56 kB | echarts.js
3.20 MB | 1.02 MB | 1.01 MB | 332.07 kB | 331.56 kB | echarts.js

6.69 MB | 2.39 MB | 2.31 MB | 496.74 kB | 488.28 kB | antd.js
6.69 MB | 2.39 MB | 2.31 MB | 496.11 kB | 488.28 kB | antd.js

10.95 MB | 3.55 MB | 3.49 MB | 913.59 kB | 915.50 kB | typescript.js
10.95 MB | 3.56 MB | 3.49 MB | 911.20 kB | 915.50 kB | typescript.js

Loading