RFR: 8355177: Speed up StringBuilder::append(char[]) via Unsafe::copyMemory [v8]

Raffaello Giulietti rgiulietti at openjdk.org
Thu Jul 24 15:24:04 UTC 2025


On Fri, 27 Jun 2025 01:04:32 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> In BufferedReader.readLine and other similar scenarios, we need to use StringBuilder.append(char[]) to build the string.
>> 
>> For these scenarios, we can Unsafe.copyMemory instead of the character copy of the char-by-char loop to improve the speed.
>> 
>> @RogerRiggs completed the optimization when the encoder is LATIN1 in PR #24967. This PR continues to complete the optimization when the encoder is UTF16.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Update src/java.base/share/classes/java/lang/StringUTF16.java
>   
>   Co-authored-by: Chen Liang <liach at openjdk.org>

src/java.base/share/classes/java/lang/StringUTF16.java line 1487:

> 1485:         Unsafe.getUnsafe().copyMemory(
> 1486:                 ca,
> 1487:                 Unsafe.ARRAY_CHAR_BASE_OFFSET + ((long) off << 1),

Suggestion:

                Unsafe.ARRAY_CHAR_BASE_OFFSET + (long) off * Unsafe.ARRAY_CHAR_INDEX_SCALE,

src/java.base/share/classes/java/lang/StringUTF16.java line 1489:

> 1487:                 Unsafe.ARRAY_CHAR_BASE_OFFSET + ((long) off << 1),
> 1488:                 val,
> 1489:                 Unsafe.ARRAY_BYTE_BASE_OFFSET + ((long) index << 1),

Suggestion:

                Unsafe.ARRAY_BYTE_BASE_OFFSET + ((long) index << 1) * Unsafe.ARRAY_BYTE_INDEX_SCALE,

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

PR Review Comment: https://git.openjdk.org/jdk/pull/24773#discussion_r2228828535
PR Review Comment: https://git.openjdk.org/jdk/pull/24773#discussion_r2228829451


More information about the core-libs-dev mailing list