Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 571579

Summary: [performance] prefer Arrays.equals
Product: [Eclipse Project] JDT Reporter: Jörg Kubitz <jkubitz-eclipse>
Component: CoreAssignee: Jörg Kubitz <jkubitz-eclipse>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: jarthana, julian.honnen, Lars.Vogel, loskutov
Version: 4.20   
Target Milestone: 4.20 M1   
Hardware: PC   
OS: Windows 10   
See Also: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/176984
https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=a055c74e546351cc9356bde8e6c5eb95416f542e
Whiteboard:
Bug Depends on:    
Bug Blocks: 571430    

Description Jörg Kubitz CLA 2021-03-01 03:08:08 EST
I microbenchmarked CharOperation.equals vs Arrays.equals for 4 cases:
a) strings are equal
b) strings are different at begin
c) strings are different at mid
d) strings are different at end

CharOperation.equals only has an slight advantage if strings are different at end. But in average Arrays.equals is twice as fast:

# VM version: JDK 11.0.5, OpenJDK 64-Bit Server VM, 11.0.5+10

Benchmark                                     Mode  Cnt   Score   Error  Units

EqualsBenchmark.testEqualsArraysBegin         avgt    5   4,420 ± 0,048  ns/op
EqualsBenchmark.testEqualsArraysMid           avgt    5   5,095 ± 0,196  ns/op
EqualsBenchmark.testEqualsArraysEnd           avgt    5   6,247 ± 0,151  ns/op
EqualsBenchmark.testEqualsArraysEqual         avgt    5   6,244 ± 0,236  ns/op

EqualsBenchmark.testEqualsCharOperationEqual  avgt    5  16,436 ± 0,161  ns/op
EqualsBenchmark.testEqualsCharOperationBegin  avgt    5  15,993 ± 0,149  ns/op
EqualsBenchmark.testEqualsCharOperationMid    avgt    5  10,929 ± 0,097  ns/op
EqualsBenchmark.testEqualsCharOperationEnd    avgt    5   3,145 ± 0,047  ns/op
Comment 1 Eclipse Genie CLA 2021-03-01 03:11:41 EST
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/176984
Comment 2 Julian Honnen CLA 2021-03-01 03:39:10 EST
How long were your input strings?
Comment 3 Jörg Kubitz CLA 2021-03-01 03:41:50 EST
38 chars like: "org.openjdk.jmh.annotations.Benchmark" + randomchar
Comment 4 Julian Honnen CLA 2021-03-01 03:45:59 EST
Please try the same benchmark for unqualified names.
Comment 5 Jörg Kubitz CLA 2021-03-01 03:53:59 EST
"Benchmark"+randomchar

Benchmark                                      Mode  Cnt  Score   Error  Units

EqualsUnqualified.testEqualsArraysBegin        avgt    5  4,123 ± 0,060  ns/op
EqualsUnqualified.testEqualsArraysMid          avgt    5  5,005 ± 0,395  ns/op
EqualsUnqualified.testEqualsArraysEnd          avgt    5  5,931 ± 0,105  ns/op
EqualsUnqualified.testEqualsArraysEqual        avgt    5  6,216 ± 0,122  ns/op

EqualsUnqualified.testEqualsCharOperationEqual avgt    5  8,237 ± 0,278  ns/op
EqualsUnqualified.testEqualsCharOperationBegin avgt    5  7,900 ± 0,273  ns/op
EqualsUnqualified.testEqualsCharOperationMid   avgt    5  5,456 ± 0,219  ns/op
EqualsUnqualified.testEqualsCharOperationEnd   avgt    5  3,225 ± 0,194  ns/op

There is overhead in Arrays.equals but still slightly better then the loop.
Comment 6 Julian Honnen CLA 2021-03-01 04:23:48 EST
I would have expected a greater difference in Arrays.equals, but the fully qualified name is probably still a short string for the vectorized implementation.
Comment 7 Jörg Kubitz CLA 2021-03-01 08:12:50 EST
Just for completness (and verification, that length matters):

Benchmark                                Mode  Cnt    Score   Error  Units

EqualsLong.testEqualsArraysBegin         avgt    5    4,405 ± 0,108  ns/op
EqualsLong.testEqualsArraysMid           avgt    5   18,018 ± 0,398  ns/op
EqualsLong.testEqualsArraysEnd           avgt    5   18,832 ± 0,293  ns/op
EqualsLong.testEqualsArraysEqual         avgt    5   18,904 ± 0,580  ns/op

EqualsLong.testEqualsCharOperationEqual  avgt    5  108,430 ± 3,130  ns/op
EqualsLong.testEqualsCharOperationBegin  avgt    5  111,048 ± 3,234  ns/op
EqualsLong.testEqualsCharOperationMid    avgt    5   62,427 ± 1,168  ns/op
EqualsLong.testEqualsCharOperationEnd    avgt    5    3,175 ± 0,069  ns/op

for length 385
Comment 9 Julian Honnen CLA 2021-03-18 03:21:36 EDT
Thanks, Jörg!
Comment 10 Jay Arthanareeswaran CLA 2021-04-08 03:21:00 EDT
Verified for 4.20 M1 by code inspection