diff options
| author | bors <bors@rust-lang.org> | 2022-01-20 06:50:14 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-01-20 06:50:14 +0000 |
| commit | 74fbbefea8d13683cca5eee62e4740706cb3144a (patch) | |
| tree | eb044d094bfe63e07842d79263d06a64a46eb806 /library/alloc/src | |
| parent | 237949b6c8e5b387e0a9d1e7a22e0397c95fe488 (diff) | |
| parent | ea570c689cf5dfb07f39853cc26701f382dff9a6 (diff) | |
| download | rust-74fbbefea8d13683cca5eee62e4740706cb3144a.tar.gz rust-74fbbefea8d13683cca5eee62e4740706cb3144a.zip | |
Auto merge of #92138 - AngelicosPhosphoros:try_smarter_vec_from_iter_48994_2, r=Mark-Simulacrum
Improve capacity estimation in Vec::from_iter Iterates on the attempt made in #53086. Closes #48994
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/vec/spec_from_iter_nested.rs | 10 |
2 files changed, 9 insertions, 3 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 3806bc546ee..8fa0242ca9a 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -108,7 +108,7 @@ impl<T, A: Allocator> RawVec<T, A> { // to round up a request of less than 8 bytes to at least 8 bytes. // - 4 if elements are moderate-sized (<= 1 KiB). // - 1 otherwise, to avoid wasting too much space for very short Vecs. - const MIN_NON_ZERO_CAP: usize = if mem::size_of::<T>() == 1 { + pub(crate) const MIN_NON_ZERO_CAP: usize = if mem::size_of::<T>() == 1 { 8 } else if mem::size_of::<T>() <= 1024 { 4 diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index 948cf044197..f915ebb86e5 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -1,5 +1,8 @@ +use core::cmp; use core::iter::TrustedLen; -use core::ptr::{self}; +use core::ptr; + +use crate::raw_vec::RawVec; use super::{SpecExtend, Vec}; @@ -24,8 +27,11 @@ where None => return Vec::new(), Some(element) => { let (lower, _) = iterator.size_hint(); - let mut vector = Vec::with_capacity(lower.saturating_add(1)); + let initial_capacity = + cmp::max(RawVec::<T>::MIN_NON_ZERO_CAP, lower.saturating_add(1)); + let mut vector = Vec::with_capacity(initial_capacity); unsafe { + // SAFETY: We requested capacity at least 1 ptr::write(vector.as_mut_ptr(), element); vector.set_len(1); } |
