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 Nov 2, 2023
1 parent 608e968 commit 029fbd6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
18 changes: 14 additions & 4 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/codegen/vec-reserve-extend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// compile-flags: -O

#![crate_type = "lib"]

// CHECK-LABEL: @should_reserve_once
#[no_mangle]
pub fn should_reserve_once(v: &mut Vec<u8>) {
// CHECK: tail call void @llvm.assume
v.try_reserve(3).unwrap();
// CHECK-NOT: call {{.*}}reserve
// CHECK-NOT: call {{.*}}do_reserve_and_handle
// CHECK-NOT: call {{.*}}__rust_alloc(
v.extend([1, 2, 3]);
}
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:545:5:
capacity overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 comments on commit 029fbd6

Please sign in to comment.