String.equalsIgnoreCase(...) optimization

Martijn Verburg martijnverburg at gmail.com
Tue Apr 26 10:10:06 UTC 2016


Hi Andrey,

This is best discussed in the core-libs mailing list.

Cheers,
Martijn

On 26 April 2016 at 10:49, Andrey <andrey at tweak.su> wrote:

> Hello!
>
> I read source code equalsIgnoreCase(...) in String class and saw that it
> is not optimal. This method check length and call
> regionMatches(...)  with 'constant' values
>
>
> http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l1095
>
>
> ...
>
>    && (anotherString.value.length == value.length)
>     && regionMatches(true, 0, anotherString, 0, value.length);
> ...
>
> But regionMatches(...) check 'constant' values
>
>
> http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l1338
>
> // Note: toffset, ooffset, or len might be near -1>>>1.
> if ((ooffset < 0) || (toffset < 0)
>     || (toffset > (long)value.length - len)
>     || (ooffset > (long)other.value.length - len)) {
>   return false;
> }
>
> and increment equalent variables to==po in loop
>
>
> http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l1344
>
> while (len-- > 0) {
>
>   char c1 = ta[to++];
>
>   char c2 = pa[po++];
>
> ...
>
> }
>
> and use if(...) in while loop
>
>
> http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l1349
>
> if (ignoreCase) {
>
> ...
>
> }
>
>
>
> May be can create optimized regionMatches(...) for use
> in equalsIgnoreCase(...)?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/quality-discuss/attachments/20160426/15d039d8/attachment.html>


More information about the quality-discuss mailing list