-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8254146: Avoid unnecessary volatile write on new AtomicBoolean(false) #510
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
Conversation
Hi @dreis2211, welcome to this OpenJDK project and thanks for contributing! We do not recognize you as Contributor and need to ensure you have signed the Oracle Contributor Agreement (OCA). If you have not signed the OCA, please follow the instructions. Please fill in your GitHub username in the "Username" field of the application. Once you have signed the OCA, please let us know by writing If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please use "Add GitHub user dreis2211" as summary for the issue. If you are contributing this work on behalf of your employer and your employer has signed the OCA, please let us know by writing |
@dreis2211 The following label will be automatically applied to this pull request:
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. |
I have contributed before the move to GitHub and signed the OCA there. Can anybody tell me what the process is for this case? |
@dreis2211 Have you tried what the bot suggested?
|
/signed |
Thank you! Please allow for up to two weeks to process your OCA, although it is usually done within one to two business days. Also, please note that pull requests that are pending an OCA check will not usually be evaluated, so your patience is appreciated! |
@jerboaa Since I have signed the OCA before the move to GitHub there is no connection to my GitHub handle, so I was confused by that if that means I have to completely sign the OCA again. |
@dreis2211 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 45 new commits pushed to the
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 (@cl4es, @RogerRiggs, @ChrisHegarty) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
/integrate |
I assume this should be coordinated with an update to the jsr166 CVS too. |
@dreis2211 |
@AlanBateman What would be the label (or workflow) for that now? |
@dreis2211 what Alan reminded us of here is that all of j.u.concurrent is maintained upstream from the OpenJDK, and that this change needs to be coordinated with the maintainers there. Either by getting an OK to go ahead and push this as-is, or by contributing this patch to the JSR166 repo directly (which will then be integrated back into the OpenJDK at some point). See http://gee.cs.oswego.edu/dl/concurrency-interest/ This isn't anything new since the GitHub migration: Various bits and pieces of the OpenJDK sources are maintained upstream, and it's not always easy to igure out exactly where or how to go about upstreaming a change. |
@cl4es Thanks. My question was more referring to how the PR tooling should be used these days to notify the maintainers. As JSR166 is a crucial piece of the JDK I was wondering if there might be a way already via a label or something. |
You can probably go ahead and ping @DougLea (like this). |
I'm a little hesitant about the precedent of checking for 0 in every ctor argument for a volatile field. |
I'm confused by this if I'm entirely honest @DougLea . Where do we check for 0 in the proposed change? |
As in: If unnecessarily writing 0 to a voltaile field in a ctor is expensive enough for a code work-around here, don't you think it would be better to teach a compiler to avoid it? |
I see what you mean now - thanks! Given that there exists already an empty The same argument applies to initializing volatile fields with their default value, if I'm thinking about it longer. Of course one would wish that the compiler would be able to avoid it, but there have been examples in the past already where the explicit volatile write was avoided (e.g. in JDK-8145680 instead of working on JDK-8145948 for example). Overall, I think it's a reasonably simple improvement with a good performance gain for relatively little cost, but after all I'm in your hands when it comes to approving the proposed change. |
OK. I won't resist doing this as a one-shot. We'll include in jsr166 sources, so it won't matter if done now or later along with integration pass. (Well, except the braces, which are stylistically not used in j.u.c. code) |
/sponsor |
@RogerRiggs @dreis2211 Since your change was applied there have been 54 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit 7e82ba1. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
I appologise for writing this to already closed issue, but Christoph's approach seems to be applicable for at least
Semantically both new AtomicInteger() and new AtomicInteger(0) are the same, but explicitValue() is much slower:
So the same pattern as we used here could be applied for public AtomicInteger(int initialValue) {
if (initialValue != 0 {
value = initialValue;
}
} What do you think? |
For For In the short term we could consider a point fix to replace existing use of |
@stsypanov What Claes said: I explicitly only did it for I don't think we should do it for the other Atomic classes for the mentioned reasons. Let's rather work on the JIT compiler for future optimizations in this area. The |
@cl4es , @dreis2211 thanks for explanation! |
Mailing list message from Andrew Haley on core-libs-dev: On 15/10/2020 11:41, Claes Redestad wrote:
I guess it all depends on whether there's a happens-before -- |
I misphrased somewhat: I think we should try and remove explicit stores of default values to volatile fields in constructors, as suggested by https://bugs.openjdk.java.net/browse/JDK-8145948 I can't think of a way this would break any program (even arguably incorrect ones), and would net us the gains shown in comments here without any point fixes. |
@cl4es could I ask one more question? Would it be helpful to create a separate PR about removal of explicit zeroing of Atomic* classes (excluding AtomicBoolean), e.g. somethin like this master...stsypanov:atom-simpl ? |
Sure. Fixing General advice is to limit sweeping changes to related modules, and not include code maintained in some upstream repo. What you have here looks fine. |
DOne #818 |
Hi,
the following PR optimizes
new AtomicBoolean(boolean)
by avoiding the volatile write in casefalse
is passed. Essentially, it changes the ternary operator to a simpleif
without theelse
that would cause the volatile write.The resulting bytecode seems to also benefit from the change:
After:
A simple benchmark that returns
new AtomicBoolean(false)
shows the following results, that brings it on par tonew AtomicBoolean()
:In case you think this is worthwhile I'd be happy if this is sponsored.
Cheers,
Christoph
Progress
Issue
Reviewers
Download
$ git fetch https://git.openjdk.java.net/jdk pull/510/head:pull/510
$ git checkout pull/510