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

perf(minifier): only run optimizations on local changes #8644

Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ oxc_syntax = { workspace = true }
oxc_traverse = { workspace = true }

cow-utils = { workspace = true }
rustc-hash = { workspace = true }

[dev-dependencies]
oxc_parser = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'a> PeepholeOptimizations {
}

*stmts = new_stmts;
self.changed = true;
self.mark_current_function_as_changed();
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'a> PeepholeOptimizations {
}
}

if self.changed {
if self.is_current_function_changed() {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
}
}
Expand All @@ -147,7 +147,7 @@ impl<'a> PeepholeOptimizations {
if let Statement::ExpressionStatement(expr_stmt) = ctx.ast.move_statement(&mut stmts[i]) {
if let Statement::ForStatement(for_stmt) = &mut stmts[i + 1] {
for_stmt.init = Some(ForStatementInit::from(expr_stmt.unbox().expression));
self.changed = true;
self.mark_current_function_as_changed();
};
}
}
Expand All @@ -163,11 +163,11 @@ impl<'a> PeepholeOptimizations {
match for_stmt.init.as_mut() {
Some(ForStatementInit::VariableDeclaration(for_var)) => {
for_var.declarations.splice(0..0, var.unbox().declarations);
self.changed = true;
self.mark_current_function_as_changed();
}
None => {
for_stmt.init = Some(ForStatementInit::VariableDeclaration(var));
self.changed = true;
self.mark_current_function_as_changed();
}
_ => {
unreachable!()
Expand Down Expand Up @@ -209,7 +209,7 @@ impl<'a> PeepholeOptimizations {
_ => unreachable!(),
};
*left = ForStatementLeft::VariableDeclaration(var);
self.changed = true;
self.mark_current_function_as_changed();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a> PeepholeOptimizations {
*expr = MemberExpression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, e.optional),
);
self.changed = true;
self.mark_current_function_as_changed();
return;
}
let v = s.value.as_str();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/peephole/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a, 'b> PeepholeOptimizations {
_ => None,
} {
*expr = folded_expr;
self.changed = true;
self.mark_current_function_as_changed();
};
}

Expand Down Expand Up @@ -706,7 +706,7 @@ impl<'a, 'b> PeepholeOptimizations {
true
});
if e.properties.len() != len {
self.changed = true;
self.mark_current_function_as_changed();
}
None
}
Expand Down
32 changes: 20 additions & 12 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ impl<'a> PeepholeOptimizations {
stmts: &mut oxc_allocator::Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
self.try_replace_if(stmts, ctx);
let changed = self.changed;
while self.changed {
self.changed = false;
self.try_replace_if(stmts, ctx);
let mut changed = false;
let mut changed2 = false;
Self::try_replace_if(stmts, &mut changed2, ctx);
while changed2 {
changed2 = false;
Self::try_replace_if(stmts, &mut changed2, ctx);
if stmts.iter().any(|stmt| matches!(stmt, Statement::EmptyStatement(_))) {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
}
changed = changed2;
}
if changed {
self.mark_current_function_as_changed();
}
self.changed = self.changed || changed;
}

pub fn minimize_conditions_exit_statement(
Expand Down Expand Up @@ -64,7 +68,7 @@ impl<'a> PeepholeOptimizations {
_ => None,
} {
*stmt = folded_stmt;
self.changed = true;
self.mark_current_function_as_changed();
};
}

Expand All @@ -87,7 +91,7 @@ impl<'a> PeepholeOptimizations {
}
}
if changed {
self.changed = true;
self.mark_current_function_as_changed();
} else {
break;
}
Expand All @@ -99,7 +103,7 @@ impl<'a> PeepholeOptimizations {
_ => None,
} {
*expr = folded_expr;
self.changed = true;
self.mark_current_function_as_changed();
};
}

Expand Down Expand Up @@ -236,7 +240,11 @@ impl<'a> PeepholeOptimizations {
None
}

fn try_replace_if(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn try_replace_if(
stmts: &mut Vec<'a, Statement<'a>>,
changed: &mut bool,
ctx: &mut TraverseCtx<'a>,
) {
for i in 0..stmts.len() {
let Statement::IfStatement(if_stmt) = &stmts[i] else {
continue;
Expand Down Expand Up @@ -268,15 +276,15 @@ impl<'a> PeepholeOptimizations {
alternate,
);
stmts[i] = ctx.ast.statement_return(if_stmt.span, Some(argument));
self.changed = true;
*changed = true;
break;
} else if else_branch.is_some() && Self::statement_must_exit_parent(then_branch) {
let Statement::IfStatement(if_stmt) = &mut stmts[i] else {
unreachable!();
};
let else_branch = if_stmt.alternate.take().unwrap();
stmts.insert(i + 1, else_branch);
self.changed = true;
*changed = true;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/peephole/minimize_exit_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a> PeepholeOptimizations {
if let Some(last) = stmts.last() {
if matches!(last, Statement::ReturnStatement(ret) if ret.argument.is_none()) {
stmts.pop();
self.changed = true;
self.mark_current_function_as_changed();
}
}
}
Expand Down
Loading
Loading