RFR: 8369564: Provide a MemorySegment API to read strings with known lengths [v16]
Hannes Greule
hgreule at openjdk.org
Sun Jan 11 07:44:35 UTC 2026
On Sat, 10 Jan 2026 18:51:06 GMT, Liam Miller-Cushon <cushon at openjdk.org> wrote:
>> 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.
>>
>> See also discussion in [this panama-dev@ thread](https://mail.openjdk.org/pipermail/panama-dev/2025-November/021193.html), and mcimadamore's document [Pulling the (foreign) string](https://cr.openjdk.org/~mcimadamore/panama/strings_ffm.html)
>>
>> Benchmark results:
>>
>>
>> Benchmark (size) Mode Cnt Score Error Units
>> ToJavaStringTest.jni_readString 5 avgt 30 55.339 ± 0.401 ns/op
>> ToJavaStringTest.jni_readString 20 avgt 30 59.887 ± 0.295 ns/op
>> ToJavaStringTest.jni_readString 100 avgt 30 84.288 ± 0.419 ns/op
>> ToJavaStringTest.jni_readString 200 avgt 30 119.275 ± 0.496 ns/op
>> ToJavaStringTest.jni_readString 451 avgt 30 193.106 ± 1.528 ns/op
>> ToJavaStringTest.panama_copyLength 5 avgt 30 7.348 ± 0.048 ns/op
>> ToJavaStringTest.panama_copyLength 20 avgt 30 7.440 ± 0.125 ns/op
>> ToJavaStringTest.panama_copyLength 100 avgt 30 11.766 ± 0.058 ns/op
>> ToJavaStringTest.panama_copyLength 200 avgt 30 16.096 ± 0.089 ns/op
>> ToJavaStringTest.panama_copyLength 451 avgt 30 25.844 ± 0.054 ns/op
>> ToJavaStringTest.panama_readString 5 avgt 30 5.857 ± 0.046 ns/op
>> ToJavaStringTest.panama_readString 20 avgt 30 7.750 ± 0.046 ns/op
>> ToJavaStringTest.panama_readString 100 avgt 30 14.109 ± 0.187 ns/op
>> ToJavaStringTest.panama_readString 200 avgt 30 18.035 ± 0.130 ns/op
>> ToJavaStringTest.panama_readString 451 avgt 30 35.896 ± 0.227 ns/op
>> ToJavaStringTest.panama_readStringLength 5 avgt 30 4.565 ± 0.038 ns/op
>> ToJavaStringTest.panama_readStringLength 20...
>
> Liam Miller-Cushon has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 21 additional commits since the last revision:
>
> - Merge branch 'master' into JDK-8369564
> - Rename the parameter of getString from length to byteLength
> - Merge branch 'master' into JDK-8369564
> - Review feedback
> - Update discussion of truncated reads of strings containing \0
> - Return the number of copied bytes
> - More javadoc updates
> - Use Utils.checkNonNegativeArgument
> - Review feedback
>
> * handle numChars + srcIndex overflow, and add tests
> * replace yen with a character that round trips
> - Improve test coverage, and more fixes
> - ... and 11 more: https://git.openjdk.org/jdk/compare/87751392...e2cc6f0b
src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 1351:
> 1349: * largest string supported by the platform
> 1350: * @throws IndexOutOfBoundsException if {@code offset < 0}
> 1351: * @throws IndexOutOfBoundsException if {@code offset > byteSize() - length}
Suggestion:
* @throws IndexOutOfBoundsException if {@code offset > byteSize() - byteLength}
I assume this was missed?
src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 1356:
> 1354: * @throws WrongThreadException if this method is called from a thread {@code T},
> 1355: * such that {@code isAccessibleBy(T) == false}
> 1356: * @throws IllegalArgumentException if {@code length < 0}
Suggestion:
* @throws IllegalArgumentException if {@code byteLength < 0}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/28043#discussion_r2679311350
PR Review Comment: https://git.openjdk.org/jdk/pull/28043#discussion_r2679311392
More information about the core-libs-dev
mailing list