@@ -8962,6 +8962,7 @@ mod test_map {
89628962#[ cfg( all( test, unix) ) ]
89638963mod test_map_with_mmap_allocations {
89648964 use super :: HashMap ;
8965+ use crate :: raw:: prev_pow2;
89658966 use allocator_api2:: alloc:: { AllocError , Allocator } ;
89668967 use core:: alloc:: Layout ;
89678968 use core:: ptr:: { null_mut, NonNull } ;
@@ -9033,13 +9034,22 @@ mod test_map_with_mmap_allocations {
90339034 let alloc = MmapAllocator :: new ( ) . unwrap ( ) ;
90349035 let mut map: HashMap < usize , ( ) , _ , _ > = HashMap :: with_capacity_in ( 1 , alloc) ;
90359036
9036- let rough_bucket_size = core:: mem:: size_of :: < ( usize , ( ) , usize ) > ( ) ;
9037- let x = alloc. page_size / rough_bucket_size;
9038- // x * ¾ should account for control bytes and also load factor, at
9039- // least for realistic page sizes (4096+).
9040- let min_elems = x / 4 * 3 ;
9037+ // Size of an element plus its control byte.
9038+ let rough_bucket_size = core:: mem:: size_of :: < ( usize , ( ) ) > ( ) + 1 ;
9039+
9040+ // Accounting for some misc. padding that's likely in the allocation
9041+ // due to rounding to group width, etc.
9042+ let overhead = 3 * core:: mem:: size_of :: < usize > ( ) ;
9043+ let num_buckets = ( alloc. page_size - overhead) / rough_bucket_size;
9044+ // Buckets are always powers of 2.
9045+ let min_elems = prev_pow2 ( num_buckets) ;
9046+ // Real load-factor is 7/8, but this is a lower estimation, so 1/2.
9047+ let min_capacity = min_elems >> 1 ;
90419048 let capacity = map. capacity ( ) ;
9042- assert ! ( capacity > min_elems, "failed: {capacity} > {min_elems}" ) ;
9049+ assert ! (
9050+ capacity >= min_capacity,
9051+ "failed: {capacity} >= {min_capacity}"
9052+ ) ;
90439053
90449054 // Fill it up.
90459055 for i in 0 ..capacity {
0 commit comments