about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2024-08-03 14:48:33 -0400
committerBen Kimock <ben.kimock@redjack.com>2024-08-09 20:06:27 -0400
commitda15248d26076fe3a6482dd1e25c54c2cc1ef577 (patch)
tree5864503df993b91c3dbdfc783b7a7eae52150fa0
parentb44b367232ee0f1c8c4419ad6cae73f0815e9421 (diff)
downloadrust-da15248d26076fe3a6482dd1e25c54c2cc1ef577.tar.gz
rust-da15248d26076fe3a6482dd1e25c54c2cc1ef577.zip
Add an optimizer hint for the capacity that with_capacity_in returns
-rw-r--r--library/alloc/src/raw_vec.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs
index 151358a07c9..9c8fa7ceff4 100644
--- a/library/alloc/src/raw_vec.rs
+++ b/library/alloc/src/raw_vec.rs
@@ -423,7 +423,13 @@ impl<A: Allocator> RawVecInner<A> {
     #[inline]
     fn with_capacity_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self {
         match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) {
-            Ok(res) => res,
+            Ok(this) => {
+                unsafe {
+                    // Make it more obvious that a subsquent Vec::reserve(capacity) will not allocate.
+                    hint::assert_unchecked(!this.needs_to_grow(0, capacity, elem_layout));
+                }
+                this
+            }
             Err(err) => handle_error(err),
         }
     }