[foreign-memaccess+abi] RFR: 8264933: Improve stream support in memory segments
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Fri Apr 9 13:48:21 UTC 2021
On Fri, 9 Apr 2021 13:30:00 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>>> 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.
>
>> This is true when using VarHandle too, as a user you have to use the right segment.
>
> Yes! Which is why having a memory segment slice that give you access only to the portion of data you want is the right way to approach this:
>
> segment.addresses(AN_INT)
> .mapToInt(MemoryAccess::getInt)
> .sum();
>
> I don't question that, in some cases, having a stream of offsets would be handy - but the goal of this PR is mostly to avoid what the API forces you to do today: `StreamSupport.stream(segment.spliterator(LAYOUT), true/false)`. Surely we can add other accessors in the future?
>
> Is it possible we are quibbling over naming? E.g. perhaps instead of calling this new method `MemorySegment::stream` and suggesting that this is _the way_ to view a segment as a stream, perhaps calling it "slices" would be more honest? I'm open to that, although that needs some way to create a parallel stream too (`MemorySegment::slices` looks good, `MemorySegment::asParallelSlices` not so much ;-) )
Proposal: instead of what we do in this PR, let's tweak things a little:
* for usability, let's add `MemorySegment::slices(MemoryLayout)`, which returns a _sequential_ stream of all the slices in a segment (with given element layout).
* for advanced use cases, including parallel streams, users will have to use spliterator + StreamSupport, as before.
This doesn't close the door to other stream-ified views to be added in the future. What do people think?
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/494
More information about the panama-dev
mailing list