RFR: 8263436: Silly array comparison in GaloisCounterMode.overlapDetection

Aleksey Shipilev shade at openjdk.java.net
Thu Mar 11 18:27:08 UTC 2021


On Thu, 11 Mar 2021 18:19:41 GMT, Anthony Scarpino <ascarpino at openjdk.org> wrote:

>> SonarCloud reports:
>>   Use "Arrays.equals(array1, array2)" or the "==" operator instead of using the "Object.equals(Object obj)" method.
>> 
>>         } else if (!src.isDirect() && !dst.isDirect()) {
>>             if (!src.isReadOnly()) {
>>                 // If using the heap, check underlying byte[] address.
>>                 if (!src.array().equals(dst.array()) ) { // <--- here
>> 
>> Additional testing:
>>   - [x] Linux x86_64 fastdebug `jdk_security`
>
> Marked as reviewed by ascarpino (Reviewer).

Here is a demo, if anyone interested:

@State(Scope.Thread)
public class ArrayEquals {
    byte[] b1 = new byte[1];
    byte[] b2 = new byte[1];

    @Benchmark
    public boolean acmp() { return b1 == b2; }

    @Benchmark
    public boolean eq() { return b1.equals(b2); }
}

Benchmark                        Mode  Cnt   Score    Error   Units

# C2 (Default)
ArrayEquals.acmp                 avgt   5     0.678 ± 0.001   ns/op
ArrayEquals.acmp:cycles          avgt         2.576           #/op
ArrayEquals.acmp:instructions    avgt        10.199           #/op

ArrayEquals.eq                   avgt   5     0.680 ± 0.007   ns/op
ArrayEquals.eq:cycles            avgt         2.540           #/op
ArrayEquals.eq:instructions      avgt        13.032           #/op ; <--- null check

# C1
ArrayEquals.acmp                 avgt   5     1.086 ± 0.006   ns/op
ArrayEquals.acmp:cycles          avgt         4.110           #/op
ArrayEquals.acmp:instructions    avgt        16.325           #/op

ArrayEquals.eq                   avgt   5     4.606 ± 0.001   ns/op ; <--- equals() inline failed
ArrayEquals.eq:cycles            avgt        17.328           #/op
ArrayEquals.eq:instructions      avgt        42.520           #/op

# -Xint
ArrayEquals.acmp                 avgt   5   207.550 ± 1.126   ns/op
ArrayEquals.acmp:cycles          avgt       763.037           #/op
ArrayEquals.acmp:instructions    avgt       570.161           #/op

ArrayEquals.eq                   avgt   5   300.449 ± 26.939  ns/op ; <--- interpreter call cost
ArrayEquals.eq:cycles            avgt      1068.852           #/op
ArrayEquals.eq:instructions      avgt       722.228           #/op

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

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


More information about the security-dev mailing list