From e1474da1b59478d279d7116bfbe37d2f461e2251 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 30 Dec 2024 20:59:30 +0800 Subject: [PATCH] feat(minifier): improve `StatementFusion` --- crates/oxc_allocator/src/vec.rs | 19 ++- .../src/ast_passes/statement_fusion.rs | 120 ++++++++++-------- tasks/minsize/minsize.snap | 22 ++-- 3 files changed, 89 insertions(+), 72 deletions(-) diff --git a/crates/oxc_allocator/src/vec.rs b/crates/oxc_allocator/src/vec.rs index 4b1e22e30252de..29b0723a50d9b6 100644 --- a/crates/oxc_allocator/src/vec.rs +++ b/crates/oxc_allocator/src/vec.rs @@ -9,6 +9,7 @@ use std::{ mem::ManuallyDrop, ops, ptr::NonNull, + slice::SliceIndex, }; use allocator_api2::vec; @@ -253,16 +254,24 @@ impl<'alloc, T> IntoIterator for &'alloc Vec<'alloc, T> { } } -impl ops::Index for Vec<'_, T> { - type Output = T; +impl ops::Index 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 ops::IndexMut for Vec<'_, T> { - fn index_mut(&mut self, index: usize) -> &mut Self::Output { +impl ops::IndexMut for Vec<'_, T> +where + I: SliceIndex<[T]>, +{ + #[inline] + fn index_mut(&mut self, index: I) -> &mut Self::Output { self.0.index_mut(index) } } diff --git a/crates/oxc_minifier/src/ast_passes/statement_fusion.rs b/crates/oxc_minifier/src/ast_passes/statement_fusion.rs index 4d563d94789dc8..b4f5ec1b5d25d6 100644 --- a/crates/oxc_minifier/src/ast_passes/statement_fusion.rs +++ b/crates/oxc_minifier/src/ast_passes/statement_fusion.rs @@ -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 { @@ -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( @@ -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] @@ -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] @@ -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] @@ -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] @@ -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; } } }", // ); } diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index db227b91e5e5cb..ed7f8a7b505fe6 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -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