about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-30 00:29:24 +0000
committerbors <bors@rust-lang.org>2024-03-30 00:29:24 +0000
commit877d36b1928b5a4f7d193517b48290ecbe404d71 (patch)
tree55673ca0ab72dc8ff30e308b1d937b7b3e477cf7
parenta3cfa031fa7726a957d73e6cad5744eb9706f56d (diff)
parent4500c83c62e9fe90b9807006bcd809af1ec940b3 (diff)
downloadrust-877d36b1928b5a4f7d193517b48290ecbe404d71.tar.gz
rust-877d36b1928b5a4f7d193517b48290ecbe404d71.zip
Auto merge of #122976 - caibear:optimize_reserve_for_push, r=cuviper
Remove len argument from RawVec::reserve_for_push

Removes `RawVec::reserve_for_push`'s `len` argument since it's always the same as capacity.
Also makes `Vec::insert` use `RawVec::reserve_for_push`.
-rw-r--r--library/alloc/src/collections/vec_deque/mod.rs2
-rw-r--r--library/alloc/src/raw_vec.rs8
-rw-r--r--library/alloc/src/vec/mod.rs4
-rw-r--r--tests/codegen/vec_pop_push_noop.rs6
4 files changed, 10 insertions, 10 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs
index 41adc2e79dc..5ff7f184a6d 100644
--- a/library/alloc/src/collections/vec_deque/mod.rs
+++ b/library/alloc/src/collections/vec_deque/mod.rs
@@ -2087,7 +2087,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
         // buffer without it being full emerge
         debug_assert!(self.is_full());
         let old_cap = self.capacity();
-        self.buf.reserve_for_push(old_cap);
+        self.buf.grow_one();
         unsafe {
             self.handle_capacity_increase(old_cap);
         }
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs
index 175e23b543c..0883080d735 100644
--- a/library/alloc/src/raw_vec.rs
+++ b/library/alloc/src/raw_vec.rs
@@ -345,12 +345,12 @@ impl<T, A: Allocator> RawVec<T, A> {
         }
     }
 
-    /// A specialized version of `reserve()` used only by the hot and
-    /// oft-instantiated `Vec::push()`, which does its own capacity check.
+    /// A specialized version of `self.reserve(len, 1)` which requires the
+    /// caller to ensure `len == self.capacity()`.
     #[cfg(not(no_global_oom_handling))]
     #[inline(never)]
-    pub fn reserve_for_push(&mut self, len: usize) {
-        if let Err(err) = self.grow_amortized(len, 1) {
+    pub fn grow_one(&mut self) {
+        if let Err(err) = self.grow_amortized(self.cap.0, 1) {
             handle_error(err);
         }
     }
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs
index 8ca8046dac5..7e3463bc082 100644
--- a/library/alloc/src/vec/mod.rs
+++ b/library/alloc/src/vec/mod.rs
@@ -1547,7 +1547,7 @@ impl<T, A: Allocator> Vec<T, A> {
 
         // space for the new element
         if len == self.buf.capacity() {
-            self.reserve(1);
+            self.buf.grow_one();
         }
 
         unsafe {
@@ -1967,7 +1967,7 @@ impl<T, A: Allocator> Vec<T, A> {
         // This will panic or abort if we would allocate > isize::MAX bytes
         // or if the length increment would overflow for zero-sized types.
         if self.len == self.buf.capacity() {
-            self.buf.reserve_for_push(self.len);
+            self.buf.grow_one();
         }
         unsafe {
             let end = self.as_mut_ptr().add(self.len);
diff --git a/tests/codegen/vec_pop_push_noop.rs b/tests/codegen/vec_pop_push_noop.rs
index 83765d10854..4d76c24a9d9 100644
--- a/tests/codegen/vec_pop_push_noop.rs
+++ b/tests/codegen/vec_pop_push_noop.rs
@@ -5,10 +5,10 @@
 #[no_mangle]
 // CHECK-LABEL: @noop(
 pub fn noop(v: &mut Vec<u8>) {
-    // CHECK-NOT: reserve_for_push
+    // CHECK-NOT: grow_one
     // CHECK-NOT: call
     // CHECK: tail call void @llvm.assume
-    // CHECK-NOT: reserve_for_push
+    // CHECK-NOT: grow_one
     // CHECK-NOT: call
     // CHECK: ret
     if let Some(x) = v.pop() {
@@ -19,6 +19,6 @@ pub fn noop(v: &mut Vec<u8>) {
 #[no_mangle]
 // CHECK-LABEL: @push_byte(
 pub fn push_byte(v: &mut Vec<u8>) {
-    // CHECK: call {{.*}}reserve_for_push
+    // CHECK: call {{.*}}grow_one
     v.push(3);
 }