Skip to content

Commit c03a3af

Browse files
committed
Hint optimizer about reserved capacity
1 parent 9372999 commit c03a3af

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

library/alloc/src/raw_vec.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ impl<T, A: Allocator> RawVec<T, A> {
292292
if self.needs_to_grow(len, additional) {
293293
do_reserve_and_handle(self, len, additional);
294294
}
295+
296+
// SAFETY: The call to `do_reserve_and_handle` ensured this
297+
// (or it panicked) and thus the addition cannot overflow.
298+
unsafe {
299+
// Inform the optimizer that the reservation has succeeded or wasn't needed
300+
core::intrinsics::assume(!self.needs_to_grow(len, additional));
301+
}
295302
}
296303

297304
/// A specialized version of `reserve()` used only by the hot and
@@ -305,10 +312,13 @@ impl<T, A: Allocator> RawVec<T, A> {
305312
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
306313
pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
307314
if self.needs_to_grow(len, additional) {
308-
self.grow_amortized(len, additional)
309-
} else {
310-
Ok(())
315+
self.grow_amortized(len, additional)?;
316+
}
317+
unsafe {
318+
// Inform the optimizer that the reservation has succeeded or wasn't needed
319+
core::intrinsics::assume(!self.needs_to_grow(len, additional));
311320
}
321+
Ok(())
312322
}
313323

314324
/// Ensures that the buffer contains at least enough space to hold `len +
@@ -339,7 +349,14 @@ impl<T, A: Allocator> RawVec<T, A> {
339349
len: usize,
340350
additional: usize,
341351
) -> Result<(), TryReserveError> {
342-
if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) }
352+
if self.needs_to_grow(len, additional) {
353+
self.grow_exact(len, additional)?;
354+
}
355+
unsafe {
356+
// Inform the optimizer that the reservation has succeeded or wasn't needed
357+
core::intrinsics::assume(!self.needs_to_grow(len, additional));
358+
}
359+
Ok(())
343360
}
344361

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

tests/codegen/vec-reserve-extend.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @should_reserve_once
6+
#[no_mangle]
7+
pub fn should_reserve_once(v: &mut Vec<u8>) {
8+
// CHECK: tail call void @llvm.assume
9+
v.try_reserve(3).unwrap();
10+
// CHECK-NOT: call {{.*}}reserve
11+
// CHECK-NOT: call {{.*}}do_reserve_and_handle
12+
// CHECK-NOT: call {{.*}}__rust_alloc(
13+
v.extend([1, 2, 3]);
14+
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
thread 'main' panicked at library/alloc/src/raw_vec.rs:535:5:
1+
thread 'main' panicked at library/alloc/src/raw_vec.rs:552:5:
22
capacity overflow
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)