<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>