Optimize JVM array hashing without reflection#3312
Merged
Conversation
Motivation: HashCode.hash iterates JVM arrays through java.lang.reflect.Array, adding reflective per-element access and boxing in a shared utility. Modification: Dispatch all nine JVM array kinds to typed while loops. Add a directional test that uses the original reflective algorithm as the parity oracle across primitive, reference, empty, null-element, and nested arrays. Add a JMH benchmark for primitive and reference arrays. Result: Hash values remain compatible while JDK 17 JMH measurements improve by roughly 39x to 135x for the measured 16- and 256-element arrays. Tests: - JDK 17 Scala 2.13 HashCodeSpec: 3 tests passed - JDK 17 Scala 3.3.8 HashCodeSpec: 3 tests passed - JDK 17 JMH: Int[16] 824.277 to 6.106 ns/op; Int[256] 12749.433 to 183.033 ns/op; AnyRef[16] 635.492 to 13.945 ns/op; AnyRef[256] 9491.604 to 240.387 ns/op - sbt +headerCheckAll and checkCodeStyle passed - scalafmt --list --mode diff-ref=origin/main passed - git diff --check passed - qodercli formal stdout review found no must-fix findings References: Refs #3300
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.
Motivation
HashCode.hashiterates JVM arrays throughjava.lang.reflect.Array, adding reflective per-element access and boxing in a shared utility. A focused JDK 17 JMH comparison showed this path is substantially slower than typed array loops.Modification
whileloops.java.lang.reflect.Arrayalgorithm as the parity oracle for primitive, reference, empty, null-element, and nested arrays.Result
Hash values remain compatible while the measured array paths improve by roughly 39x to 135x. JDK 17, 2 forks, 5 warmup iterations, 8 measurement iterations, 250 ms each:
mainInt[16]Int[256]AnyRef[16]AnyRef[256]Tests
sbt "actor-tests / Test / testOnly org.apache.pekko.util.HashCodeSpec"- passed, 3 testssbt "++3.3.8" "actor-tests / Test / testOnly org.apache.pekko.util.HashCodeSpec"- passed, 3 testssbt "bench-jmh/Jmh/run -wi 5 -i 8 -f 2 -w 250ms -r 250ms -t 1 -p size=16,256 .*HashCodeArrayBenchmark.*"- passed; results abovesbt +headerCheckAll- passedsbt checkCodeStyle- passedscalafmt --list --mode diff-ref=origin/main- passedgit diff --check- passedqodercliformal stdout review - no must-fix findingsReferences
Refs #3300