[foreign-memaccess+abi] RFR: 8310362: Improve composability of handle derived from layouts
Jorn Vernee
jvernee at openjdk.org
Wed Jun 21 12:12:48 UTC 2023
On Tue, 20 Jun 2023 12:49:41 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:
> Me and Maurizio have been discussing how to better address certain use cases like accessing structs with variable length array members, or matrices that have a dynamic row and column size.
>
> The solution we came up with is captured in this patch. It adds an additional base offset parameter to the handles produced by `MemoryLayout::varHandle`, `MemoryLayout::sliceHandle` and `MemoryLayout::byteOffsetHandle`. This allows these handles to be composed with other offset computations that are not necessarily derived from layouts.
>
> This patch also adds a `scale` method to MemoryLayout, which can be used to scale and index by the size of a layout. Essentially: `offset + layout.byteSize() * index`. This is useful for expressing ad-hoc offset computations. A `scaleHandle` method is also added for convience, which returns a MethodHandle for `scale` bound to the receiver layout.
>
> Both these changes allowed for several simplifications:
> - `MethodHandles::memorySegmentViewVarHandle` can be removed, as it's now exactly the same as calling ML::varHandle on a ValueLayout with an empty layout path. (the former method also didn't take advantage of the access handle cache found on ValueLayouts).
> - `ValueLayout::arrayHandle` is removed, as most use cases can now simply be expressed using ML::varHandle + ML::scaleHandle. e.g.:
>
> MethodHandles.collectCoordinates(layout.varHandle(),
> 1, MethodHandles.insertArguments(layout.scaleHandle(), 0, 0L));
src/java.base/share/classes/jdk/internal/foreign/Utils.java line 110:
> 108: if (handle != null) {
> 109: return handle;
> 110: }
Added a cache lookup here so that we don't end up re-creating the var handle if one is already in the cache. Also, I noticed that previously the cache was sensitive to layout names, which it shouldn't I think, since the names are not of consequence to the produced var handle.
src/java.base/share/classes/jdk/internal/foreign/abi/SharedUtils.java line 428:
> 426: }
> 427: }
> 428:
Saw this dead code was still here, so I've removed it.
src/java.base/share/classes/jdk/internal/foreign/layout/AbstractLayout.java line 58:
> 56: @SuppressWarnings("unchecked")
> 57: public final L withoutName() {
> 58: return name.isPresent() ? dup(byteAlignment(), Optional.empty()) : (L) this;
Made this idempotent to make the `withoutName()` call in `makeSegmentViewVarHandle` cheaper.
-------------
PR Review Comment: https://git.openjdk.org/panama-foreign/pull/840#discussion_r1235223824
PR Review Comment: https://git.openjdk.org/panama-foreign/pull/840#discussion_r1235224486
PR Review Comment: https://git.openjdk.org/panama-foreign/pull/840#discussion_r1235225987
More information about the panama-dev
mailing list