RFR: 8263038: Optimize String.format for simple specifiers

Claes Redestad redestad at openjdk.java.net
Fri Mar 5 17:06:16 UTC 2021


On Fri, 5 Mar 2021 16:36:03 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> This patch optimizes String.format expressions that uses trivial specifiers. In the JDK, the most common variation of String.format is a variation of format("foo: %s", s), which gets a significant speed-up from this.
>> 
>> Various other cleanups and minor improvements reduce overhead further and ensure we get a small gain also for more complex format strings.
>
> src/java.base/share/classes/java/util/Formatter.java line 3017:
> 
>> 3015:                 s = ((Character)arg).toString();
>> 3016:             } else if (arg instanceof Byte) {
>> 3017:                 byte i = (Byte) arg;
> 
> Can the pattern matching for instanceof be used here to remove explicit casts.  (Supported in JDK 16) 
>  s = null;
>             if (arg instanceof Character c) {
>                 s = c.toString();
>             } else if (arg instanceof Byte i) {
>                 if (Character.isValidCodePoint(i))
>                     s = new String(Character.toChars(i));
>                 else
>                     throw new IllegalFormatCodePointException(i);
>             } else if (arg instanceof Short i) {
>                 if (Character.isValidCodePoint(i))
>                     s = new String(Character.toChars(i));
>                 else
>                     throw new IllegalFormatCodePointException(i);
>             } ```
> etc..

I did think about it, but it seemed to stray a bit too far from the intent of this enhancement.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2830


More information about the core-libs-dev mailing list