@@ -114,6 +114,19 @@ impl<T> RawVec<T, Global> {
114114}
115115
116116impl < T , A : Allocator > RawVec < T , A > {
117+ // Tiny Vecs are dumb. Skip to:
118+ // - 8 if the element size is 1, because any heap allocators is likely
119+ // to round up a request of less than 8 bytes to at least 8 bytes.
120+ // - 4 if elements are moderate-sized (<= 1 KiB).
121+ // - 1 otherwise, to avoid wasting too much space for very short Vecs.
122+ const MIN_NON_ZERO_CAP : usize = if mem:: size_of :: < T > ( ) == 1 {
123+ 8
124+ } else if mem:: size_of :: < T > ( ) <= 1024 {
125+ 4
126+ } else {
127+ 1
128+ } ;
129+
117130 /// Like `new`, but parameterized over the choice of allocator for
118131 /// the returned `RawVec`.
119132 #[ rustc_allow_const_fn_unstable( const_fn) ]
@@ -399,22 +412,7 @@ impl<T, A: Allocator> RawVec<T, A> {
399412 // This guarantees exponential growth. The doubling cannot overflow
400413 // because `cap <= isize::MAX` and the type of `cap` is `usize`.
401414 let cap = cmp:: max ( self . cap * 2 , required_cap) ;
402-
403- // Tiny Vecs are dumb. Skip to:
404- // - 8 if the element size is 1, because any heap allocators is likely
405- // to round up a request of less than 8 bytes to at least 8 bytes.
406- // - 4 if elements are moderate-sized (<= 1 KiB).
407- // - 1 otherwise, to avoid wasting too much space for very short Vecs.
408- // Note that `min_non_zero_cap` is computed statically.
409- let elem_size = mem:: size_of :: < T > ( ) ;
410- let min_non_zero_cap = if elem_size == 1 {
411- 8
412- } else if elem_size <= 1024 {
413- 4
414- } else {
415- 1
416- } ;
417- let cap = cmp:: max ( min_non_zero_cap, cap) ;
415+ let cap = cmp:: max ( Self :: MIN_NON_ZERO_CAP , cap) ;
418416
419417 let new_layout = Layout :: array :: < T > ( cap) ;
420418
0 commit comments