Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8279833: Loop optimization issue in String.encodeUTF8_UTF16 #7026

Closed
wants to merge 5 commits into from

Conversation

cl4es
Copy link
Member

@cl4es cl4es commented Jan 11, 2022

In String.encodeUTF8_UTF16, making the char c local to each loop helps the performance of the method by helping C2 optimize each individual loop better.

Results on the updated micros:
19-b04:

Benchmark                          (charsetName)  Mode  Cnt     Score     Error  Units
StringEncode.encodeUTF16                   UTF-8  avgt   15   164.457 ±   7.296  ns/op
StringEncode.encodeUTF16LongEnd            UTF-8  avgt   15  2294.160 ±  40.580  ns/op
StringEncode.encodeUTF16LongStart          UTF-8  avgt   15  9128.698 ± 124.636  ns/op

Patch:

Benchmark                          (charsetName)  Mode  Cnt     Score    Error  Units
StringEncode.encodeUTF16                   UTF-8  avgt   15   131.296 ±  6.693  ns/op
StringEncode.encodeUTF16LongEnd            UTF-8  avgt   15  2282.750 ± 46.891  ns/op
StringEncode.encodeUTF16LongStart          UTF-8  avgt   15  4786.965 ± 64.896  ns/op

Going back this seem to have been an issue since this method was introduced with JEP 254 in JDK 9:

8u311:

Benchmark                          (charsetName)  Mode  Cnt     Score     Error  Units
StringEncode.encodeUTF16                   UTF-8  avgt   15   194.057 ±   3.913  ns/op
StringEncode.encodeUTF16LongEnd            UTF-8  avgt   15  3024.860 ±  88.386  ns/op
StringEncode.encodeUTF16LongStart          UTF-8  avgt   15  5282.849 ± 247.230  ns/op

9:

Benchmark                          (charsetName)  Mode  Cnt      Score     Error  Units
StringEncode.encodeUTF16                   UTF-8  avgt   15    148.481 ±   9.374  ns/op
StringEncode.encodeUTF16LongEnd            UTF-8  avgt   15   2832.754 ± 263.372  ns/op
StringEncode.encodeUTF16LongStart          UTF-8  avgt   15  10447.115 ± 408.338  ns/op

(Interestingly JDK 9 seem faster on some of the micros than later iterations, while slower on others. The main issue is the slow non-ASCII loop, where the patch speeds things up ~2x. With the patch we're significantly faster than both JDK 8 and 9 on all measures.)

There's a JIT compiler bug hiding in plain sight here where the potentially uninitialized local char c appears to mess up optimization of the second loop. I'll file a separate bug for this (a standalone reproducer should be straightforward to produce). I think this patch is reasonable to put back into the JDK while we investigate if/how C2 can better handle this kind of condition. It might also be easier to backport, depending on whether the C2 fix is trivial or not.


Progress

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

Issue

  • JDK-8279833: Loop optimization issue in String.encodeUTF8_UTF16

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 7026

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 11, 2022

👋 Welcome back redestad! 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
Copy link

openjdk bot commented Jan 11, 2022

@cl4es 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 Jan 11, 2022
@cl4es cl4es marked this pull request as ready for review January 11, 2022 12:39
@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 11, 2022
@mlbridge
Copy link

mlbridge bot commented Jan 11, 2022

Webrevs

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Looks fine, consider touchups in benchmark code.

test/micro/org/openjdk/bench/java/lang/StringEncode.java Outdated Show resolved Hide resolved
@openjdk
Copy link

openjdk bot commented Jan 11, 2022

@cl4es 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:

8279833: Loop optimization issue in String.encodeUTF8_UTF16

Reviewed-by: shade, alanb

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:

  • c08b2ac: 8225093: Special property jdk.boot.class.path.append should not default to empty string
  • 4c52eb3: 8279669: test/jdk/com/sun/jdi/TestScaffold.java uses wrong condition
  • d46410c: 8279785: JFR: 'jfr configure' should show default values

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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 11, 2022
Copy link
Contributor

@AlanBateman AlanBateman left a comment

Choose a reason for hiding this comment

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

A good find, hopefully it will be linked to the C2 issue.

This is the first change to both files in 2022 so you might have to update the copyright headers.

@cl4es
Copy link
Member Author

cl4es commented Jan 11, 2022

Thanks for reviewing, @shipilev and @AlanBateman

/integrate

@openjdk
Copy link

openjdk bot commented Jan 11, 2022

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

  • 9e02447: 8279834: Alpine Linux fails to build when --with-source-date enabled
  • 08e14c6: 8278207: G1: Tighten verification in G1ResetSkipCompactingClosure
  • c08b2ac: 8225093: Special property jdk.boot.class.path.append should not default to empty string
  • 4c52eb3: 8279669: test/jdk/com/sun/jdi/TestScaffold.java uses wrong condition
  • d46410c: 8279785: JFR: 'jfr configure' should show default values

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jan 11, 2022
@openjdk openjdk bot closed this Jan 11, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jan 11, 2022
@openjdk
Copy link

openjdk bot commented Jan 11, 2022

@cl4es Pushed as commit c3d0a94.

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

@stsypanov
Copy link
Contributor

Looks like there's one more similar case: #7034

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
4 participants