HashMap reallocates even though capacity is not exceeded #134459
Labels
A-collections
Area: `std::collection`
A-docs
Area: Documentation for any part of the project, including the compiler, standard library, and tools
C-bug
Category: This is a bug.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
HashMap::with_capacity
's documentation states:This statement is incorrect as the following code demonstrates.
A possible output of this program is as follows. It is a possible output because the default hasher is randomly seeded. You can make it deterministic with a custom hasher.
As you can see in the output the capacity jumps from the initial 112 to 224 in iteration 253. This means the HashMap has allocated more memory. However, as the assert shows, the HashMap never held more elements than the initial capacity. This violates the guarantee documented in
with_capacity
.This is a bug in hashbrown. I've opened an issue there too rust-lang/hashbrown#602 .
The text was updated successfully, but these errors were encountered: