String indexOf Performance
Aleksey Shipilev
shade at redhat.com
Fri Jan 27 20:56:46 UTC 2017
On 01/27/2017 09:21 PM, Scott Palmer wrote:
> http://pastebin.com/zRXKwD64
Okay:
Benchmark Mode Cnt Score Error Units
# JDK 8u121
IndexOfBenchmark.StringIndexOfChar thrpt 5 141857.332 ± 5530.472 ops/s
IndexOfBenchmark.StringIndexOfString thrpt 5 113091.517 ± 2241.533 ops/s
# JDK 9b152
IndexOfBenchmark.StringIndexOfChar thrpt 5 154525.343 ± 3796.818 ops/s
IndexOfBenchmark.StringIndexOfString thrpt 5 185917.059 ± 3391.230 ops/s
Perfasm for 9b152:
http://cr.openjdk.java.net/~shade/scratch/indexOf.perfasm
It does indeed look like indexOf(String) is backed by the intrinsic (notice
"vpcmpestri $0xc,(%rbx),%xmm0"), while indexOf(char) does the Java code (notice
how real locations in bytecode are mapped onto generated code). This is further
corroborated by StringLatin1.indexOf not having @HotspotIntrinsicCandidate:
http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/e4b19b8d4bbf/src/java.base/share/classes/java/lang/StringLatin1.java#l181
Submitted:
https://bugs.openjdk.java.net/browse/JDK-8173585
Think about it this way: you have the improvement for both cases in JDK 9 due to
Compact Strings, but it somewhat twisted the performance model to be less
intuitive. Continue using indexOf(char) in the code, and wait for JDK to catch up.
Thanks,
-Aleksey
More information about the jdk9-dev
mailing list