From 42f909a5bdf6a683034920783c88f0a08ba76826 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 2 Apr 2021 19:49:49 -0400 Subject: [PATCH 1/3] Revert #83357 --- library/alloc/src/raw_vec.rs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index dc02c9c883ea0..56f4ebe57f8af 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -315,24 +315,8 @@ impl RawVec { /// # vector.push_all(&[1, 3, 5, 7, 9]); /// # } /// ``` - #[inline] pub fn reserve(&mut self, len: usize, additional: usize) { - // Callers expect this function to be very cheap when there is already sufficient capacity. - // Therefore, we move all the resizing and error-handling logic from grow_amortized and - // handle_reserve behind a call, while making sure that the this function is likely to be - // inlined as just a comparison and a call if the comparison fails. - #[cold] - fn do_reserve_and_handle( - slf: &mut RawVec, - len: usize, - additional: usize, - ) { - handle_reserve(slf.grow_amortized(len, additional)); - } - - if self.needs_to_grow(len, additional) { - do_reserve_and_handle(self, len, additional); - } + handle_reserve(self.try_reserve(len, additional)); } /// The same as `reserve`, but returns on errors instead of panicking or aborting. From 0dc772ee3e3a8438ed76498eef9a5b1b641fd03d Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 2 Apr 2021 19:50:12 -0400 Subject: [PATCH 2/3] Check before calling Vec::reserve in spec_extend --- library/alloc/src/vec/spec_extend.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index e132befcfa5e2..7129c9a07a2b0 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -35,7 +35,9 @@ where ); } if let Some(additional) = high { - self.reserve(additional); + if additional > self.capacity().wrapping_sub(self.len()) { + self.reserve(additional); + } unsafe { let mut ptr = self.as_mut_ptr().add(self.len()); let mut local_len = SetLenOnDrop::new(&mut self.len); From 85bd865872927918a0d23dbf05d7f4aa862cd26c Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sat, 3 Apr 2021 18:07:21 -0400 Subject: [PATCH 3/3] Revert "Check before calling Vec::reserve in spec_extend" This reverts commit 0dc772ee3e3a8438ed76498eef9a5b1b641fd03d. --- library/alloc/src/vec/spec_extend.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 7129c9a07a2b0..e132befcfa5e2 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -35,9 +35,7 @@ where ); } if let Some(additional) = high { - if additional > self.capacity().wrapping_sub(self.len()) { - self.reserve(additional); - } + self.reserve(additional); unsafe { let mut ptr = self.as_mut_ptr().add(self.len()); let mut local_len = SetLenOnDrop::new(&mut self.len);