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 
> it 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.

I've filed https://bugs.openjdk.java.net/browse/JDK-8276951 

This pattern _is_ quite common - both in the OpenJDK and elsewhere.

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

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

Reply via email to