Repository navigation
Spread bits in computing bucket index - #163
Conversation
Signed-off-by: Fengyun Liu <fengyun.liu.cs@gmail.com>
Signed-off-by: Fengyun Liu <fengyun.liu.cs@gmail.com>
Signed-off-by: Fengyun Liu <fengyun.liu.cs@gmail.com>
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
| val h = hash.hashCode(x) | ||
| val spread = h ^ ((h >> 16) & 0xffff) | ||
| (spread & 0x7fffffff) % cap |
There was a problem hiding this comment.
we should ensure cap is prime number
There was a problem hiding this comment.
this is more important than the hash code value
There was a problem hiding this comment.
Using prime number is a design choice, but not the only one. Here we are following the common design that use power-of-two capacity and spread higher-bits.
It's used e.g. in JDK: https://github.com/openjdk/jdk/blob/8610e806260f4eec6d98a5b418e4a1bc11a9a3cb/src/java.base/share/classes/java/util/HashMap.java#L320-L339
Spread bits in computing bucket index
Summary
This is a common optimization in power-of-two hash tables (%16, %32, etc.), to distribute hash codes that have similar lower bits but different high bits more evenly.
This is a performance improvement. The following test is an extreme case, but it does show the optimization pays off:
--jitless)4,000 distinct
Longvalues (i << 32). Median of five runs after one warm-up.Checklist
tests/pos/ortests/warn/Docs updated if the change affects user-visible behaviorA JIP is required for language changeHow to sign off commits
Use
git commit -sto add theSigned-off-byline automatically:To add a sign-off to the last commit retroactively:
To add sign-off to the last 3 commits:
Security impact
No
Compatibility impact
No