Reduce load factor for Hash from 5 to 2#9124
Merged
Merged
Conversation
The default load factor here was inherited from CRuby, which also used a load factor of 5 for its "chained bucket" implementation of Hash. Since then, they have moved on to a more space and cache- efficient open-addressing implementation. In jruby#9113, a user reported that large Hash initialization regressed in performance relative to JRuby 1.7 and CRuby 3.4, and this high load factor was found to be a big part of that. A load factor of five means a saturated Hash may do 5x as much work to find a given entry, because up to five chained entries must be searched in a given bucket. Reducing this to a load factor of one may be too aggressive, so we use a load factor of two here to cut the maximum number of extra hops down to one. Given 32-bit compressed OOPS and non-Liliput, we can do some math for the size increase: A 1000-element Hash at load factor = 5 consumes 800 bytes for the bucket array and about 40_000 bytes for the entry objects for a total of 40_000 bytes (not including the Hash object itself). The same Hash at load factor = 2 consumes 2000 bytes for the bucket array, an extra 1200 bytes or 2.9% size increase.
headius
marked this pull request as ready for review
December 8, 2025 20:52
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.
The default load factor here was inherited from CRuby, which also used a load factor of 5 for its "chained bucket" implementation of Hash. Since then, they have moved on to a more space and cache- efficient open-addressing implementation.
In #9113, a user reported that large Hash initialization regressed in performance relative to JRuby 1.7 and CRuby 3.4, and this high load factor was found to be a big part of that.
A load factor of five means a saturated Hash may do 5x as much work to find a given entry, because up to five chained entries must be searched in a given bucket. Reducing this to a load factor of one may be too aggressive, so we use a load factor of two here to cut the maximum number of extra hops down to one.
Given 32-bit compressed OOPS and non-Liliput, we can do some math for the size increase:
A 1000-element Hash at load factor = 5 consumes 800 bytes for the bucket array and about 40_000 bytes for the entry objects for a total of 40_000 bytes (not including the Hash object itself).
The same Hash at load factor = 2 consumes 2000 bytes for the bucket array, an extra 1200 bytes or 2.9% size increase.