RFR: 8254146: Avoid unnecessary volatile write on new AtomicBoolean(false)

DougLea github.com+2233919+douglea at openjdk.java.net
Thu Oct 8 06:49:49 UTC 2020


On Wed, 7 Oct 2020 18:18:43 GMT, Christoph Dreis <github.com+6304496+dreis2211 at openjdk.org> wrote:

>> 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 `AtomicBoolean` constructor that avoids the
> volatile write, I didn't see that as a big problem to be honest. Also: The proposed "workaround" doesn't seem to be
> overly complicated - personally I find the empty constructor to be the bigger workaround and less explicit.  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](https://bugs.openjdk.java.net/browse/JDK-8145680) instead of
> working on [JDK-8145948](https://bugs.openjdk.java.net/browse/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)

-------------

PR: https://git.openjdk.java.net/jdk/pull/510


More information about the core-libs-dev mailing list