Skip to content

Commit 128562c

Browse files
committed
Hint optimizer about reserved capacity
1 parent 7ed044c commit 128562c

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

library/alloc/src/raw_vec.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ impl<T, A: Allocator> RawVec<T, A> {
291291

292292
if self.needs_to_grow(len, additional) {
293293
do_reserve_and_handle(self, len, additional);
294+
if self.needs_to_grow(len, additional) {
295+
unsafe { core::hint::unreachable_unchecked(); }
296+
}
294297
}
295298
}
296299

@@ -305,10 +308,12 @@ impl<T, A: Allocator> RawVec<T, A> {
305308
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
306309
pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
307310
if self.needs_to_grow(len, additional) {
308-
self.grow_amortized(len, additional)
309-
} else {
310-
Ok(())
311+
self.grow_amortized(len, additional)?;
312+
if self.needs_to_grow(len, additional) {
313+
unsafe { core::hint::unreachable_unchecked(); }
314+
}
311315
}
316+
Ok(())
312317
}
313318

314319
/// Ensures that the buffer contains at least enough space to hold `len +
@@ -339,7 +344,13 @@ impl<T, A: Allocator> RawVec<T, A> {
339344
len: usize,
340345
additional: usize,
341346
) -> Result<(), TryReserveError> {
342-
if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) }
347+
if self.needs_to_grow(len, additional) {
348+
self.grow_exact(len, additional)?;
349+
if self.needs_to_grow(len, additional) {
350+
unsafe { core::hint::unreachable_unchecked(); }
351+
}
352+
}
353+
Ok(())
343354
}
344355

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

0 commit comments

Comments
 (0)