diff options
| author | Ben Kimock <kimockb@gmail.com> | 2021-03-21 17:47:57 -0400 |
|---|---|---|
| committer | Ben Kimock <kimockb@gmail.com> | 2021-03-21 18:11:42 -0400 |
| commit | f5e37100d9c115ab327bafd42d0c4dad539d318f (patch) | |
| tree | 02f358eb62e9373c0f47f62ed567ca63e8e496d2 | |
| parent | f82664191d0e8764b7435b9d72eb0e366b8b1464 (diff) | |
| download | rust-f5e37100d9c115ab327bafd42d0c4dad539d318f.tar.gz rust-f5e37100d9c115ab327bafd42d0c4dad539d318f.zip | |
Mark RawVec::reserve as inline and outline the resizing logic
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 56f4ebe57f8..ff7bff7584a 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -315,8 +315,20 @@ impl<T, A: Allocator> RawVec<T, A> { /// # vector.push_all(&[1, 3, 5, 7, 9]); /// # } /// ``` + #[inline] pub fn reserve(&mut self, len: usize, additional: usize) { - handle_reserve(self.try_reserve(len, additional)); + // 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. + #[inline(never)] + fn do_reserve_and_handle<T, A: Allocator>(slf: &mut RawVec<T,A>, 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); + } } /// The same as `reserve`, but returns on errors instead of panicking or aborting. |
