[foreign-memaccess+abi] RFR: 8301801: Implement arena-centric API [v4]

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Feb 10 14:05:21 UTC 2023


On Fri, 10 Feb 2023 11:15:58 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> src/java.base/share/classes/jdk/internal/foreign/MemorySessionImpl.java line 63:
>> 
>>> 61: 
>>> 62:     public static final MemorySessionImpl GLOBAL = new GlobalSession(null, false);
>>> 63:     public static final MemorySessionImpl NATIVE = new GlobalSession(null, true);
>> 
>> I don't get why this distinction is needed. GlobalSession has an isInternal() predicate but I don't see it called anywhere.
>> 
>> Could you explain why we need 2 kinds of global session?
>
> We no longer need `isInternal`. But we do need, I think, another session for native segments returned by linker, so that comparisons against global arena's scope would fail.

I have uploaded a new version which vastly improves the situation when it comes to specifying the lifetime of segments associated with external resources (raw native segments, array segments, buffer segments). Now these segments are specified to be associated with a *fresh* scope, which is always alive. This means that the scope of these segment is not equal to any other scope from any other segment (except for the scope of the slices derived from these segments, which of course share the same scope).

Implementation-wise, I played some tricks to avoid unnecessary creation of memory session objects when creating segments backed by external resources. Now all segments have a "parent" (the segment from which they have been derived), which can be `null` (if the segment is the "root"). When we ask for a segment scope, if the segment session is a real session, we just return that. If the session is the "external" session singleton, we return a new scope which wraps the segment and whose equality is defined in terms of the root of the wrapped segment. This means that we only pay for a scope creation when the scope is accessed on an externally managed segment (this operation should be rather infrequent). In all the other cases, we just return the segment session (which also acts as a scope).

I have also clarified the javadoc to specify the places in which a _new_ scope is created. Note that these were mostly loose ends from previous iterations of the API - which this change finally addresses.

(an alternative we have considered was to make the scope accessor optional in MemorySegment. While that is possible, I think an API where all segments have some scope, which could be an arena scope or some other synthetic scope, leads to more uniform usage).

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

PR: https://git.openjdk.org/panama-foreign/pull/781


More information about the panama-dev mailing list