diff options
| author | bors <bors@rust-lang.org> | 2023-11-05 00:03:41 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-05 00:03:41 +0000 |
| commit | f5ca57e153afaed818f8be88abf5ce46715c0f9a (patch) | |
| tree | 9dc9e0381a9643a643075d7160ccdda95db0c1d0 /library/alloc | |
| parent | 4b85902b438f791c5bfcb6b1c5b476d5b88e2bef (diff) | |
| parent | 029fbd67ef65dae50172678033c0762610041a38 (diff) | |
| download | rust-f5ca57e153afaed818f8be88abf5ce46715c0f9a.tar.gz rust-f5ca57e153afaed818f8be88abf5ce46715c0f9a.zip | |
Auto merge of #117503 - kornelski:hint-try-reserved, r=workingjubilee
Hint optimizer about try-reserved capacity This is #116568, but limited only to the less-common `try_reserve` functions to reduce bloat in debug binaries from debug info, while still addressing the main use-case #116570
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index bd66ad61219..817b93720ce 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -305,10 +305,13 @@ impl<T, A: Allocator> RawVec<T, A> { /// 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) { - self.grow_amortized(len, additional) - } else { - Ok(()) + self.grow_amortized(len, additional)?; + } + unsafe { + // Inform the optimizer that the reservation has succeeded or wasn't needed + core::intrinsics::assume(!self.needs_to_grow(len, additional)); } + Ok(()) } /// Ensures that the buffer contains at least enough space to hold `len + @@ -339,7 +342,14 @@ impl<T, A: Allocator> RawVec<T, A> { len: usize, additional: usize, ) -> Result<(), TryReserveError> { - if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) } + if self.needs_to_grow(len, additional) { + self.grow_exact(len, additional)?; + } + unsafe { + // Inform the optimizer that the reservation has succeeded or wasn't needed + core::intrinsics::assume(!self.needs_to_grow(len, additional)); + } + Ok(()) } /// Shrinks the buffer down to the specified capacity. If the given amount |
