Skip to content

Commit da697ef

Browse files
committed
perf(transformer/minimize_statements): reduce allocations of vec
1 parent 07fcd0b commit da697ef

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

crates/oxc_minifier/src/peephole/minimize_statements.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::ControlFlow;
1+
use std::{iter, ops::ControlFlow};
22

33
use oxc_allocator::{Box, TakeIn, Vec};
44
use oxc_ast::ast::*;
@@ -39,7 +39,7 @@ impl<'a> PeepholeOptimizations {
3939
let mut result: Vec<'a, Statement<'a>> = ctx.ast.vec_with_capacity(stmts.len());
4040
let mut is_control_flow_dead = false;
4141
let mut keep_var = KeepVar::new(ctx.ast);
42-
let mut new_stmts = ctx.ast.vec_from_iter(stmts.drain(..));
42+
let mut new_stmts = stmts.take_in(ctx.ast.allocator);
4343
for i in 0..new_stmts.len() {
4444
let stmt = new_stmts[i].take_in(ctx.ast.allocator);
4545
if is_control_flow_dead
@@ -508,11 +508,12 @@ impl<'a> PeepholeOptimizations {
508508
}
509509

510510
if can_move_branch_condition_outside_scope {
511-
let mut body = ctx.ast.vec();
512-
if let Some(alternate) = if_stmt.alternate.take() {
513-
body.push(alternate);
514-
}
515-
body.extend(stmts.drain(i + 1..));
511+
let drained_stmts = stmts.drain(i + 1..);
512+
let mut body = if let Some(alternate) = if_stmt.alternate.take() {
513+
ctx.ast.vec_from_iter(iter::once(alternate).chain(drained_stmts))
514+
} else {
515+
ctx.ast.vec_from_iter(drained_stmts)
516+
};
516517

517518
self.minimize_statements(&mut body, state, ctx);
518519
let span =

0 commit comments

Comments
 (0)