Skip to content

Commit 15fcd44

Browse files
committed
fix test accuracy
1 parent 89f6d1f commit 15fcd44

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/map.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -8962,6 +8962,7 @@ mod test_map {
89628962
#[cfg(all(test, unix))]
89638963
mod 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 {

src/raw/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ impl RawTableInner {
17381738

17391739
/// Find the previous power of 2. If it's already a power of 2, it's unchanged.
17401740
/// Passing zero is undefined behavior.
1741-
fn prev_pow2(z: usize) -> usize {
1741+
pub(crate) fn prev_pow2(z: usize) -> usize {
17421742
let shift = mem::size_of::<usize>() * 8 - 1;
17431743
1 << (shift - (z.leading_zeros() as usize))
17441744
}

0 commit comments

Comments
 (0)