On Wed, 10 Nov 2021 09:22:32 GMT, Сергей Цыпанов <d...@openjdk.java.net> wrote:
> Looking into File.pathSeparator I've found out that currently we initialize > them as > > public static final String separator = "" + separatorChar; > > Which after compilation turns into > > NEW java/lang/StringBuilder > DUP > INVOKESPECIAL java/lang/StringBuilder.<init> ()V > LDC "" > INVOKEVIRTUAL java/lang/StringBuilder.append > (Ljava/lang/String;)Ljava/lang/StringBuilder; > GETSTATIC java/io/File.pathSeparatorChar : C > INVOKEVIRTUAL java/lang/StringBuilder.append (C)Ljava/lang/StringBuilder; > INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; > PUTSTATIC java/io/File.pathSeparator : Ljava/lang/String; > > This can be simplified to > > public static final String separator = String.valueOf(separatorChar); > > Which is in turn complied into more compact > > GETSTATIC java/io/File.pathSeparatorChar : C > INVOKESTATIC java/lang/String.valueOf (C)Ljava/lang/String; > PUTSTATIC java/io/File.pathSeparator : Ljava/lang/String; > > The changed code is likely to slightly improve start-up time as it allocates > less and does no bound checks. it should compile into invokedynamic makeConcatWithConstants on later JDKs, but valueOf(char) is most likely going to have smaller first-invocation overhead still. ------------- PR: https://git.openjdk.java.net/jdk/pull/6326