From 18d390883efc7eaf5ced77d89abc5b51c8b7f816 Mon Sep 17 00:00:00 2001 From: Cai Bear Date: Sat, 23 Mar 2024 21:27:59 -0700 Subject: [PATCH 1/4] Remove len argument from RawVec::reserve_for_push because it's always equal to capacity. Also make Vec::insert use reserve_for_push. --- library/alloc/src/collections/vec_deque/mod.rs | 5 ++--- library/alloc/src/raw_vec.rs | 4 ++-- library/alloc/src/vec/mod.rs | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 41adc2e79dc74..742daea873e85 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2086,10 +2086,9 @@ impl VecDeque { // Extend or possibly remove this assertion when valid use-cases for growing the // 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.reserve_for_push(); unsafe { - self.handle_capacity_increase(old_cap); + self.handle_capacity_increase(self.capacity()); } debug_assert!(!self.is_full()); } diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 175e23b543cc8..45258cc86ff6b 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -349,8 +349,8 @@ impl RawVec { /// oft-instantiated `Vec::push()`, which does its own capacity check. #[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 reserve_for_push(&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 8ca8046dac587..5f4c45d578dbf 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1547,7 +1547,7 @@ impl Vec { // space for the new element if len == self.buf.capacity() { - self.reserve(1); + self.buf.reserve_for_push(); } unsafe { @@ -1967,7 +1967,7 @@ impl Vec { // 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.reserve_for_push(); } unsafe { let end = self.as_mut_ptr().add(self.len); From 78dc89b0d5654bb16a63121c60198cbe24878289 Mon Sep 17 00:00:00 2001 From: Cai Bear Date: Sat, 23 Mar 2024 21:45:49 -0700 Subject: [PATCH 2/4] Fix previous. --- library/alloc/src/collections/vec_deque/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 742daea873e85..e1341c21af637 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2086,9 +2086,10 @@ impl VecDeque { // Extend or possibly remove this assertion when valid use-cases for growing the // buffer without it being full emerge debug_assert!(self.is_full()); + let old_cap = self.capacity(); self.buf.reserve_for_push(); unsafe { - self.handle_capacity_increase(self.capacity()); + self.handle_capacity_increase(old_cap); } debug_assert!(!self.is_full()); } From aba592d09c78fcf1432de24c200fffa2fb21aeb6 Mon Sep 17 00:00:00 2001 From: Cai Bear Date: Thu, 28 Mar 2024 16:38:01 -0700 Subject: [PATCH 3/4] Rename reserve_for_push to grow_one and fix comment. --- library/alloc/src/collections/vec_deque/mod.rs | 2 +- library/alloc/src/raw_vec.rs | 6 +++--- library/alloc/src/vec/mod.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index e1341c21af637..5ff7f184a6dd7 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2087,7 +2087,7 @@ impl VecDeque { // buffer without it being full emerge debug_assert!(self.is_full()); let old_cap = self.capacity(); - self.buf.reserve_for_push(); + 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 45258cc86ff6b..0883080d7357f 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -345,11 +345,11 @@ impl RawVec { } } - /// 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) { + 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 5f4c45d578dbf..7e3463bc082a1 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1547,7 +1547,7 @@ impl Vec { // space for the new element if len == self.buf.capacity() { - self.buf.reserve_for_push(); + self.buf.grow_one(); } unsafe { @@ -1967,7 +1967,7 @@ impl Vec { // 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.buf.grow_one(); } unsafe { let end = self.as_mut_ptr().add(self.len); From 4500c83c62e9fe90b9807006bcd809af1ec940b3 Mon Sep 17 00:00:00 2001 From: Cai Bear Date: Fri, 29 Mar 2024 15:37:43 -0700 Subject: [PATCH 4/4] Fix test. --- tests/codegen/vec_pop_push_noop.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/codegen/vec_pop_push_noop.rs b/tests/codegen/vec_pop_push_noop.rs index 83765d1085419..4d76c24a9d9f4 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) { - // 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) { #[no_mangle] // CHECK-LABEL: @push_byte( pub fn push_byte(v: &mut Vec) { - // CHECK: call {{.*}}reserve_for_push + // CHECK: call {{.*}}grow_one v.push(3); }