-
Notifications
You must be signed in to change notification settings - Fork 613
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
Compact() method for maps doesn't check if a rehash is needed #1668
Comments
The only reason to call map.remove(k1);
map.remove(k2);
... // maybe a loop to remove stuff
//at the end:
map.compact(); If you're calling If a map has undergone removal, compact can not only change the size, but also remove sentinels (aka "tombstones"). This is how something is removed: this.keysValues[index] = REMOVED_KEY;
this.keysValues[index + 1] = EMPTY_VALUE; The map does keep track of |
Thank you for your swift reply! |
After investigating the this.rehash(this.smallestPowerOfTwoGreaterThan(this.size())); The Would it be possible that with the compact() call not do the extra grow from 4096 to 8192 as one could assume that after calling compact() that the map won't be modified anymore. The other option would be to initially in compact set it to 8192 (using e.g. |
Good catch. That should be fixed, and the optimization regarding compact only if size changes or there are sentinels can be applied as well. Do you still need another function that only compacts if there is a memory benefit? |
Thank you!
How would that differ from "and the optimization regarding compact only if size changes or there are sentinels can be applied as well"? Or do you mean that compact() always would try to compact() and we would have a separate isCompactable() (horrible name) or something similar? Yes, a method like that would be useful unless compact internally would call it. |
In for example LongDoubleHashMap the compact() method directly calls:
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
This rehashes also the maps in those cases where the map cannot be compacted. It would be better to first check if
this.smallestPowerOfTwoGreaterThan(this.size()) != getTableSize()
too quickly see if we can compact the map or not.The text was updated successfully, but these errors were encountered: