@@ -305,10 +305,13 @@ impl<T, A: Allocator> RawVec<T, A> {
305
305
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
306
306
pub fn try_reserve ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
307
307
if self . needs_to_grow ( len, additional) {
308
- self . grow_amortized ( len, additional)
309
- } else {
310
- Ok ( ( ) )
308
+ self . grow_amortized ( len, additional) ?;
309
+ }
310
+ unsafe {
311
+ // Inform the optimizer that the reservation has succeeded or wasn't needed
312
+ core:: intrinsics:: assume ( !self . needs_to_grow ( len, additional) ) ;
311
313
}
314
+ Ok ( ( ) )
312
315
}
313
316
314
317
/// Ensures that the buffer contains at least enough space to hold `len +
@@ -339,7 +342,14 @@ impl<T, A: Allocator> RawVec<T, A> {
339
342
len : usize ,
340
343
additional : usize ,
341
344
) -> Result < ( ) , TryReserveError > {
342
- if self . needs_to_grow ( len, additional) { self . grow_exact ( len, additional) } else { Ok ( ( ) ) }
345
+ if self . needs_to_grow ( len, additional) {
346
+ self . grow_exact ( len, additional) ?;
347
+ }
348
+ unsafe {
349
+ // Inform the optimizer that the reservation has succeeded or wasn't needed
350
+ core:: intrinsics:: assume ( !self . needs_to_grow ( len, additional) ) ;
351
+ }
352
+ Ok ( ( ) )
343
353
}
344
354
345
355
/// Shrinks the buffer down to the specified capacity. If the given amount
0 commit comments