Skip to content

Conversation

@stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Nov 10, 2021

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.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8276926: Use String.valueOf() when initializing File.separator and File.pathSeparator

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6326/head:pull/6326
$ git checkout pull/6326

Update a local copy of the PR:
$ git checkout pull/6326
$ git pull https://git.openjdk.java.net/jdk pull/6326/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 6326

View PR using the GUI difftool:
$ git pr show -t 6326

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6326.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 10, 2021

👋 Welcome back stsypanov! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 10, 2021
@openjdk
Copy link

openjdk bot commented Nov 10, 2021

@stsypanov The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Nov 10, 2021
@mlbridge
Copy link

mlbridge bot commented Nov 10, 2021

Webrevs

@mbien
Copy link
Contributor

mbien commented Nov 10, 2021

it should compile into invokedynamic makeConcatWithConstants on later JDKs, but valueOf(char) is most likely going to have smaller first-invocation overhead still.

@stsypanov
Copy link
Contributor Author

it should compile into invokedynamic makeConcatWithConstants on later JDKs

As far as I understand StringConcatFactory is not available on bootstrap stage and StringBuilder is used instead. See #3464 (comment) and #4507 (comment) (pay attention to class loading order - j.i.File is loaded before StringConcatFactory

Copy link
Member

@cl4es cl4es left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW the specific pattern of "" + foo could probably profitably translate into String.valueOf(foo) regardless of StringConcatFactory availability. (And translating String concat to use SCF in class initializers is likely a pessimization most of the time.) I'm no expert on javac but I suspect implementing (and worse: specifying) minor improvements to the translation might be more effort than it's worth, though, so point fixes like these seem fine to me.

@openjdk
Copy link

openjdk bot commented Nov 10, 2021

⚠️ @stsypanov the full name on your profile does not match the author name in this pull requests' HEAD commit. If this pull request gets integrated then the author name from this pull requests' HEAD commit will be used for the resulting commit. If you wish to push a new commit with a different author name, then please run the following commands in a local repository of your personal fork:

$ git checkout 8276926
$ git commit -c user.name='Preferred Full Name' --allow-empty -m 'Update full name'
$ git push

@openjdk
Copy link

openjdk bot commented Nov 10, 2021

@stsypanov This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8276926: Use String.valueOf() when initializing File.separator and File.pathSeparator

Reviewed-by: redestad, jlaskey

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 3 new commits pushed to the master branch:

  • a0b8445: 8276846: JDK-8273416 is incomplete for UseSSE=1
  • a3f710e: 8276215: Intrinsics matchers should handle native method flags better
  • 0f463a7: 8276845: (fs) java/nio/file/spi/SetDefaultProvider.java fails on x86_32

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@cl4es, @JimLaskey) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 10, 2021
@stsypanov
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Nov 10, 2021
@openjdk
Copy link

openjdk bot commented Nov 10, 2021

@stsypanov
Your change (at version 7b5bab3) is now ready to be sponsored by a Committer.

@cl4es
Copy link
Member

cl4es commented Nov 10, 2021

/sponsor

@openjdk
Copy link

openjdk bot commented Nov 10, 2021

Going to push as commit 0f23c6a.
Since your change was applied there have been 3 commits pushed to the master branch:

  • a0b8445: 8276846: JDK-8273416 is incomplete for UseSSE=1
  • a3f710e: 8276215: Intrinsics matchers should handle native method flags better
  • 0f463a7: 8276845: (fs) java/nio/file/spi/SetDefaultProvider.java fails on x86_32

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Nov 10, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Nov 10, 2021
@openjdk
Copy link

openjdk bot commented Nov 10, 2021

@cl4es @stsypanov Pushed as commit 0f23c6a.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@stsypanov stsypanov deleted the 8276926 branch November 10, 2021 12:49
@JimLaskey
Copy link
Member

It's unlikely that javac would do this kind of optimization (javac tends to produce code verbatim), but it might be worth filing a RFE.

@cl4es
Copy link
Member

cl4es commented Nov 10, 2021

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants