Open
Description
HashTable::with_capacity
docs say:
The hash table will be able to hold at least
capacity
elements without reallocating.
The following test shows that this statement is incorrect:
#[test]
fn foo() {
let capacity = 100;
let mut hash_table = HashTable::with_capacity(capacity);
for i in 0u64..1_000 {
dbg!(i);
assert!(hash_table.len() < capacity);
let hasher =
|_: &_| unreachable!("hasher won't be called because there is capacity left");
let eq = |j: &u64| *j == i;
let Entry::Vacant(entry) = hash_table.entry(i, eq, hasher) else {
unreachable!("actually unreachable");
};
entry.insert(i);
if hash_table.len() == capacity {
hash_table.find_entry(i, eq).unwrap().remove();
}
}
}
Expectation:
As the assert shows, every time the loop begins there is at least one capacity left. The HashTable does not need to grow and it will not call our hasher in HashTable::entry
.
Reality:
The HashTable attempts to grow and calls the hasher, triggering the unreachable. The debug prints tell us that this happens at i=112.
This is a problem because it prevents you from writing code that relies on HashTable not growing when enough capacity has been allocated. You should be able to write such code.
Metadata
Metadata
Assignees
Labels
No labels