Skip to content

Commit b272893

Browse files
committed
perf(mangler, minifier): initialize a Vec with a specific value using Vec::from_iter_in combined with repeat_with (#9908)
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a7c0cc8b2427a67e50259253a85d0cbd Replace `std::iter::from_fn` with `repeat_with` because it accepts a function that expects to return an `Option<T>`, but we always return a `Some`, and it doesn't satisfy the `TrustedLen` trait.
1 parent f7d078c commit b272893

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

crates/oxc_mangler/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::iter;
1+
use std::iter::{self, repeat_with};
22

33
use fixedbitset::FixedBitSet;
44
use itertools::Itertools;
@@ -373,10 +373,10 @@ impl Mangler {
373373
allocator: &'a Allocator,
374374
) -> Vec<'a, SlotFrequency<'a>> {
375375
let root_scope_id = scoping.root_scope_id();
376-
let mut frequencies = Vec::with_capacity_in(total_number_of_slots, allocator);
377-
for _ in 0..total_number_of_slots {
378-
frequencies.push(SlotFrequency::new(allocator));
379-
}
376+
let mut frequencies = Vec::from_iter_in(
377+
repeat_with(|| SlotFrequency::new(allocator)).take(total_number_of_slots),
378+
allocator,
379+
);
380380

381381
for (symbol_id, slot) in slots.iter().copied().enumerate() {
382382
let symbol_id = SymbolId::from_usize(symbol_id);

crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter::repeat_with;
2+
13
use oxc_allocator::{CloneIn, Vec};
24
use oxc_ast::{NONE, ast::*};
35
use oxc_ecmascript::constant_evaluation::DetermineValueType;
@@ -744,8 +746,8 @@ impl<'a> PeepholeOptimizations {
744746
if n.value.fract() == 0.0 {
745747
let n_int = n.value as usize;
746748
if (1..=6).contains(&n_int) {
747-
let elisions = std::iter::from_fn(|| {
748-
Some(ArrayExpressionElement::Elision(ctx.ast.elision(n.span)))
749+
let elisions = repeat_with(|| {
750+
ArrayExpressionElement::Elision(ctx.ast.elision(n.span))
749751
})
750752
.take(n_int);
751753
return Some(ctx.ast.expression_array(

0 commit comments

Comments
 (0)