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 20, 2023
1 parent f31316f commit b90a3af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 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 @@ -292,6 +292,13 @@ impl<T, A: Allocator> RawVec<T, A> {
if self.needs_to_grow(len, additional) {
do_reserve_and_handle(self, len, additional);
}

// SAFETY: The call to `do_reserve_and_handle` ensured this
// (or it panicked) and thus the addition cannot overflow.
unsafe {
// Inform the optimizer that the reservation has succeeded or wasn't needed
core::intrinsics::assume(!self.needs_to_grow(len, additional));
}
}

/// A specialized version of `reserve()` used only by the hot and
Expand All @@ -305,10 +312,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 +
Expand Down Expand Up @@ -339,7 +349,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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/hygiene/panic-location.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
thread 'main' panicked at library/alloc/src/raw_vec.rs:535:5:
thread 'main' panicked at library/alloc/src/raw_vec.rs:552:5:
capacity overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 comments on commit b90a3af

Please sign in to comment.