-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
base: master
Are you sure you want to change the base?
Conversation
👋 Welcome back swen! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
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
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% |
/reviewers 2 |
@AlanBateman |
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
…hars_202504 # Conflicts: # src/java.base/share/classes/java/lang/StringUTF16.java
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
Issue
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