Skip to content

Commit 876b6ea

Browse files
committed
perf(minifier): reduce allocations (#10301)
In these 3 places, we `pop` a statement from a `Vec` and insert it elsewhere. Previously we used `take_in_box` to get the statement first (which involves allocating a dummy node into arena) and then `pop`-ed and discarded the dummy. Instead do the `pop` first, to avoid the unnecessary allocation. I *assume* that compiler will see that the `unreachable!` path is genuinely unreachable because we just checked the type of the statement above, and will elide that check.
1 parent afe663b commit 876b6ea

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

crates/oxc_minifier/src/peephole/minimize_statements.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'a> PeepholeOptimizations {
329329
self.handle_for_in_statement(for_in_stmt, result, state, ctx);
330330
}
331331
Statement::ForOfStatement(for_of_stmt) => {
332-
self.handle_for_of_statement(for_of_stmt, result, state, ctx);
332+
self.handle_for_of_statement(for_of_stmt, result, state);
333333
}
334334
Statement::BlockStatement(block_stmt) => self.handle_block(result, block_stmt, state),
335335
stmt => result.push(stmt),
@@ -642,10 +642,10 @@ impl<'a> PeepholeOptimizations {
642642
}
643643
}
644644
} else if prev_var_decl.kind.is_var() {
645-
for_stmt.init = Some(ForStatementInit::VariableDeclaration(
646-
prev_var_decl.take_in_box(ctx.ast.allocator),
647-
));
648-
result.pop();
645+
let Some(Statement::VariableDeclaration(prev_var_decl)) = result.pop() else {
646+
unreachable!()
647+
};
648+
for_stmt.init = Some(ForStatementInit::VariableDeclaration(prev_var_decl));
649649
state.changed = true;
650650
}
651651
}
@@ -709,10 +709,13 @@ impl<'a> PeepholeOptimizations {
709709
&prev_var_decl_item.id.kind
710710
{
711711
if id.name == decl_id.name {
712-
for_in_stmt.left = ForStatementLeft::VariableDeclaration(
713-
prev_var_decl.take_in_box(ctx.ast.allocator),
714-
);
715-
result.pop();
712+
let Some(Statement::VariableDeclaration(prev_var_decl)) =
713+
result.pop()
714+
else {
715+
unreachable!()
716+
};
717+
for_in_stmt.left =
718+
ForStatementLeft::VariableDeclaration(prev_var_decl);
716719
state.changed = true;
717720
}
718721
}
@@ -729,7 +732,6 @@ impl<'a> PeepholeOptimizations {
729732
mut for_of_stmt: Box<'a, ForOfStatement<'a>>,
730733
result: &mut Vec<'a, Statement<'a>>,
731734
state: &mut State,
732-
ctx: Ctx<'a, '_>,
733735
) {
734736
// "var a; for (a of b) c" => "for (var a of b) c"
735737
if let Some(Statement::VariableDeclaration(prev_var_decl)) = result.last_mut() {
@@ -749,10 +751,11 @@ impl<'a> PeepholeOptimizations {
749751
&prev_var_decl_item.id.kind
750752
{
751753
if id.name == decl_id.name {
752-
for_of_stmt.left = ForStatementLeft::VariableDeclaration(
753-
prev_var_decl.take_in_box(ctx.ast.allocator),
754-
);
755-
result.pop();
754+
let Some(Statement::VariableDeclaration(prev_var_decl)) = result.pop()
755+
else {
756+
unreachable!()
757+
};
758+
for_of_stmt.left = ForStatementLeft::VariableDeclaration(prev_var_decl);
756759
state.changed = true;
757760
}
758761
}

0 commit comments

Comments
 (0)