-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify documentation on HashMap::capacity() #24591
Comments
Seems to me like this is more of a bug in |
This is intended behaviour. It's a lower-bound. |
@gankro maybe the wrong venue for this question, but as a user of If values are only moved when a resize happens I suppose I could just take a pointer to some value before the operation, and then locate it again and compare its address after the operation. |
@tomjakubowski I think you cannot predict whether a value will be moved, regardless of whether the hashmap is resized. In particular, we use robin-hood hashing which can move elements around (essentially as an attempt to move the most frequently accessed elements to an earlier point in the chain of hash-colliding elements). |
robin-hood doesn't do "most frequent" but rather "furthest from its ideal". But yeah all the elements are in general slowly drifting to higher addresses. You'd need a chaining hashmap to try to guarantee position properties. Even then an optimized chaining map wouldn't try to preserve that property. If you want to know if a reallocation happened just check if the capacity has increased. It's a lower-bound but it's fairly accurate. |
So, this isn't a bug then? Or should we clarify that it's a lower bound? |
Hmm... I thought I specifically worded the docs to make this more clear (or at least, more debateable). Yes clarifying that for HashMap it is a lower bound seems appropriate. Although it might not even be that if we decide to allow reallocs if degenerate collisions are detected... |
The documentation for
HashMap::capacity()
says:However, a
HashMap
will sometimes store more elements than its capacity indicates. See this example (Playpen link):The text was updated successfully, but these errors were encountered: