Do not inline finish_grow

We also change the specialization of `SpecFromIterNested::from_iter` for
`TrustedLen` to use `Vec::with_capacity` when the iterator has a proper size
hint, instead of `Vec::new`, avoiding calls to `grow_*` and thus
`finish_grow` in some fully inlinable cases, which would regress with
this change.

Fixes #78471.
This commit is contained in:
Mike Hommey 2020-11-18 12:57:40 +09:00
parent 87776d7d53
commit 76bd145489
2 changed files with 5 additions and 1 deletions

View File

@ -471,6 +471,7 @@ fn shrink(&mut self, amount: usize) -> Result<(), TryReserveError> {
// above `RawVec::grow_amortized` for details. (The `A` parameter isn't
// significant, because the number of different `A` types seen in practice is
// much smaller than the number of `T` types.)
#[inline(never)]
fn finish_grow<A>(
new_layout: Result<Layout, LayoutError>,
current_memory: Option<(NonNull<u8>, Layout)>,

View File

@ -2103,7 +2103,10 @@ impl<T, I> SpecFromIterNested<T, I> for Vec<T>
I: TrustedLen<Item = T>,
{
fn from_iter(iterator: I) -> Self {
let mut vector = Vec::new();
let mut vector = match iterator.size_hint() {
(_, Some(upper)) => Vec::with_capacity(upper),
_ => Vec::new(),
};
// must delegate to spec_extend() since extend() itself delegates
// to spec_from for empty Vecs
vector.spec_extend(iterator);