RFR: 8355177: Speed up StringBuilder::append(char[]) via Unsafe::copyMemory [v8]
Shaojin Wen
swen at openjdk.org
Mon Jul 28 14:06:58 UTC 2025
On Mon, 28 Jul 2025 12:46:22 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:
>> String uses `<< coder` in many places. I think the following way of writing is also good:
>>
>> Unsafe.getUnsafe().copyMemory(
>> ca,
>> Unsafe.ARRAY_CHAR_BASE_OFFSET + (long) off << String.UTF16,
>> val,
>> Unsafe.ARRAY_CHAR_BASE_OFFSET + (long) index << String.UTF16,
>> (long) (end - off) << String.UTF16);
>
> I agree that we can expect arrays to be laid out as a contiguous chunk of memory with the intuitively expected element size.
> _But..._ AFAIK this is not specified anywhere in the JVMS, although it is true that it is tacitly assumed in many low-level parts of the codebase. So, in this sense, I'm fine with your code.
There are many places in the String class that use `<< 1` and `>> 1` to handle the length of UTF16 byte[], so is it okay to use `<< 1` directly in the current version of the code?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24773#discussion_r2236631515
More information about the core-libs-dev
mailing list