about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMike Hommey <mh@glandium.org>2020-11-18 12:57:40 +0900
committerMike Hommey <mh@glandium.org>2020-12-08 13:05:34 +0900
commit76bd14548910a13049b59b14e09a80cfd3a29b77 (patch)
treef1445fe09c640abf23df7892d5d1c78ad948bcc7
parent87776d7d5322422e5239e153e793b687f7f9c379 (diff)
downloadrust-76bd14548910a13049b59b14e09a80cfd3a29b77.tar.gz
rust-76bd14548910a13049b59b14e09a80cfd3a29b77.zip
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.
-rw-r--r--library/alloc/src/raw_vec.rs1
-rw-r--r--library/alloc/src/vec.rs5
2 files changed, 5 insertions, 1 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs
index 522c5bcf5af..fdf6884794a 100644
--- a/library/alloc/src/raw_vec.rs
+++ b/library/alloc/src/raw_vec.rs
@@ -471,6 +471,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
 // 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)>,
diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs
index 2c8bc3d53ef..62398b82607 100644
--- a/library/alloc/src/vec.rs
+++ b/library/alloc/src/vec.rs
@@ -2103,7 +2103,10 @@ where
     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);