5656#[ cfg( not( no_global_oom_handling) ) ]
5757use core:: cmp;
5858use core:: cmp:: Ordering ;
59- use core:: fmt;
6059use core:: hash:: { Hash , Hasher } ;
6160#[ cfg( not( no_global_oom_handling) ) ]
6261use core:: iter;
@@ -65,6 +64,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
6564use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
6665use core:: ptr:: { self , NonNull } ;
6766use core:: slice:: { self , SliceIndex } ;
67+ use core:: { fmt, intrinsics} ;
6868
6969use crate :: alloc:: { Allocator , Global } ;
7070use crate :: borrow:: { Cow , ToOwned } ;
@@ -907,8 +907,13 @@ impl<T, A: Allocator> Vec<T, A> {
907907 /// ```
908908 #[ cfg( not( no_global_oom_handling) ) ]
909909 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
910+ #[ inline]
910911 pub fn reserve ( & mut self , additional : usize ) {
911912 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+ }
912917 }
913918
914919 /// Reserves the minimum capacity for at least `additional` more elements to
@@ -937,8 +942,13 @@ impl<T, A: Allocator> Vec<T, A> {
937942 /// ```
938943 #[ cfg( not( no_global_oom_handling) ) ]
939944 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
945+ #[ inline]
940946 pub fn reserve_exact ( & mut self , additional : usize ) {
941947 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+ }
942952 }
943953
944954 /// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -974,8 +984,14 @@ impl<T, A: Allocator> Vec<T, A> {
974984 /// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
975985 /// ```
976986 #[ stable( feature = "try_reserve" , since = "1.57.0" ) ]
987+ #[ inline]
977988 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 ( ( ) )
979995 }
980996
981997 /// Tries to reserve the minimum capacity for at least `additional`
@@ -1017,8 +1033,14 @@ impl<T, A: Allocator> Vec<T, A> {
10171033 /// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
10181034 /// ```
10191035 #[ stable( feature = "try_reserve" , since = "1.57.0" ) ]
1036+ #[ inline]
10201037 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 ( ( ) )
10221044 }
10231045
10241046 /// Shrinks the capacity of the vector as much as possible.
0 commit comments