Skip to content

8364320: String encodeUTF8 latin1 with negatives #26597

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bokken
Copy link
Contributor

@bokken bokken commented Aug 1, 2025

As suggested on mailing list, when encoding latin1 bytes to utf-8, we can count the leading positive bytes and in the case where there is a negative, we can copy all the positive values to the target byte[] prior to processing the remaining data 1 byte at a time.

https://mail.openjdk.org/pipermail/core-libs-dev/2025-July/149417.html


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8364320: String encodeUTF8 latin1 with negatives (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26597/head:pull/26597
$ git checkout pull/26597

Update a local copy of the PR:
$ git checkout pull/26597
$ git pull https://git.openjdk.org/jdk.git pull/26597/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26597

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26597.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 1, 2025

👋 Welcome back bokken! 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 Aug 1, 2025

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

8364320: String encodeUTF8 latin1 with negatives

Reviewed-by: liach

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 163 new commits pushed to 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 (@liach) 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 rfr Pull request is ready for review label Aug 1, 2025
@openjdk
Copy link

openjdk bot commented Aug 1, 2025

@bokken 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 Aug 1, 2025
@bokken
Copy link
Contributor Author

bokken commented Aug 1, 2025

Benchmark on win64

Baseline:

Benchmark                           (charsetName)  Mode  Cnt      Score     Error  Units
StringEncode.encodeAllMixed                 UTF-8  avgt   10  20067.519 ┬▒ 528.152  ns/op
StringEncode.encodeAsciiLong                UTF-8  avgt   10  12115.389 ┬▒ 307.491  ns/op
StringEncode.encodeAsciiShort               UTF-8  avgt   10     70.098 ┬▒   1.696  ns/op
StringEncode.encodeLatin1LongEnd            UTF-8  avgt   10   1974.391 ┬▒ 162.405  ns/op
StringEncode.encodeLatin1LongOnly           UTF-8  avgt   10    270.097 ┬▒  13.840  ns/op
StringEncode.encodeLatin1LongStart          UTF-8  avgt   10   1876.366 ┬▒  51.971  ns/op
StringEncode.encodeLatin1Mixed              UTF-8  avgt   10   4973.070 ┬▒ 130.426  ns/op
StringEncode.encodeLatin1Short              UTF-8  avgt   10     96.227 ┬▒   2.816  ns/op
StringEncode.encodeShortMixed               UTF-8  avgt   10    360.586 ┬▒   8.691  ns/op
StringEncode.encodeUTF16LongEnd             UTF-8  avgt   10   1534.748 ┬▒  34.584  ns/op
StringEncode.encodeUTF16LongOnly            UTF-8  avgt   10    528.919 ┬▒  15.143  ns/op
StringEncode.encodeUTF16LongStart           UTF-8  avgt   10   2275.117 ┬▒  50.152  ns/op
StringEncode.encodeUTF16Mixed               UTF-8  avgt   10   4398.943 ┬▒ 116.607  ns/op
StringEncode.encodeUTF16Short               UTF-8  avgt   10    152.219 ┬▒   8.677  ns/op

Patch:

Benchmark                           (charsetName)  Mode  Cnt      Score     Error  Units
StringEncode.encodeAllMixed                 UTF-8  avgt   10  18876.056 ┬▒ 330.644  ns/op
StringEncode.encodeAsciiLong                UTF-8  avgt   10  12040.590 ┬▒ 165.905  ns/op
StringEncode.encodeAsciiShort               UTF-8  avgt   10     69.895 ┬▒   0.318  ns/op
StringEncode.encodeLatin1LongEnd            UTF-8  avgt   10    574.455 ┬▒  14.769  ns/op
StringEncode.encodeLatin1LongOnly           UTF-8  avgt   10    284.553 ┬▒   1.886  ns/op
StringEncode.encodeLatin1LongStart          UTF-8  avgt   10   2230.789 ┬▒  11.043  ns/op
StringEncode.encodeLatin1Mixed              UTF-8  avgt   10   3278.998 ┬▒  96.779  ns/op
StringEncode.encodeLatin1Short              UTF-8  avgt   10     99.332 ┬▒   1.977  ns/op
StringEncode.encodeShortMixed               UTF-8  avgt   10    378.183 ┬▒  17.504  ns/op
StringEncode.encodeUTF16LongEnd             UTF-8  avgt   10   1531.960 ┬▒  19.300  ns/op
StringEncode.encodeUTF16LongOnly            UTF-8  avgt   10    563.810 ┬▒   4.811  ns/op
StringEncode.encodeUTF16LongStart           UTF-8  avgt   10   2270.970 ┬▒  28.495  ns/op
StringEncode.encodeUTF16Mixed               UTF-8  avgt   10   4403.824 ┬▒  60.338  ns/op
StringEncode.encodeUTF16Short               UTF-8  avgt   10    158.600 ┬▒   2.044  ns/op

@mlbridge
Copy link

mlbridge bot commented Aug 1, 2025

Webrevs

byte[] dst = StringUTF16.newBytesFor(val.length);
for (byte c : val) {
if (positives > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if for very short strings, the overhead of the setup for arraycopy is worth it.
It might be interesting to raise the test here to 4 or 8 for the array copy and otherwise start the loop at 0 and see if the performance difference is detectable.

@liach
Copy link
Member

liach commented Aug 1, 2025

@bokken FYI to make JMH comparison easier, you can let JMH generate JSON reports, upload them to github gists, and use https://jmh.morethan.io/ to compare the two results from two gists.

Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

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

On second thought, even though Roger's concern is valid, I think the current shape is the best for this code. All usages of JavaLangAccess.inflateBytesToChars already exhibit the same pattern as the code here. A separate heuristic for this method would increase our maintenance cost for uncertain gains.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 11, 2025
@bokken
Copy link
Contributor Author

bokken commented Aug 11, 2025

@liach / @RogerRiggs I have been experimenting locally with other options which are a bit more complex:
https://github.com/bokken/jdk/commits/string-utf8-mincopylength/
This seems like maybe a decent balance of complexity vs gain: bokken@ee9d9e3

I am continuing to evaluate various options.

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 ready Pull request is ready to be integrated rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants