Skip to content

Commit 7d8efd8

Browse files
committed
perf(transformer/refresh): use take/take_in instead of drain (#10656)
Calling `take` is cheaper than calling `drain`. These cases don't need to reuse the `Vec` after it drained, so we can replace them with `take`, which gives a tiny performance improvement. <img width="633" alt="image" src="https://github.com/user-attachments/assets/a2a65957-09a0-47a5-9b9f-eaec576220c1" />
1 parent a3ada34 commit 7d8efd8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

crates/oxc_transformer/src/jsx/refresh.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::hash_map::Entry;
1+
use std::{collections::hash_map::Entry, mem};
22

33
use base64::{
44
encoded_len as base64_encoded_len,
@@ -139,8 +139,8 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
139139

140140
impl<'a> Traverse<'a> for ReactRefresh<'a, '_> {
141141
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
142-
let mut new_statements = ctx.ast.vec_with_capacity(program.body.len());
143-
for mut statement in program.body.drain(..) {
142+
let mut new_statements = ctx.ast.vec_with_capacity(program.body.len() * 2);
143+
for mut statement in program.body.take_in(ctx.ast.allocator) {
144144
let next_statement = self.process_statement(&mut statement, ctx);
145145
new_statements.push(statement);
146146
if let Some(assignment_expression) = next_statement {
@@ -156,8 +156,8 @@ impl<'a> Traverse<'a> for ReactRefresh<'a, '_> {
156156
}
157157

158158
let mut variable_declarator_items = ctx.ast.vec_with_capacity(self.registrations.len());
159-
let mut new_statements = ctx.ast.vec_with_capacity(self.registrations.len() + 1);
160-
for (binding, persistent_id) in self.registrations.drain(..) {
159+
let mut new_statements = ctx.ast.vec_with_capacity(self.registrations.len());
160+
for (binding, persistent_id) in mem::take(&mut self.registrations) {
161161
variable_declarator_items.push(ctx.ast.variable_declarator(
162162
SPAN,
163163
VariableDeclarationKind::Var,

0 commit comments

Comments
 (0)