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

Liam Miller-Cushon cushon at openjdk.org
Wed Oct 29 16:37:34 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.

Liam Miller-Cushon has updated the pull request incrementally with one additional commit since the last revision:

  Consolidate duplicate code in read methods

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/28043/files
  - new: https://git.openjdk.org/jdk/pull/28043/files/937a8681..cd6db90b

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=28043&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=28043&range=00-01

  Stats: 70 lines in 2 files changed: 3 ins; 57 del; 10 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