RFR: 8254146: Avoid unnecessary volatile write on new AtomicBoolean(false)
Christoph Dreis
github.com+6304496+dreis2211 at openjdk.java.net
Wed Oct 7 10:08:26 UTC 2020
Hi,
the following PR optimizes `new AtomicBoolean(boolean)` by avoiding the volatile write in case `false` is passed.
Essentially, it changes the ternary operator to a simple `if` without the `else` that would cause the volatile write.
The resulting bytecode seems to also benefit from the change:
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iload_1
6: ifeq 13
9: iconst_1
10: goto 14
13: iconst_0
14: putfield #7 // Field value:I
17: return
After:
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: iload_1
5: ifeq 13
8: aload_0
9: iconst_1
10: putfield #7 // Field value:I
13: return
A simple benchmark that returns `new AtomicBoolean(false)` shows the following results, that brings it on par to `new
AtomicBoolean()`: MyBenchmark.empty avgt 10 3,103 ± 0,246 ns/op
MyBenchmark.explicitNew avgt 10 2,966 ± 0,071 ns/op
MyBenchmark.explicitOld avgt 10 7,738 ± 0,321 ns/op
In case you think this is worthwhile I'd be happy if this is sponsored.
Cheers,
Christoph
-------------
Commit messages:
- Avoid unnecessary volatile write on new AtomicBoolean(false)
Changes: https://git.openjdk.java.net/jdk/pull/510/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=510&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8254146
Stats: 3 lines in 1 file changed: 2 ins; 0 del; 1 mod
Patch: https://git.openjdk.java.net/jdk/pull/510.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/510/head:pull/510
PR: https://git.openjdk.java.net/jdk/pull/510
More information about the core-libs-dev
mailing list