[foreign-memaccess+abi] RFR: 8274157: java.foreign: Add method MemorySegment::isOverlapping
Lee Rhodes
github.com+12941506+leerho at openjdk.java.net
Tue Sep 28 22:30:03 UTC 2021
On Tue, 28 Sep 2021 14:01:59 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> This change adds a method to the MemorySegment API that detects if two segments are overlapping.
>
> src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java line 324:
>
>> 322:
>> 323: /**
>> 324: * Returns true if this segment overlaps the {@code other} segment.
>
> We should probably explain what "overlaps" mean. E.g. "Two segments S1 and S2 are said to overlap if it is possible to find at least two slices L1 (from S1) and L2 (from S2) such that L1 and L2 are backed by the same memory region".
>
> This description is vague enough (as it is defined in terms of slicing). At the same time it makes it clear as to why an heap segment can never overlap with an heap segment (and vice-versa).
Returning the slice of the overlapped region is an interesting idea.
I was trying to think if this would be useful in computing intersections between A and B, but alas, the circumstances where this would be true are highly specialized. In the general case, if the returned slice is != null, then the lower bound of the size of the intersection is the size of the slice. But if it is null, we know absolutely nothing. So for intersections anyway, this is not too useful.
The use-case I originally had in mind was copying non-primitive elements from A into B. To do this efficiently, without requiring extra copying space requires that we know if there is an overlap and knowing whether A_lo <= B_lo, or B_lo < A_lo (by address), or if A is a subset of B. Knowing that there exists an overlap is a big help, but to answer the rest we would need to obtain the start and end addresses of both A and B.
In my mind, what would be more useful than returning the overlapping slice, would be to return sufficient information so that an efficient copy from A to B could proceed immediately. For example, returning a small array of 4 addresses: {A_lo, A_hi, B_lo, B_hi}. This provides all the info we need for all different types of copies and moves.
Cheers,
Lee.
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/585
More information about the panama-dev
mailing list