Skip to content

Commit

Permalink
Hint optimizer about reserved capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Oct 9, 2023
1 parent 7ed044c commit 6767186
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ impl<T, A: Allocator> RawVec<T, A> {

if self.needs_to_grow(len, additional) {
do_reserve_and_handle(self, len, additional);
if self.needs_to_grow(len, additional) {
unsafe {
core::hint::unreachable_unchecked();
}
}
}
}

Expand All @@ -305,10 +310,14 @@ 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)?;
if self.needs_to_grow(len, additional) {
unsafe {
core::hint::unreachable_unchecked();
}
}
}
Ok(())
}

/// Ensures that the buffer contains at least enough space to hold `len +
Expand Down Expand Up @@ -339,7 +348,15 @@ 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)?;
if self.needs_to_grow(len, additional) {
unsafe {
core::hint::unreachable_unchecked();
}
}
}
Ok(())
}

/// Shrinks the buffer down to the specified capacity. If the given amount
Expand Down

0 comments on commit 6767186

Please sign in to comment.