compose MemorySegments
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Jun 10 21:29:27 UTC 2021
Hi Douglas
I think the point you raise re. compisition is a valid one, but I'm also
afraid that what you are looking for is higher-level than what memory
segments have been created for.
A MemorySegment models a _contiguous_ memory region, with a start and an
end. Then we have dereference primitives that allow you to peek and poke
at this contiguous memory region. A memory dereference operation is
designed to be fast - e.g. it should boil down to, ideally, a single
machine instruction (or better, a SIMD instruction for vectorized loops).
I get that, in your case, you want to work with a superposition of
multiple segments - and have dereference operations "just work"
(probably by dynamically selecting the right underlying segment based on
the offset). But, your case is also pretty distant from what the API was
designed for. While it's technically possible for the API to create a
"multi-segment", the performance model of these things would be so
different from that of plain segments that it will just be confusing. A
bit like asking for an API which allowed us to combine Java arrays into
a single logical array which, in reality is scattered in multiple places
- and then passing these arrays around to clients which expects just
"plain arrays".
So, I'm afraid that the thing you identify as "missing" is kind of
missing _by design_: what you describe is simply not a segment, in the
way the API describes what a segment is (sorry!).
On 10/06/2021 20:38, Douglas Surber wrote:
> I do have a question on the toXxxArray methods. How is endianness accounted for? The data I get from the network may have a different endianness from the platform.
Good point - for now toXyzArray methods just use native endianness.
We are in the process of adding other ways to bulk copy data from arrays
into segments and viceversa (see
https://github.com/openjdk/panama-foreign/pull/555), I'm sure there's
something in there that you could use to enforce right endianness.
>
> Also, it would be convenient to add methods such as getInt to MemorySegment analogous to toIntArray. I know this can be done with a VarHandle but getting a single primitive value is likely to be such a common use case that encapsulating it into a method would be helpful.
Look at the MemoryAccess class. That class contains a lot of static
methods which remove the need, for simple cases, to tinker with var handles.
Maurizio
>
> Douglas
>
>
More information about the panama-dev
mailing list