RFR: 8369564: Provide a MemorySegment API to read strings with known lengths

Liam Miller-Cushon cushon at openjdk.org
Wed Oct 29 10:58:02 UTC 2025


This PR proposes adding a new overload to `MemorySegment::getString` that takes a known byte length of the content.

This was previously proposed in https://github.com/openjdk/jdk/pull/20725, but the outcome of [JDK-8333843](https://bugs.openjdk.org/browse/JDK-8333843) was to update `MemorySegment#getString` to suggest


    byte[] bytes = new byte[length];
    MemorySegment.copy(segment, JAVA_BYTE, offset, bytes, 0, length);
    return new String(bytes, charset);


However this is less efficient than what the implementation of getString does after [JDK-8362893](https://bugs.openjdk.org/browse/JDK-8362893), it now uses `JavaLangAccess::uncheckedNewStringNoRepl` to avoid the copy.

-------------

Commit messages:
 - 8369564: Provide a MemorySegment API to read strings with known lengths

Changes: https://git.openjdk.org/jdk/pull/28043/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=28043&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8369564
  Stats: 152 lines in 4 files changed: 136 ins; 14 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/28043.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/28043/head:pull/28043

PR: https://git.openjdk.org/jdk/pull/28043


More information about the core-libs-dev mailing list