On Thu, 4 Mar 2021 17:20:40 GMT, Claes Redestad <[email protected]> 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.
Looks good.
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..
-------------
Marked as reviewed by rriggs (Reviewer).
PR: https://git.openjdk.java.net/jdk/pull/2830