RFR: 8255299: Drop explicit zeroing at instantiation of Atomic* objects

Сергей Цыпанов github.com+10835776+stsypanov at openjdk.java.net
Fri Oct 23 09:59:41 UTC 2020


On Fri, 23 Oct 2020 09:12:15 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

>> As discussed in https://github.com/openjdk/jdk/pull/510 there is never a reason to explicitly instantiate any instance of `Atomic*` class with its default value, i.e. `new AtomicInteger(0)` could be replaced with `new AtomicInteger()` which is faster:
>> @State(Scope.Thread)
>> @OutputTimeUnit(TimeUnit.NANOSECONDS)
>> @BenchmarkMode(value = Mode.AverageTime)
>> public class AtomicBenchmark {
>>   @Benchmark
>>   public Object defaultValue() {
>>     return new AtomicInteger();
>>   }
>>   @Benchmark
>>   public Object explicitValue() {
>>     return new AtomicInteger(0);
>>   }
>> }
>> THis benchmark demonstrates that `explicitValue()` is much slower:
>> Benchmark                      Mode  Cnt   Score   Error  Units
>> AtomicBenchmark.defaultValue   avgt   30   4.778 ± 0.403  ns/op
>> AtomicBenchmark.explicitValue  avgt   30  11.846 ± 0.273  ns/op
>> So meanwhile https://bugs.openjdk.java.net/browse/JDK-8145948 is still in progress we could trivially replace explicit zeroing with default constructors gaining some performance benefit with no risk.
>> 
>> I've tested the changes locally, both tier1 and tier 2 are ok. 
>> 
>> Could one create an issue for tracking this?
>
> src/java.base/share/classes/sun/net/ResourceManager.java line 65:
> 
>> 63:         } catch (NumberFormatException e) {}
>> 64:         maxSockets = defmax;
>> 65:         numSockets = new AtomicInteger();
> 
> Changes in sun/net look good to me.

@dfuch Could you then sponsor this PR?

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

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



More information about the security-dev mailing list