RFR: 8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX [v2]

Andrey Turbanov aturbanov at openjdk.java.net
Mon Jun 13 09:24:49 UTC 2022


On Sun, 15 May 2022 10:31:20 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:

>> TimeUnit.toMillis/toNanos/toMicros/toSeconds is shorter and a bit faster.
>> Compared via JMH benchmark: 150ns -> 125ns/op
>> <details>
>> <summary>Benchamark adapted from http://cr.openjdk.java.net/~shade/8152083/TimeUnitBench.java</summary>
>> 
>> 
>> @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
>> @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
>> @Fork(1)
>> @State(Scope.Thread)
>> @BenchmarkMode(Mode.AverageTime)
>> @OutputTimeUnit(TimeUnit.NANOSECONDS)
>> public class TimeUnitBench {
>> 
>>     static final int SIZE = TimeUnit.values().length * 10;
>> 
>>     @Param({"0", "1", "2", "3", "4", "5", "6", "-1"})
>>     int bias;
>> 
>>     TimeUnit[] mod;
>> 
>>     @Setup
>>     public void setup() {
>>         mod = new TimeUnit[SIZE];
>> 
>>         TimeUnit[] vals = TimeUnit.values();
>>         for (int c = 0; c < SIZE; c++) {
>>             if (bias == -1) {
>>                 // megamorphic
>>                 mod[c] = vals[c % vals.length];
>>             } else {
>>                 mod[c] = vals[bias];
>>             }
>>         }
>> 
>>         Random r = new Random();
>>         for (int c = 0; c < 10000; c++) {
>>             int i = r.nextInt();
>>             for (int o = 0; o < vals.length; o++) {
>>                 if (vals[o].toNanos(i) != TimeUnit.NANOSECONDS.convert(i, vals[o]))
>>                     throw new IllegalStateException("switch " + o);
>>             }
>>         }
>>     }
>> 
>>     @Benchmark
>>     public void const_convert(Blackhole bh) {
>>         for (TimeUnit tu : mod) {
>>             bh.consume(do_convert(tu));
>>         }
>>     }
>> 
>>     @Benchmark
>>     public void value_convert(Blackhole bh) {
>>         for (TimeUnit tu : mod) {
>>             bh.consume(do_convert(tu, 1L));
>>         }
>>     }
>> 
>>     @Benchmark
>>     public void const_toNanos(Blackhole bh) {
>>         for (TimeUnit tu : mod) {
>>             bh.consume(do_toNanos(tu));
>>         }
>>     }
>> 
>>     @Benchmark
>>     public void value_toNanos(Blackhole bh) {
>>         for (TimeUnit tu : mod) {
>>             bh.consume(do_toNanos(tu, 1L));
>>         }
>>     }
>> 
>>     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
>>     private long do_toNanos(TimeUnit tu) {
>>         return tu.toNanos(1L);
>>     }
>> 
>>     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
>>     private long do_toNanos(TimeUnit tu, long value) {
>>         return tu.toNanos(value);
>>     }
>> 
>>     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
>>     private long do_convert(TimeUnit tu) {
>>         return TimeUnit.NANOSECONDS.convert(1L, tu);
>>     }
>> 
>>     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
>>     private long do_convert(TimeUnit tu, long value) {
>>         return TimeUnit.NANOSECONDS.convert(value, tu);
>>     }
>> }
>> 
>> Results:
>> 
>> Benchmark                    (bias)  Mode  Cnt    Score    Error  Units
>> TimeUnitBench.const_convert       0  avgt    5  151,856 ± 28,595  ns/op
>> TimeUnitBench.const_convert       1  avgt    5  150,720 ± 23,863  ns/op
>> TimeUnitBench.const_convert       2  avgt    5  151,432 ± 23,184  ns/op
>> TimeUnitBench.const_convert       3  avgt    5  150,959 ± 24,726  ns/op
>> TimeUnitBench.const_convert       4  avgt    5  150,966 ± 25,280  ns/op
>> TimeUnitBench.const_convert       5  avgt    5  150,976 ± 25,542  ns/op
>> TimeUnitBench.const_convert       6  avgt    5  151,118 ± 25,254  ns/op
>> TimeUnitBench.const_convert      -1  avgt    5  152,673 ± 29,861  ns/op
>> TimeUnitBench.const_toNanos       0  avgt    5  121,296 ± 25,236  ns/op
>> TimeUnitBench.const_toNanos       1  avgt    5  121,707 ± 25,364  ns/op
>> TimeUnitBench.const_toNanos       2  avgt    5  121,736 ± 25,726  ns/op
>> TimeUnitBench.const_toNanos       3  avgt    5  121,822 ± 25,491  ns/op
>> TimeUnitBench.const_toNanos       4  avgt    5  121,867 ± 26,090  ns/op
>> TimeUnitBench.const_toNanos       5  avgt    5  121,927 ± 25,611  ns/op
>> TimeUnitBench.const_toNanos       6  avgt    5  121,821 ± 25,843  ns/op
>> TimeUnitBench.const_toNanos      -1  avgt    5  121,923 ± 25,206  ns/op
>> TimeUnitBench.value_convert       0  avgt    5  150,525 ± 25,439  ns/op
>> TimeUnitBench.value_convert       1  avgt    5  151,098 ± 24,024  ns/op
>> TimeUnitBench.value_convert       2  avgt    5  151,259 ± 25,381  ns/op
>> TimeUnitBench.value_convert       3  avgt    5  151,030 ± 25,548  ns/op
>> TimeUnitBench.value_convert       4  avgt    5  151,290 ± 25,998  ns/op
>> TimeUnitBench.value_convert       5  avgt    5  151,006 ± 25,448  ns/op
>> TimeUnitBench.value_convert       6  avgt    5  150,945 ± 25,314  ns/op
>> TimeUnitBench.value_convert      -1  avgt    5  151,186 ± 25,814  ns/op
>> TimeUnitBench.value_toNanos       0  avgt    5  121,556 ± 25,745  ns/op
>> TimeUnitBench.value_toNanos       1  avgt    5  123,410 ± 22,323  ns/op
>> TimeUnitBench.value_toNanos       2  avgt    5  125,452 ± 26,575  ns/op
>> TimeUnitBench.value_toNanos       3  avgt    5  125,297 ± 26,204  ns/op
>> TimeUnitBench.value_toNanos       4  avgt    5  125,357 ± 26,085  ns/op
>> TimeUnitBench.value_toNanos       5  avgt    5  125,165 ± 26,185  ns/op
>> TimeUnitBench.value_toNanos       6  avgt    5  125,536 ± 25,487  ns/op
>> TimeUnitBench.value_toNanos      -1  avgt    5  125,460 ± 25,063  ns/op
>> 
>> 
>> </details>
>
> Andrey Turbanov has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains two commits:
> 
>  - Merge remote-tracking branch 'origin/master' into Use_direct_TimeUnit.toSome_instead_of_TimeUnit.convert
>    
>    # Conflicts:
>    #	src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java
>    #	src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java
>  - [PATCH] Simplify TimeUnit.convert calls to TimeUnit.toSomething instead

Still hope to merge it :)

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

PR: https://git.openjdk.org/jdk/pull/8376


More information about the core-libs-dev mailing list