Today I've found that an actual capacity of a HashSet is more than it was requested via HashSet::with_capacity. Is it something expected? If yes, would you mind to explain why it is so?
with_capacity only guarantees that you will get at least the specified capacity. I think you want a with_capacity_exact, but this doesn't exist right now. on any collection To work around this you would use reserve_exact like this,
let mut set = HashSet::<usize>::new();
set.reserve_exact(exact_capacity);
But unfortunately, it look like HashSet doesn't have reserve_exact for some reason.
The raw capacity (buckets) is always a power of 2, so it's a simple bitmask to get the raw index from a hash value. Combining this with a maximum load factor of 7/8 means the available capacity is 7*2n, except for sizes smaller than 8.