diff options
| author | bors <bors@rust-lang.org> | 2021-11-30 13:52:38 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-11-30 13:52:38 +0000 |
| commit | 207c80f105282245d93024c95ac408c622f70114 (patch) | |
| tree | e05bfbe67697f3d435a8243c1c1921151ecd6bf3 | |
| parent | 1c0287830e0fb3c4007afea2819ba03766da6e9c (diff) | |
| parent | 6a83352aa3cb80eb83cb57aba30aad32d3881af7 (diff) | |
Auto merge of #91352 - nnethercote:RawVec-reserve_for_push, r=dtolnay
Introduce `RawVec::reserve_for_push`. If `Vec::push`'s capacity check fails it calls `RawVec::reserve`, which then also does a capacity check. This commit introduces `reserve_for_push` which skips the redundant capacity check, for some slight compile time speed-ups. I tried lots of minor variations on this, e.g. different inlining attributes. This was the best one I could find. r? `@ghost`
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 8 | ||||
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 2 |
2 files changed, 9 insertions, 1 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 4ab38c802a1..3d38e73305a 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -289,6 +289,14 @@ 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. + #[cfg(not(no_global_oom_handling))] + #[inline(never)] + pub fn reserve_for_push(&mut self, len: usize) { + handle_reserve(self.grow_amortized(len, 1)); + } + /// The same as `reserve`, but returns on errors instead of panicking or aborting. pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> { if self.needs_to_grow(len, additional) { diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 85759917765..88bde6e8ce4 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1726,7 +1726,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.reserve(1); + self.buf.reserve_for_push(self.len); } unsafe { let end = self.as_mut_ptr().add(self.len); |
