Skip to content

8355177: Speed up StringBuilder::append(char[]) via UTF16::compress & Unsafe::copyMemory #24773

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 6 commits into
base: master
Choose a base branch
from

Conversation

wenshao
Copy link
Contributor

@wenshao wenshao commented Apr 21, 2025

In BufferedReader.readLine and other similar scenarios, we need to use StringBuilder.append(char[]) to build the string.

For these scenarios, we can use the intrinsic method StringUTF16.compress and Unsafe.copyMemory instead of the character copy of the char-by-char loop to improve the speed.


Progress

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

Issue

  • JDK-8355177: Speed up StringBuilder::append(char[]) via UTF16::compress & Unsafe::copyMemory (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24773

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 21, 2025

👋 Welcome back swen! 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.

@wenshao wenshao changed the title Using UTF16::compress & Unsafe::copyMemory to speed up StringBuilder UTF16 append(char[]) Speed up StringBuilder UTF16 append(char[]) via Using UTF16::compress & Unsafe::copyMemory Apr 21, 2025
@openjdk
Copy link

openjdk bot commented Apr 21, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@wenshao wenshao changed the title Speed up StringBuilder UTF16 append(char[]) via Using UTF16::compress & Unsafe::copyMemory Speed up StringBuilder UTF16 append(char[]) via UTF16::compress & Unsafe::copyMemory Apr 21, 2025
@wenshao wenshao changed the title Speed up StringBuilder UTF16 append(char[]) via UTF16::compress & Unsafe::copyMemory Speed up StringBuilder::append(char[]) via UTF16::compress & Unsafe::copyMemory Apr 21, 2025
@openjdk
Copy link

openjdk bot commented Apr 21, 2025

@wenshao 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 Apr 21, 2025
@wenshao
Copy link
Contributor Author

wenshao commented Apr 21, 2025

Below are the performance numbers on a MacBookPro M1 Max, showing a 112% speed increase when coder = LATIN1 and a 44.19% speed increase when coder = UTF16.

shell

git remote add wenshao git@github.com:wenshao/jdk.git
git fetch wenshao

# basline
git clone ee356d3af177877e2702db08a3b55d170a7e454c
make test TEST="micro:java.lang.StringBuilders.appendWithCharArray"

# current
git clone cd5137097b4a7be370cf60df9aa5000203ea99c0
make test TEST="micro:java.lang.StringBuilders.appendWithCharArray"

Performance Numbrers

-Benchmark                                 Mode  Cnt   Score   Error  Units (baseline cd5137097b4)
-StringBuilders.appendWithCharArrayLatin1  avgt   15  33.039 ± 0.059  ns/op
-StringBuilders.appendWithCharArrayUTF16   avgt   15  19.977 ± 0.054  ns/op

+Benchmark                                 Mode  Cnt   Score   Error  Units (current ee356d3af17)
+StringBuilders.appendWithCharArrayLatin1  avgt   15  15.533 ± 0.039  ns/op +112.70%
+StringBuilders.appendWithCharArrayUTF16   avgt   15  13.868 ± 0.053  ns/op +44.19%

@wenshao wenshao changed the title Speed up StringBuilder::append(char[]) via UTF16::compress & Unsafe::copyMemory 8355177: Speed up StringBuilder::append(char[]) via UTF16::compress & Unsafe::copyMemory Apr 21, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 21, 2025
@mlbridge
Copy link

mlbridge bot commented Apr 21, 2025

Webrevs

@AlanBateman
Copy link
Contributor

/reviewers 2

@openjdk
Copy link

openjdk bot commented Apr 21, 2025

@AlanBateman
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

@liach
Copy link
Member

liach commented Apr 21, 2025

This might be helpful combined with #21730.

@@ -1767,6 +1768,9 @@ private final void appendChars(char[] s, int off, int end) {
int count = this.count;
if (isLatin1()) {
byte[] val = this.value;
int compressed = StringUTF16.compress(s, off, val, count, end - off);
count += compressed;
off += compressed;
Copy link
Member

Choose a reason for hiding this comment

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

Should we update this.count eagerly after compression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the StringLatin1.canEncode(c) branch of the original code, this.count is not updated either, and the behavior is the same as before.

wenshao added 2 commits April 21, 2025 23:45
…hars_202504

# Conflicts:
#	src/java.base/share/classes/java/lang/StringUTF16.java
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 rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants