[foreign-memaccess+abi] RFR: 8274157: java.foreign: Add method MemorySegment::isOverlapping

Lee Rhodes github.com+12941506+leerho at openjdk.java.net
Tue Sep 28 23:10:24 UTC 2021


On Tue, 28 Sep 2021 22:26:20 GMT, Lee Rhodes <github.com+12941506+leerho at openjdk.org> wrote:

>> 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.

If you are reluctant to return actual addresses, you could return the 4 values as relative offsets where either A_lo == 0, or B_lo == 0, or A_lo == B_lo == 0.

I thought I saw in one of your refactorings where you eliminated both MemoryAccess and MemoryCopy, a new general purpose copy(seg A, LayoutA, offsetA, seg B, LayoutB, offsetB, length) or something like that (I can't find it at this moment).  If the layouts can describe repeating elements of any size, doesn't this solve the copy problem and possibly eliminate the need for the isOverlapping() method?

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

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


More information about the panama-dev mailing list