Skip to content

Commit

Permalink
feat(minifier): fold if(x)return;y -> if(!x)y (#8226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 3, 2025
1 parent 2f9fab9 commit 2041477
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
58 changes: 55 additions & 3 deletions crates/oxc_minifier/src/ast_passes/minimize_exit_points.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_semantic::ScopeFlags;
use oxc_span::{GetSpan, SPAN};
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::CompressorPass;
Expand All @@ -14,20 +16,56 @@ pub struct MinimizeExitPoints {

impl<'a> CompressorPass<'a> for MinimizeExitPoints {
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = true;
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
}
}

impl<'a> Traverse<'a> for MinimizeExitPoints {
fn exit_statements(&mut self, _stmts: &mut Vec<'a, Statement<'a>>, _ctx: &mut TraverseCtx<'a>) {
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.fold_if_return(stmts, ctx);
}
}

impl MinimizeExitPoints {
impl<'a> MinimizeExitPoints {
pub fn new() -> Self {
Self { changed: false }
}

// `if(x)return;foo` -> `if(!x)foo;`
fn fold_if_return(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
if stmts.len() <= 1 {
return;
}
let Some(index) = stmts.iter().position(|stmt| {
if let Statement::IfStatement(if_stmt) = stmt {
if if_stmt.alternate.is_none()
&& matches!(
if_stmt.consequent.get_one_child(),
Some(Statement::ReturnStatement(s)) if s.argument.is_none()
)
{
return true;
}
}
false
}) else {
return;
};
let Some(stmts_rest) = stmts.get_mut(index + 1..) else { return };
let body = ctx.ast.vec_from_iter(stmts_rest.iter_mut().map(|s| ctx.ast.move_statement(s)));
let Statement::IfStatement(if_stmt) = &mut stmts[index] else { unreachable!() };
let scope_id = ctx.create_child_scope_of_current(ScopeFlags::empty());
let argument = ctx.ast.move_expression(&mut if_stmt.test);
if_stmt.test =
ctx.ast.expression_unary(argument.span(), UnaryOperator::LogicalNot, argument);
if_stmt.alternate = None;
if_stmt.consequent = Statement::BlockStatement(
ctx.ast.alloc_block_statement_with_scope_id(SPAN, body, scope_id),
);
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
self.changed = true;
}
}

#[cfg(test)]
Expand All @@ -46,6 +84,20 @@ mod test {
fold(source_text, source_text);
}

#[test]
fn simple() {
fold(
"function foo() { if (foo) return; bar; quaz; }",
"function foo() { if (!foo) { bar; quaz; } }",
);
fold(
"function foo() { x; if (foo) return; bar; quaz; }",
"function foo() { x; if (!foo) { bar; quaz; } }",
);
fold_same("function foo() { if (foo) return }");
fold_same("function foo() { if (foo) return bar; baz }");
}

#[test]
#[ignore]
fn test_break_optimization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ impl<'a> PeepholeMinimizeConditions {
{
if then_if_stmt.alternate.is_none() {
let and_left = ctx.ast.move_expression(&mut if_stmt.test);
let Some(then_if_stmt) = if_stmt.consequent.get_one_child_mut() else {
unreachable!()
};
let Statement::IfStatement(mut then_if_stmt) =
ctx.ast.move_statement(&mut if_stmt.consequent)
ctx.ast.move_statement(then_if_stmt)
else {
unreachable!()
};
Expand Down
11 changes: 10 additions & 1 deletion crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn cjs() {
});",
);
// @babel/types/lib/index.js
test_same(
test(
r#"Object.keys(_index6).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Expand All @@ -42,6 +42,15 @@ fn cjs() {
}
});
});"#,
"
Object.keys(_index6).forEach(function(key) {
!(key === 'default' || key === '__esModule') && !Object.prototype.hasOwnProperty.call(_exportNames, key) && (key in exports && exports[key] === _index6[key] || Object.defineProperty(exports, key, {
enumerable: !0,
get: function() {
return _index6[key];
}
}));
});"
);
}

Expand Down
22 changes: 11 additions & 11 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
| Oxc | ESBuild | Oxc | ESBuild |
Original | minified | minified | gzip | gzip | Fixture
-------------------------------------------------------------------------------------
72.14 kB | 23.71 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js
72.14 kB | 23.69 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js

173.90 kB | 59.90 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js
173.90 kB | 59.87 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js

287.63 kB | 90.37 kB | 90.07 kB | 32.10 kB | 31.95 kB | jquery.js
287.63 kB | 90.27 kB | 90.07 kB | 32.09 kB | 31.95 kB | jquery.js

342.15 kB | 118.47 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js
342.15 kB | 118.25 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js

544.10 kB | 71.84 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
544.10 kB | 71.81 kB | 72.48 kB | 26.18 kB | 26.20 kB | lodash.js

555.77 kB | 273.58 kB | 270.13 kB | 90.98 kB | 90.80 kB | d3.js
555.77 kB | 273.25 kB | 270.13 kB | 90.99 kB | 90.80 kB | d3.js

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

1.25 MB | 653.16 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js
1.25 MB | 652.71 kB | 646.76 kB | 163.55 kB | 163.73 kB | three.js

2.14 MB | 726.69 kB | 724.14 kB | 180.25 kB | 181.07 kB | victory.js
2.14 MB | 726.39 kB | 724.14 kB | 180.23 kB | 181.07 kB | victory.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 | 492.95 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 492.88 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.02 kB | 915.50 kB | typescript.js
10.95 MB | 3.50 MB | 3.49 MB | 910.01 kB | 915.50 kB | typescript.js

2 changes: 1 addition & 1 deletion tasks/minsize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn minify(source_text: &str, source_type: SourceType) -> String {
.build(symbols, scopes, &mut program);
let ret = Minifier::new(MinifierOptions::default()).build(&allocator, &mut program);
CodeGenerator::new()
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.with_options(CodegenOptions { minify: true, comments: false, ..CodegenOptions::default() })
.with_mangler(ret.mangler)
.build(&program)
.code
Expand Down

0 comments on commit 2041477

Please sign in to comment.