<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Philippe and Maurizio,</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Sure, I will take a look at this improvement suggestion. Thanks, Philippe, for the tip. </div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Best, Per Minborg</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Maurizio Cimadamore <maurizio.cimadamore@oracle.com><br>
<b>Sent:</b> Friday, June 13, 2025 3:45 PM<br>
<b>To:</b> Philippe Marschall <kustos@gmx.net>; panama-dev@openjdk.org <panama-dev@openjdk.org>; Per-Ake Minborg <per-ake.minborg@oracle.com><br>
<b>Subject:</b> Re: Reducing byte[] copies of MemorySegment.getString</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hi Philippe,<br>
this is an interesting suggestion. As you observed, we already use JDK <br>
internals in String support, most notably to reduce a double allocation <br>
when _writing_ a string off-heap (where we get access to the string <br>
byte[] directly, instead of going through an intermediate array with <br>
String::getBytes). So your proposed patch completes that for reads too, <br>
which is nice, and will provide a decent boost in what is a relatively <br>
common use case.<br>
<br>
I also think your handling of CharacterCodingException seems pragmatic.<br>
<br>
@Per, can you take a look?<br>
<br>
Thanks for sharing<br>
Maurizio<br>
<br>
<br>
On 13/06/2025 13:55, Philippe Marschall wrote:<br>
> Hello<br>
><br>
> I had a look at the implementation of MemorySegment.getString and it <br>
> seems to allocate every byte[] once in StringSupport and then a copy <br>
> in the String constructor. StringSupport already has access to <br>
> JavaLangAccess and therefore uncheckedNewStringNoRepl which can <br>
> allocate a String without copying the given byte[].<br>
><br>
> I did an experiment and changed the StringSupport implementation to <br>
> call uncheckedNewStringNoRepl instead [1]. uncheckedNewStringNoRepl <br>
> behaves different from new String() and throws <br>
> CharacterCodingException on malformed input. The API contract of <br>
> MemorySegment#getString specifies replacement characters in case of <br>
> malformed input. The easiest way I found to support this is simply <br>
> catch and call the String constructor for malformed input.<br>
><br>
> There is already an existing JMH microbenchmark for <br>
> MemorySegment#getString in ToJavaStringTest, I ran it with the GC <br>
> profiler (MICRO="OPTIONS=-prof gc"). And the results are as follows:<br>
><br>
> - the average time per op goes down by about 25% to 30% depending on <br>
> the String size<br>
> - allocations per op goes down by 33% to 50% depending on the String <br>
> size, the the relative savings increasing with the String size<br>
><br>
> I ran the tier1 test suite and it passed.<br>
><br>
>  [1] <br>
> <a href="https://github.com/marschall/jdk/commit/b43920dd7b666c5d09c088a9918c3d65a5a32e8c">
https://github.com/marschall/jdk/commit/b43920dd7b666c5d09c088a9918c3d65a5a32e8c</a><br>
><br>
> Cheers<br>
> Philippe<br>
</div>
</span></font></div>
</body>
</html>