Skip to content

Commit

Permalink
feat(minifier): improve StatementFusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 30, 2024
1 parent d0de560 commit e1474da
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 72 deletions.
19 changes: 14 additions & 5 deletions crates/oxc_allocator/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
mem::ManuallyDrop,
ops,
ptr::NonNull,
slice::SliceIndex,
};

use allocator_api2::vec;
Expand Down Expand Up @@ -253,16 +254,24 @@ impl<'alloc, T> IntoIterator for &'alloc Vec<'alloc, T> {
}
}

impl<T> ops::Index<usize> for Vec<'_, T> {
type Output = T;
impl<T, I> ops::Index<I> for Vec<'_, T>
where
I: SliceIndex<[T]>,
{
type Output = I::Output;

fn index(&self, index: usize) -> &Self::Output {
#[inline]
fn index(&self, index: I) -> &Self::Output {
self.0.index(index)
}
}

impl<T> ops::IndexMut<usize> for Vec<'_, T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
impl<T, I> ops::IndexMut<I> for Vec<'_, T>
where
I: SliceIndex<[T]>,
{
#[inline]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
self.0.index_mut(index)
}
}
Expand Down
120 changes: 64 additions & 56 deletions crates/oxc_minifier/src/ast_passes/statement_fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,44 @@ impl<'a> StatementFusion {
}

fn fuse_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
if Self::can_fuse_into_one_statement(stmts) {
self.fuse_into_one_statement(stmts, ctx);
}
}

fn can_fuse_into_one_statement(stmts: &[Statement<'a>]) -> bool {
let len = stmts.len();

if len <= 1 {
return false;
return;
}

let mut end = None;

for i in (0..stmts.len()).rev() {
match end {
None => {
if Self::is_fusable_control_statement(&stmts[i]) {
end = Some(i);
}
}
Some(j) => {
let is_expr_stmt = matches!(&stmts[i], Statement::ExpressionStatement(_));
if i == 0 && is_expr_stmt {
Self::fuse_into_one_statement(&mut stmts[0..=j], ctx);
self.changed = true;
} else if !is_expr_stmt {
if j - i > 1 {
Self::fuse_into_one_statement(&mut stmts[i + 1..=j], ctx);
self.changed = true;
}
if Self::is_fusable_control_statement(&stmts[i]) {
end = Some(i);
} else {
end = None;
}
}
}
}
}
if stmts[0..len - 1].iter().any(|s| !matches!(s, Statement::ExpressionStatement(_))) {
return false;

if self.changed {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
}
Self::is_fusable_control_statement(&stmts[len - 1])
}

fn is_fusable_control_statement(stmt: &Statement<'a>) -> bool {
Expand All @@ -82,39 +106,28 @@ impl<'a> StatementFusion {
}
}

fn fuse_into_one_statement(
&mut self,
stmts: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
fn fuse_into_one_statement(stmts: &mut [Statement<'a>], ctx: &mut TraverseCtx<'a>) {
let mut exprs = ctx.ast.vec();

let len = stmts.len();
let mut expressions = ctx.ast.vec();

for stmt in stmts.iter_mut().take(len - 1) {
match stmt {
Statement::ExpressionStatement(expr_stmt) => {
if let Expression::SequenceExpression(sequence_expr) = &mut expr_stmt.expression
{
expressions.extend(
sequence_expr
.expressions
.iter_mut()
.map(|e| ctx.ast.move_expression(e)),
);
} else {
expressions.push(ctx.ast.move_expression(&mut expr_stmt.expression));
}
*stmt = ctx.ast.statement_empty(SPAN);

for stmt in &mut stmts[0..len - 1] {
if let Statement::ExpressionStatement(expr_stmt) = stmt {
if let Expression::SequenceExpression(sequence_expr) = &mut expr_stmt.expression {
exprs.extend(
sequence_expr.expressions.iter_mut().map(|e| ctx.ast.move_expression(e)),
);
} else {
exprs.push(ctx.ast.move_expression(&mut expr_stmt.expression));
}
_ => unreachable!(),
*stmt = ctx.ast.statement_empty(SPAN);
} else {
break;
}
}

let last = stmts.last_mut().unwrap();
Self::fuse_expression_into_control_flow_statement(last, expressions, ctx);

*stmts = ctx.ast.vec1(ctx.ast.move_statement(last));
self.changed = true;
let last = &mut stmts[len - 1];
Self::fuse_expression_into_control_flow_statement(last, exprs, ctx);
}

fn fuse_expression_into_control_flow_statement(
Expand Down Expand Up @@ -229,25 +242,21 @@ mod test {
fuse("a;b;c;if(x,y){}else{}", "if(a,b,c,x,y){}else{}");
fuse("a;b;c;if(x,y){}", "if(a,b,c,x,y){}");
fuse("a;b;c;if(x,y,z){}", "if(a,b,c,x,y,z){}");

// Can't fuse if there are statements after the IF.
fuse_same("a();if(a()){}a()");
fuse("a();if(a()){}a()", "if(a(), a()){}a()");
}

#[test]
fn fold_block_return() {
fuse("a;b;c;return x", "return a,b,c,x");
fuse("a;b;c;return x+y", "return a,b,c,x+y");

// DeadAssignmentElimination would have cleaned it up anyways.
fuse_same("a;b;c;return x;a;b;c");
fuse("a;b;c;return x;a;b;c", "return a,b,c,x;a,b,c");
}

#[test]
fn fold_block_throw() {
fuse("a;b;c;throw x", "throw a,b,c,x");
fuse("a;b;c;throw x+y", "throw a,b,c,x+y");
fuse_same("a;b;c;throw x;a;b;c");
fuse("a;b;c;throw x;a;b;c", "throw a,b,c,x;a,b,c");
}

#[test]
Expand Down Expand Up @@ -278,9 +287,9 @@ mod test {

#[test]
fn fuse_into_vanilla_for2() {
fuse_same("a;b;c;for(var d;g;){}");
fuse_same("a;b;c;for(let d;g;){}");
fuse_same("a;b;c;for(const d = 5;g;){}");
fuse("a;b;c;for(var d;g;){}", "a,b,c;for(var d;g;){}");
fuse("a;b;c;for(let d;g;){}", "a,b,c;for(let d;g;){}");
fuse("a;b;c;for(const d = 5;g;){}", "a,b,c;for(const d = 5;g;){}");
}

#[test]
Expand All @@ -298,8 +307,8 @@ mod test {
"a;b; label: { if(q) break label; bar(); }",
"label: { if(a,b,q) break label; bar(); }",
);
fuse_same("a;b;c;{var x;d;e;}");
fuse_same("a;b;c;label:{break label;d;e;}");
fuse("a;b;c;{var x;d;e;}", "a,b,c;{var x;d,e;}");
fuse("a;b;c;label:{break label;d;e;}", "a,b,c;label:{break label;d,e;}");
}

#[test]
Expand All @@ -309,7 +318,7 @@ mod test {

#[test]
fn no_fuse_into_do() {
fuse_same("a;b;c;do{}while(x)");
fuse("a;b;c;do{}while(x)", "a,b,c;do{}while(x)");
}

#[test]
Expand All @@ -324,14 +333,13 @@ mod test {
fuse_same("a; { b; function a() {} }");
fuse_same("a; { b; const otherVariable = 1; }");

// enable_normalize();
// test(
// "function f(a) { if (COND) { a; { b; let a = 1; } } }",
// "function f(a) { if (COND) { { a,b; let a$jscomp$1 = 1; } } }",
// "function f(a) { if (COND) { a; { b; let a = 1; } } }",
// "function f(a) { if (COND) { { a,b; let a$jscomp$1 = 1; } } }",
// );
// test(
// "function f(a) { if (COND) { a; { b; let otherVariable = 1; } } }",
// "function f(a) { if (COND) { { a,b; let otherVariable = 1; } } }",
// "function f(a) { if (COND) { a; { b; let otherVariable = 1; } } }",
// "function f(a) { if (COND) { { a,b; let otherVariable = 1; } } }",
// );
}

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

173.90 kB | 60.15 kB | 59.82 kB | 19.50 kB | 19.33 kB | moment.js
173.90 kB | 59.91 kB | 59.82 kB | 19.45 kB | 19.33 kB | moment.js

287.63 kB | 90.54 kB | 90.07 kB | 32.17 kB | 31.95 kB | jquery.js
287.63 kB | 90.40 kB | 90.07 kB | 32.12 kB | 31.95 kB | jquery.js

342.15 kB | 118.53 kB | 118.14 kB | 44.55 kB | 44.37 kB | vue.js
342.15 kB | 118.50 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js

544.10 kB | 71.97 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
544.10 kB | 71.85 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js

555.77 kB | 273.85 kB | 270.13 kB | 91.17 kB | 90.80 kB | d3.js
555.77 kB | 273.71 kB | 270.13 kB | 91.01 kB | 90.80 kB | d3.js

1.01 MB | 460.99 kB | 458.89 kB | 126.91 kB | 126.71 kB | bundle.min.js
1.01 MB | 460.80 kB | 458.89 kB | 126.93 kB | 126.71 kB | bundle.min.js

1.25 MB | 653.54 kB | 646.76 kB | 164.04 kB | 163.73 kB | three.js
1.25 MB | 653.32 kB | 646.76 kB | 163.59 kB | 163.73 kB | three.js

2.14 MB | 727.17 kB | 724.14 kB | 180.34 kB | 181.07 kB | victory.js
2.14 MB | 726.96 kB | 724.14 kB | 180.33 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.27 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.23 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 493.10 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 493.09 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.40 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.42 kB | 915.50 kB | typescript.js

0 comments on commit e1474da

Please sign in to comment.