Shrink RubyHashLinkedBuckets#9381
Merged
Merged
Conversation
This generation count does not appear to actually be doing anything useful, only being incremented on rehash and only used to follow a slightly different path for clearing the Hash instance. Removing it does not appear to affect functionality, and it is a 32-bit integer increasing the size of every linked-bucket Hash object in the system. This patch removes it.
At one point we thought we might want to switch Hash from using MRI/CRuby semantics to using "Javasoft" semantics, but we never ended up using that logic and have been paying for an extra "threshold" field this entire time. This patch removes all of the "Javasoft" logic along with the "threshold" field that was not actually being used.
iteratorCount is only used to determine whether there's iteration happening at the time we want to rehash. Dropping this to 16 bits still allows for up to 64k concurrent iterators before we might wrap around to zero improperly; this is extremely unlikely. This patch also switches to VarHandle for atomic updates and removes the synchronized logic that never was used anyway (because almost nothing prevents us from creating the old atomic updater or the new VarHandle updater).
The "head" entry here only needs to track nextAdded and prevAdded, with all other state only used after walking past this "head". We can shrink the "head" entry by moving prevAdded and nextAdded into a smaller base object, reducing the retained size of all hash instances by 32 bytes.
headius
force-pushed
the
no_hash_generation
branch
from
May 6, 2026 17:15
93e2727 to
ef5520f
Compare
headius
marked this pull request as ready for review
May 6, 2026 17:25
Member
Author
|
Two additional changes were attempted but did not produce expected results:
At this point the remaining tricks to shrink the implementation are less productive than introducing a new space-efficient implementation (see #9394). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This started out as an experiment to remove the "generation" count that appeared to only be used for some rehashing heuristics, and expanded to an overall reduction of Hash size (based on the RubyHashLinkedBuckets implementation).
Changes
These changes reduce the retained size of an empty hash from 160 bytes (in 10.1.0.0) to 136 bytes.
On generation count
This generation count did not appear to actually be doing anything useful, only being incremented on rehash and only used to follow a slightly different path for clearing the Hash instance. Removing it does not appear to affect functionality, and it was a 32-bit integer increasing the size of every linked-bucket Hash object in the system.
It is a relic of old ported code from CRuby and I'm not sure we need it now (or perhaps ever needed it).