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

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Nov 12 11:33:41 UTC 2025


On 12/11/2025 11:14, Liam Miller-Cushon wrote:
>
>     I believe what you mean here is that if I have a string, and I
>     want to copy to a destination segment I could either:
>
>     * if the string buffer is compatible, just bulk-copy that buffer
>     into the target segment
>     * if the string buffer is not compatible, encode the string
>     _directly_ into the target segment
>
>     Correct? If so, I tend to agree this would be slightly
>     preferrable, as we'd be touching things only once. And, I believe
>     this can be also done to the existing setString method?
>
> I think that's right, but I'd also gotten slightly confused. To try to 
> restate:
>
> setString assumes \0, which I touched on in the original mail about 
> not being ideal if you want to write non-\0 terminated strings and the 
> output buffer is perfectly sized.
>
> In the case where the bytes aren't compatible, setString currently 
> falls back to getBytes and then copies that into the destination 
> segment. But I think it could be optimized to encode directly to the 
> output without changing the API.
Yes, I think we understood each other -- that's what I meant by "I 
believe this can be also done to the existing setString method"
>
> I had thought your proposal was to support the use-case of writing 
> non-\0 terminated strings by using MS::ofString and then MS::copy, 
> which I think would have equivalent performance to setString for the 
> bytes compatible case, but might be more constrained for the case 
> where the bytes aren't compatible. Had I misunderstood that part?

For the non-\0 terminated strings, you have the String-based 
MemorySegment::copy I described - e.g.

void copy(String srcString, Charset srcCharset, int srcIndex, 
MemorySegment dstSegment, long dstOffset, int length);


With this, we also have two cases:

* if the charset is compatible with the string buffer, we just bulk-copy 
the string buffer (or a portion of it) into the dest segment
* otherwise we can encode the srcString directly into the dest segment

Maurizio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20251112/18957af0/attachment.htm>


More information about the panama-dev mailing list