<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Liam! </div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Thank you for your comments on <code>MemorySegment::getString</code> !</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
The intention was that the creation of the intermediate array should be elided, but as you point out, that is (at least sometimes) not the case. I've made an experimental patch where I've used
<code>JavaLangAccess::uncheckedNewStringNoRepl</code> in <code>StringSupport</code> which looks promising:</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Base: </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString   5 avgt 30 13.559 ± 0.246 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString  20 avgt 30 67.047 ± 7.541 ns/op <- Slow </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString 100 avgt 30 28.092 ± 0.547 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString 200 avgt 30 37.679 ± 0.645 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString 451 avgt 30 64.544 ± 1.096 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Experimental Patch: </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString   5 avgt 30 8.847 ± 0.178 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString  20 avgt 30 20.332 ± 4.495 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString 100 avgt 30 21.808 ± 0.315 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString 200 avgt 30 28.711 ± 0.105 ns/op </div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
ToJavaStringTest.panama_readString 451 avgt 30 51.806 ± 1.067 ns/op</div>
<div style="font-family: Consolas, Courier, monospace; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
So, this indicates that there are gains to be made here, although the ultimate solution might be something else (such as improving inlining). I have r<a href="https://bugs.openjdk.org/browse/JDK-8362893">aised an issue for this</a>.
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Thanks again for bringing this to the general knowledge. </div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Best,</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Per Minborg</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Oracle Core Libraries</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> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Liam Miller-Cushon <cushon@google.com><br>
<b>Sent:</b> Friday, July 18, 2025 7:26 PM<br>
<b>To:</b> OpenJDK Core Libs Developers <core-libs-dev@openjdk.java.net><br>
<b>Subject:</b> FFM - Optimizing string reads in MemorySegment</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi,
<div><br>
</div>
<div>I have a question about the performance of string reads in MemorySegment.</div>
<div><br>
</div>
<div>There was some discussion about reading strings with known lengths in JDK-8333843, and the outcome was to update the getString javadoc to recommend:</div>
<div><br>
</div>
<div>byte[] bytes = new byte[length];<br>
MemorySegment.copy(segment, JAVA_BYTE, offset, bytes, 0, length);<br>
return new String(bytes, charset);</div>
<div><br>
</div>
<div>As I understand it that's as efficient as what MemorySegment#getString is doing under the hood, the main difference with getString is that it's first reading the length of a null-terminated string, but after that it just uses MemorySegment.copy and calls
 the String constructor.</div>
<div><br>
</div>
<div>Has avoiding the byte[] copy in the String constructor been considered?</div>
<div><br>
</div>
<div>In theory it might be possible for the runtime to inline the String constructor, see that the byte[] doesn't escape, and skip the copy. In practice, based on some benchmarking my colleague Louis Wasserman was doing, that doesn't seem to be happening.</div>
<div><br>
</div>
<div>Would it make sense to consider using JavaLangAccess#uncheckedNewStringNoRepl here to avoid having to copy the byte[]?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Liam</div>
</div>
</div>
</body>
</html>