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

Liam Miller-Cushon cushon at openjdk.org
Fri Oct 31 10:10:07 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:

  Update length to code units instead of bytes

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

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

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

  Stats: 44 lines in 5 files changed: 8 ins; 9 del; 27 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