perf(allocator): use lower bound of size hint when creating Vecs from an iterator (#6194)

This commit is contained in:
DonIsaac 2024-10-01 00:07:29 +00:00
parent 70d4c569d8
commit 5db9b3002c

View file

@ -95,7 +95,8 @@ impl<'alloc, T> Vec<'alloc, T> {
#[inline]
pub fn from_iter_in<I: IntoIterator<Item = T>>(iter: I, allocator: &'alloc Allocator) -> Self {
let iter = iter.into_iter();
let capacity = iter.size_hint().1.unwrap_or(0);
let hint = iter.size_hint();
let capacity = hint.1.unwrap_or(hint.0);
let mut vec = vec::Vec::with_capacity_in(capacity, &**allocator);
vec.extend(iter);
Self(vec)