[External] : Re: MemorySegment APIs for reading and writing strings with known lengths

Liam Miller-Cushon cushon at google.com
Wed Nov 19 08:35:37 UTC 2025


Thanks Jorn, I took another pass over the PR and have a draft of the
changes you described: https://github.com/openjdk/jdk/pull/28043

>     void copy(String src, Charset dstEncoding, int srcIndex,
> MemorySegment dst, int numChars);
>
For copy, many of the other overloads have a dstOffset as well. I suppose
another overload could be added for it later, or do we not want that here?

> For encoding directly into a memory segment without the need to go to an
> intermediate buffer, it looks like we can use the internal StringCharBuffer
> class, in combination with the `CharsetEncoder::encode` method. But of
> course we can skip encoding altogether when the internal string encoding
> matches the target, and just do a bulk copy.
>
Thanks, I got it working with StringCharBuffer/CharsetEncoder::encode and
did some initial benchmarking. It is nice that it avoids the copy, but the
benchmarking suggests it's ~4x slower than calling String#getBytes. I
wonder if it makes sense to use String#getBytes for now, and consider
future work to support encoding with substrings directly to the destination
with equivalent or better performance than getBytes. What do you think?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20251119/60800ba8/attachment.htm>


More information about the panama-dev mailing list