String.indexOf(single-char-String)

Michael Bien mbien42 at gmail.com
Mon Nov 22 20:52:11 UTC 2021


Hello,

I kept forgetting which variants of the String methods perform better 
with single-char-Strings and which with char (IDEs had the tendency to 
suggest the wrong variant since it changed between JDK releases). So i 
wrote JMH benchmarks and noticed that the last method with a performance 
difference seems to be String.indexOf() - all other variants performed 
equally (unless I overlooked some).

this might be fairly easy to fix:

https://github.com/openjdk/jdk/pull/6509

(side effect: contains("c") is also faster)

I haven't looked into the intrinsified code of StringLatin1 and 
StringUTF16 to check if it could be fixed there (mostly because i 
actually don't know how the JVM assembles those intrinsics). It might be 
possible to improve this for short Strings in general, not just for 
chars, dependent on why the intrinsified version is actually slower for 
single-char-Strings. I opted for the trivial fix in java code.

best regards,

michael



More information about the core-libs-dev mailing list