Strings Just Got Faster
a year ago
- #Java
- #JDK25
- #Performance
- JDK 25 improves String performance by making String::hashCode mostly constant foldable.
- Immutable Maps with String keys see significant performance improvements, up to 8x faster.
- The improvement leverages the @Stable annotation on String.hash to enable constant folding.
- Constant folding allows direct invocation of native calls like malloc() without intermediate steps.
- A corner case exists where Strings with a hash code of zero (e.g., empty string) do not benefit from this optimization.
- Future JEP 502: Stable Values (Preview) may extend similar benefits to user code.