@@ -8962,6 +8962,7 @@ mod test_map {
8962
8962
#[ cfg( all( test, unix) ) ]
8963
8963
mod test_map_with_mmap_allocations {
8964
8964
use super :: HashMap ;
8965
+ use crate :: raw:: prev_pow2;
8965
8966
use allocator_api2:: alloc:: { AllocError , Allocator } ;
8966
8967
use core:: alloc:: Layout ;
8967
8968
use core:: ptr:: { null_mut, NonNull } ;
@@ -9033,13 +9034,22 @@ mod test_map_with_mmap_allocations {
9033
9034
let alloc = MmapAllocator :: new ( ) . unwrap ( ) ;
9034
9035
let mut map: HashMap < usize , ( ) , _ , _ > = HashMap :: with_capacity_in ( 1 , alloc) ;
9035
9036
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 ;
9041
9048
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
+ ) ;
9043
9053
9044
9054
// Fill it up.
9045
9055
for i in 0 ..capacity {
0 commit comments