mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(minifier): improve StatementFusion (#8194)
This commit is contained in:
parent
42e211ad6b
commit
06e1780b8c
3 changed files with 92 additions and 78 deletions
|
|
@ -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<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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,20 +42,46 @@ 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;
|
||||
}
|
||||
if stmts[0..len - 1].iter().any(|s| !matches!(s, Statement::ExpressionStatement(_))) {
|
||||
return false;
|
||||
|
||||
let mut end = None;
|
||||
|
||||
// TODO: make this cleaner and faster. Find the groups of expressions i..j and fusable j+1
|
||||
// statement.
|
||||
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 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 +108,28 @@ impl<'a> StatementFusion {
|
|||
}
|
||||
}
|
||||
|
||||
fn fuse_into_one_statement(
|
||||
&mut self,
|
||||
stmts: &mut Vec<'a, Statement<'a>>,
|
||||
ctx: &mut TraverseCtx<'a>,
|
||||
) {
|
||||
let len = stmts.len();
|
||||
let mut expressions = ctx.ast.vec();
|
||||
fn fuse_into_one_statement(stmts: &mut [Statement<'a>], ctx: &mut TraverseCtx<'a>) {
|
||||
let mut exprs = 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);
|
||||
let len = stmts.len();
|
||||
|
||||
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(
|
||||
|
|
@ -171,9 +186,7 @@ fn can_merge_block_stmt(node: &BlockStatement) -> bool {
|
|||
fn can_merge_block_stmt_member(node: &Statement) -> bool {
|
||||
match node {
|
||||
Statement::LabeledStatement(label) => can_merge_block_stmt_member(&label.body),
|
||||
Statement::VariableDeclaration(var_decl) => {
|
||||
!matches!(var_decl.kind, VariableDeclarationKind::Const | VariableDeclarationKind::Let)
|
||||
}
|
||||
Statement::VariableDeclaration(var_decl) => var_decl.kind.is_var(),
|
||||
Statement::ClassDeclaration(_) | Statement::FunctionDeclaration(_) => false,
|
||||
_ => true,
|
||||
}
|
||||
|
|
@ -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]
|
||||
|
|
@ -262,9 +271,6 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn fuse_into_for_in2() {
|
||||
// This test case causes a parse warning in ES5 strict out, but is a parse error in ES6+ out.
|
||||
// setAcceptedLanguage(CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT);
|
||||
// set_expect_parse_warnings_in_this_test();
|
||||
fuse_same("a();for(var x = b() in y){}");
|
||||
}
|
||||
|
||||
|
|
@ -278,9 +284,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 +304,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 +315,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 +330,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; } } }",
|
||||
// );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.64 kB | 270.13 kB | 91.12 kB | 90.80 kB | d3.js
|
||||
555.77 kB | 273.49 kB | 270.13 kB | 90.96 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.41 kB | 646.76 kB | 164.04 kB | 163.73 kB | three.js
|
||||
1.25 MB | 653.19 kB | 646.76 kB | 163.58 kB | 163.73 kB | three.js
|
||||
|
||||
2.14 MB | 726.97 kB | 724.14 kB | 180.29 kB | 181.07 kB | victory.js
|
||||
2.14 MB | 726.75 kB | 724.14 kB | 180.29 kB | 181.07 kB | victory.js
|
||||
|
||||
3.20 MB | 1.01 MB | 1.01 MB | 332.24 kB | 331.56 kB | echarts.js
|
||||
3.20 MB | 1.01 MB | 1.01 MB | 332.21 kB | 331.56 kB | echarts.js
|
||||
|
||||
6.69 MB | 2.32 MB | 2.31 MB | 493.09 kB | 488.28 kB | antd.js
|
||||
6.69 MB | 2.32 MB | 2.31 MB | 493.08 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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue