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