On Mon, 10 Aug 2026 08:55:10 GMT, Volkan Yazici <[email protected]> wrote:

>> Liam Miller-Cushon has updated the pull request incrementally with two 
>> additional commits since the last revision:
>> 
>>  - Add OutOfMemoryError checks for UTF-16 encodedLength fast paths
>>  - Copyright years
>
> src/java.base/share/classes/java/lang/String.java line 1655:
> 
>> 1653:             }
>> 1654:             sp++;
>> 1655:         }
> 
> Does this pre-scan bring any measurable benefits?
> 
> Have you considered collapsing the two loops and retaining the fast checks? 
> Consider the following:
> 
>     for (int sp = off, sl = off + len; sp < sl;) {
>         char c = StringUTF16.getChar(val, sp++);
>         if (c < Character.MIN_HIGH_SURROGATE) {
>             continue;
>         }
>         if (c > Character.MAX_LOW_SURROGATE) {
>             continue;
>         }
>         if (sp == sl || !Character.isLowSurrogate(StringUTF16.getChar(val, 
> sp))) {
>             return true;
>         }
>         sp++;
>     }
>     return false;

My recollection from the similar pre-scan in `encodedLengthUTF8_UTF16` is that 
C2 was able to optimize the simpler loop better and it made a measurable 
difference, the same pattern is used in `encodeUTF8_UTF16`. I haven't 
re-measured for this loop though.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/32268#discussion_r3749911871

Reply via email to