JDK 25: String Performance Boost via Hash Code Constant Folding
JDK 25 significantly improves the performance of the String class by enabling constant folding of the String::hashCode function. This leads to substantial speedups, especially when using Strings as keys in static, immutable Maps, such as in a scenario where MethodHandles for native calls are accessed via String keys. Benchmarks show over an 8x improvement. This optimization is achieved by marking the internal String.hash field with the @Stable annotation. This allows the JVM to cache and reuse the hash code, avoiding recomputation for non-zero hash codes. While a zero hash code can hinder this optimization, a future fix is anticipated.
Read more