56
56
#[ cfg( not( no_global_oom_handling) ) ]
57
57
use core:: cmp;
58
58
use core:: cmp:: Ordering ;
59
- use core:: fmt;
60
59
use core:: hash:: { Hash , Hasher } ;
61
60
#[ cfg( not( no_global_oom_handling) ) ]
62
61
use core:: iter;
@@ -65,6 +64,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
65
64
use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
66
65
use core:: ptr:: { self , NonNull } ;
67
66
use core:: slice:: { self , SliceIndex } ;
67
+ use core:: { fmt, intrinsics} ;
68
68
69
69
use crate :: alloc:: { Allocator , Global } ;
70
70
use crate :: borrow:: { Cow , ToOwned } ;
@@ -907,8 +907,13 @@ impl<T, A: Allocator> Vec<T, A> {
907
907
/// ```
908
908
#[ cfg( not( no_global_oom_handling) ) ]
909
909
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
910
+ #[ inline]
910
911
pub fn reserve ( & mut self , additional : usize ) {
911
912
self . buf . reserve ( self . len , additional) ;
913
+ unsafe {
914
+ // Inform the optimizer that the reservation has succeeded or wasn't needed
915
+ intrinsics:: assume ( !self . buf . needs_to_grow ( self . len , additional) ) ;
916
+ }
912
917
}
913
918
914
919
/// Reserves the minimum capacity for at least `additional` more elements to
@@ -937,8 +942,13 @@ impl<T, A: Allocator> Vec<T, A> {
937
942
/// ```
938
943
#[ cfg( not( no_global_oom_handling) ) ]
939
944
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
945
+ #[ inline]
940
946
pub fn reserve_exact ( & mut self , additional : usize ) {
941
947
self . buf . reserve_exact ( self . len , additional) ;
948
+ unsafe {
949
+ // Inform the optimizer that the reservation has succeeded or wasn't needed
950
+ intrinsics:: assume ( !self . buf . needs_to_grow ( self . len , additional) ) ;
951
+ }
942
952
}
943
953
944
954
/// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -974,8 +984,14 @@ impl<T, A: Allocator> Vec<T, A> {
974
984
/// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
975
985
/// ```
976
986
#[ stable( feature = "try_reserve" , since = "1.57.0" ) ]
987
+ #[ inline]
977
988
pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
978
- self . buf . try_reserve ( self . len , additional)
989
+ self . buf . try_reserve ( self . len , additional) ?;
990
+ unsafe {
991
+ // Inform the optimizer that the reservation has succeeded or wasn't needed
992
+ intrinsics:: assume ( !self . buf . needs_to_grow ( self . len , additional) ) ;
993
+ }
994
+ Ok ( ( ) )
979
995
}
980
996
981
997
/// Tries to reserve the minimum capacity for at least `additional`
@@ -1017,8 +1033,14 @@ impl<T, A: Allocator> Vec<T, A> {
1017
1033
/// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
1018
1034
/// ```
1019
1035
#[ stable( feature = "try_reserve" , since = "1.57.0" ) ]
1036
+ #[ inline]
1020
1037
pub fn try_reserve_exact ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
1021
- self . buf . try_reserve_exact ( self . len , additional)
1038
+ self . buf . try_reserve_exact ( self . len , additional) ?;
1039
+ unsafe {
1040
+ // Inform the optimizer that the reservation has succeeded or wasn't needed
1041
+ intrinsics:: assume ( !self . buf . needs_to_grow ( self . len , additional) ) ;
1042
+ }
1043
+ Ok ( ( ) )
1022
1044
}
1023
1045
1024
1046
/// Shrinks the capacity of the vector as much as possible.
0 commit comments