-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
8336831: Optimize StringConcatHelper.simpleConcat #20253
Conversation
👋 Welcome back wenshao! A progress list of the required criteria for merging this PR into |
@wenshao 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:
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 6 new commits pushed to the
Please see this link for an up-to-date comparison between the source branch of this pull request and the 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, @cl4es, @RogerRiggs) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
Below are the performance numbers running on a MacBook M1 Pro, which is 8.71% faster -Benchmark (intValue) Mode Cnt Score Error Units
-StringConcat.concatMethodConstString 4711 avgt 15 5.499 ? 0.046 ns/op
+Benchmark (intValue) Mode Cnt Score Error Units
+StringConcat.concatMethodConstString 4711 avgt 15 5.058 ? 0.012 ns/op +8.71% |
Webrevs
|
Also beware of #19927: it simplifies the prepend part. How does that patch run in your benchmark? |
I have changed it to a shared newArray, including out of bounds checking, The performance improvement is still the same as before. -Benchmark (intValue) Mode Cnt Score Error Units (master)
-StringConcat.concatMethodConstString 4711 avgt 15 5.437 ? 0.073 ns/op
+Benchmark (intValue) Mode Cnt Score Error Units (38a6f5602f0f1f4a9eec4b7f0ac1b5c4e661f667)
+StringConcat.concatMethodConstString 4711 avgt 15 5.027 ? 0.080 ns/op +8.15% |
The performance comparison I made is based on the latest master branch (c25c4896ad9ef031e3cddec493aef66ff87c48a7), after #19927. |
As annoying and risky as this first appeared, this patch is actually in quite good shape: The usage of Another question for you: can you check out the original form over here too? 781fb29#diff-f8131d8a48caf7cfc908417fad241393c2ef55408172e9a28dcaa14b1d73e1fbL1968-L1981
|
@liach is this what you want to change it to? class String {
public String concat(String str) {
if (str.isEmpty()) {
return this;
}
return StringConcatHelper.doConcat(this, str);
}
}
class StringConcatHelper {
@ForceInline
static String simpleConcat(Object first, Object second) {
String s1 = stringOf(first);
String s2 = stringOf(second);
if (s1.isEmpty()) {
// newly created string required, see JLS 15.18.1
return new String(s2);
}
if (s2.isEmpty()) {
// newly created string required, see JLS 15.18.1
return new String(s1);
}
return doConcat(s1, s2);
}
@ForceInline
static String doConcat(String s1, String s2) {
byte coder = (byte) (s1.coder() | s2.coder());
int newLength = (s1.length() + s2.length()) << coder;
byte[] buf = newArray(newLength);
s1.getBytes(buf, 0, coder);
s2.getBytes(buf, s1.length(), coder);
return new String(buf, coder);
}
} |
Yep. I think this looks cleaner and avoids redundant checks on the |
This patch looks ideal to me; but other reviewers may have different opinions on this review. I am ready to answer their questions for you. I will approve your patch once you fixed the null and empty check in |
Co-authored-by: Chen Liang <liach@openjdk.org>
Below are the performance numbers of the latest version running on a MacBook M1 Pro, which is 9.19% faster -Benchmark (intValue) Mode Cnt Score Error Units (base c25c4896ad9ef031e3cddec493aef66ff87c48a7)
-StringConcat.concatMethodConstString 4711 avgt 15 5.440 ? 0.075 ns/op
+Benchmark (intValue) Mode Cnt Score Error Units (current 69901157e4dae9018abd727956f60fd11b8fa252)
+StringConcat.concatMethodConstString 4711 avgt 15 4.982 ? 0.019 ns/op +9.19% |
I'll take a look at this next week. |
/reviewers 2 reviewer |
@RogerRiggs |
FWIW one of the ideas when implementing I'm also experimenting with replacing the MH-based strategy with spinning hidden, shareable classes instead. In that case we might actually be better off getting rid of the |
@liach Can you give a sample code? |
@wenshao Answered at #20273 (comment) since this question is more closely related to that patch. |
…at_202407 # Conflicts: # src/java.base/share/classes/java/lang/StringConcatHelper.java
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.
This change seems reasonable, and we will need the newArray(int)
method in future work. This patch dials back on the idea that simpleConcat
is an "explainer" for what StringConcatFactory
is doing, but as it was already imprecise in that regard we should favor simplicity and performance here.
@wenshao this pull request can not be integrated into git checkout optim_simple_concat_202407
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push |
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.
Merge conflict fix looks good.
Please hold off a few hours before integration: I will see if @RogerRiggs has any concerns, and I will run the CI tests for your patch again. |
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.
looks good.
Tier 1-3 tests pass. |
/integrate |
/sponsor |
Going to push as commit 476d2ae.
Your commit was automatically rebased without conflicts. |
Currently simpleConcat is implemented using mix and prepend, but in this simple scenario, it can be implemented in a simpler way and can improve performance.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/20253/head:pull/20253
$ git checkout pull/20253
Update a local copy of the PR:
$ git checkout pull/20253
$ git pull https://git.openjdk.org/jdk.git pull/20253/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 20253
View PR using the GUI difftool:
$ git pr show -t 20253
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20253.diff
Webrev
Link to Webrev Comment