@@ -292,6 +292,13 @@ impl<T, A: Allocator> RawVec<T, A> {
292
292
if self . needs_to_grow ( len, additional) {
293
293
do_reserve_and_handle ( self , len, additional) ;
294
294
}
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
+ }
295
302
}
296
303
297
304
/// A specialized version of `reserve()` used only by the hot and
@@ -305,10 +312,13 @@ impl<T, A: Allocator> RawVec<T, A> {
305
312
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
306
313
pub fn try_reserve ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
307
314
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) ) ;
311
320
}
321
+ Ok ( ( ) )
312
322
}
313
323
314
324
/// Ensures that the buffer contains at least enough space to hold `len +
@@ -339,7 +349,14 @@ impl<T, A: Allocator> RawVec<T, A> {
339
349
len : usize ,
340
350
additional : usize ,
341
351
) -> 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 ( ( ) )
343
360
}
344
361
345
362
/// Shrinks the buffer down to the specified capacity. If the given amount
0 commit comments