Strings Just Got Faster
Published on: 2025-07-26 12:33:45
In JDK 25, we improved the performance of the class String in such a way that the String::hashCode function is mostly constant foldable. For example, if you use Strings as keys in a static unmodifiable Map , you will likely see significant performance improvements.
Example
Here is a relatively advanced example where we maintain an immutable Map of native calls, its keys are the name of the method call and the values are a MethodHandle that can be used to invoke the associated system call:
// Set up an immutable Map of system calls static final Map < String , MethodHandle > SYSTEM_CALLS = Map . of ( “ malloc ” , linker . downcallHandle ( mallocSymbol , … ), “ free ” , linker . downcallHandle ( freeSymbol … ), ...); … // Allocate a memory region of 16 bytes long address = SYSTEM_CALLS . get ( “ malloc ” ). invokeExact ( 16L ); … // Free the memory region SYSTEM_CALLS . get ( “ free ” ). invokeExact ( address );
The method linker.downcallHandle(…) takes a symbol and additional paramet
... Read full article.