Skip to content

Commit

Permalink
fix test accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi committed May 18, 2024
1 parent 89f6d1f commit 15fcd44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8962,6 +8962,7 @@ mod test_map {
#[cfg(all(test, unix))]
mod test_map_with_mmap_allocations {
use super::HashMap;
use crate::raw::prev_pow2;
use allocator_api2::alloc::{AllocError, Allocator};
use core::alloc::Layout;
use core::ptr::{null_mut, NonNull};
Expand Down Expand Up @@ -9033,13 +9034,22 @@ mod test_map_with_mmap_allocations {
let alloc = MmapAllocator::new().unwrap();
let mut map: HashMap<usize, (), _, _> = HashMap::with_capacity_in(1, alloc);

let rough_bucket_size = core::mem::size_of::<(usize, (), usize)>();
let x = alloc.page_size / rough_bucket_size;
// x * ¾ should account for control bytes and also load factor, at
// least for realistic page sizes (4096+).
let min_elems = x / 4 * 3;
// Size of an element plus its control byte.
let rough_bucket_size = core::mem::size_of::<(usize, ())>() + 1;

// Accounting for some misc. padding that's likely in the allocation
// due to rounding to group width, etc.
let overhead = 3 * core::mem::size_of::<usize>();
let num_buckets = (alloc.page_size - overhead) / rough_bucket_size;
// Buckets are always powers of 2.
let min_elems = prev_pow2(num_buckets);
// Real load-factor is 7/8, but this is a lower estimation, so 1/2.
let min_capacity = min_elems >> 1;
let capacity = map.capacity();
assert!(capacity > min_elems, "failed: {capacity} > {min_elems}");
assert!(
capacity >= min_capacity,
"failed: {capacity} >= {min_capacity}"
);

// Fill it up.
for i in 0..capacity {
Expand Down
2 changes: 1 addition & 1 deletion src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ impl RawTableInner {

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

0 comments on commit 15fcd44

Please sign in to comment.