-
Notifications
You must be signed in to change notification settings - Fork 287
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
Avoid reporting a capacity of zero when there is still space in the map. #436
base: master
Are you sure you want to change the base?
Conversation
I don't mind adding a Have you considered using |
This is intended to restore the invariant that My solution could be restricted further to target only this specific case, rather than the more general solution here, but I think it would still require code in Are you worried about the total code size, or the code size in
I don't think that helps with my specific use-case, since I was using |
This has never been something that was guaranteed. The documentation specifically says that the capacity is the number of elements that can be inserted without reallocation, and makes no mention that 0 has a special meaning.
I believe just replacing this: if guard.len() * 3 < guard.capacity() {
guard.shrink_to_fit();
} with this: guard.shrink_to(guard.len() * 3); should just work. It will also properly free the backing memory of the hashmap when |
According to the documentation it would be valid for If hashbrown has memory allocated and isn't storing any elements, then there's definitely room to insert at least one element without reallocating. Another way to avoid this would be to special-case it in
This would cause unnecessary extra resizing, and wouldn't reclaim all the memory in the "normal" case though. |
Fixes rust-lang/rust#79178
There are two changes in this PR:
Add a
max_capacity()
method. Thecapacity()
method provides a lower bound on the capacity, this complements it by providing an upper bound. There are some use-cases (such as deciding when to callshrink_to_fit()
) when using the upper bound on the capacity may be more appropriate.Recover "lost" capacity when the map is fully emptied if there are a large number of tombstones. Currently this happens when the reported capacity would be less than or equal to half the actual capacity of the map. The most important case is when the reported capacity would be zero, but we haven't deallocated the backing memory: with this change a positive capacity will be reported.