[foreign-memaccess+abi] RFR: 8264933: Improve stream support in memory segments

Rémi Forax github.com+828220+forax at openjdk.java.net
Fri Apr 9 13:05:30 UTC 2021


On Fri, 9 Apr 2021 12:57:21 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

>> This patch improves stream support for memory segments - it does so in two ways:
>> 
>> * first, tweaking the `MemorySegment::spliterator` method to take an *element layout*, instead of a full sequence layout whose size had to match that of the segment.
>> * secondly, by adding convenience methods to create streams directly, w/o needing to call `StreamSupport.stream`. As for `Collection`, two methods are provided: `MemorySegment::stream` and `MemorySegment::parallelStream`.
>> 
>> I've fixed up the documentation in places - I realized that, for example, the javadoc for `MemorySegment::spliterator` was missing a `@throws` clause for when there's a size mismatch between layout and segment.
>> Also, I've removed scope liveness check on the spliterator call - after all, a spliterator is going to make slices - a liveness check will occur when the slice is accessed.
>
> LGTM!

> I see what you mean: instead of returning slices, return offsets, and let the user deal with it. Honestly, I'm not sure - it seems like we are, again, prematurely concluding that slicing is gonna be more expensive, while the benchmarks I have show that the performance we get are the same as with Unsafe.

Yes !
I see no issue with the Spliterator<MemorySegment>, it's the right abstraction and it doesn't seem to cost too much.

But i disagree that a Stream<MemorySegment> is the right abstraction.
The design of this API separates the Segment from the way to access to it, and the user links the two together with offsets.

> Note that we want each returned segment to be disjoint - an offset only tells me the starting point - doesn't tell me how big it is - which means it is possible/easier for clients to make mistake and write on "somebody else's" slice.

This is true when using VarHandle too, as a user you have to use the right segment.

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

PR: https://git.openjdk.java.net/panama-foreign/pull/494


More information about the panama-dev mailing list