RFR: 8325169: Reduce String::indexOf overheads [v2]

Claes Redestad redestad at openjdk.org
Sat Feb 3 01:04:04 UTC 2024


On Fri, 2 Feb 2024 16:40:45 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/String.java line 2506:
>> 
>>> 2504:         fromIndex = Math.max(0, fromIndex);
>>> 2505:         return isLatin1() ? StringLatin1.indexOf(value, ch, fromIndex, value.length)
>>> 2506:                 : StringUTF16.indexOf(value, ch, fromIndex, value.length >> 1);
>> 
>> This needs to include the check for `fromIndex >= this.length()`:
>> Suggestion:
>> 
>>         fromIndex = Math.max(0, fromIndex);
>>         int toIndex = length();
>>         if (fromIndex >= toIndex) {
>>             return -1;
>>         }
>>         return isLatin1()
>>                 ? StringLatin1.indexOf(value, ch, fromIndex, toIndex)
>>                 : StringUTF16.indexOf(value, ch, fromIndex, toIndex);
>
> I don't think so.
> If you deeply follow the invoked `indexOf()` methods, there's either a check later, or the loop conditions are false on entry (although I'm not sure about the intrinsic methods).

While not directly observable (all tests pass) then sadly the intrinsic implementation emits a string range check that triggers a decompilation and subsequent compilation without the intrinsic if you'd call with a `fromIndex` with values greater than `length()` (equal to should be fine). I'll try out some options here.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/17685#discussion_r1476907109


More information about the core-libs-dev mailing list