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

Severin Gehwolf sgehwolf at openjdk.java.net
Wed Oct 7 10:08:26 UTC 2020


On Mon, 5 Oct 2020 17:29:02 GMT, Christoph Dreis <github.com+6304496+dreis2211 at openjdk.org> wrote:

>> 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
>
> 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?

Once you have signed the OCA, please let us know by writing /signed in a comment in this pull request.

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

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


More information about the core-libs-dev mailing list