RFR: 8350118: Simplify the layout access VarHandle
Maurizio Cimadamore
mcimadamore at openjdk.org
Fri Feb 21 10:07:52 UTC 2025
On Fri, 21 Feb 2025 00:01:36 GMT, Chen Liang <liach at openjdk.org> wrote:
> Simplify the layout access var handles to be direct in some common cases. Also made `VarHandle::isAccessModeSupported` report if an access mode is supported for a VH.
>
> Reduces the instructions to execute this code in a simple main by 47%:
>
> long[] arr = new long[8];
> var ms = MemorySegment.ofArray(arr);
> ms.setAtIndex(ValueLayout.JAVA_BYTE, 12, (byte) 3);
>
>
> Main overheads in FFM are identified to be:
> 1. Eager initialization of direct MethodHandle; can be CDS archived
> 2. MH combinator forms via LambdaFormEditor, not cached right now and always have large overhead
>
> Still need other measures to deal with common user patterns of `MethodHandles.insertCoordinates(vh, 1, 0L)` which currently is still very slow.
>
> Tests: 2 unrelated failures on tier 1-3
src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java line 1556:
> 1554:
> 1555: @Override
> 1556: public VarHandle memorySegmentViewHandle(Class<?> carrier, MemoryLayout enclosing, long alignmentMask, ByteOrder order, boolean fixedOffset, long offset) {
When I was playing with the code I kept being confused by the `fixedOffset` parameter name. The reason being that no var handle is really "fixed offset" (all VH take a "base" offset -- and this doesn't change that). What this "fixedOffset" means is really "there's no other (dynamic) offset on top of the base offset". I think calling it "stridedAccess" seems subjectively more evocative (or something like that).
src/java.base/share/classes/java/lang/invoke/VarForm.java line 70:
> 68: VarForm(Class<?> implClass, VarForm methodTypeSource) {
> 69: this.implClass = implClass;
> 70: // methodTypeSource already called initMethodTypes
can we turn this comment into an assertion?
src/java.base/share/classes/java/lang/invoke/VarForm.java line 150:
> 148: String methodName = value.methodName();
> 149: MethodType type = methodType_table[value.at.ordinal()].insertParameterTypes(0, VarHandle.class);
> 150: assert !UNSAFE.shouldBeInitialized(implClass) : implClass;
Thanks - took a long time to figure this one out -- hopefully it will save time in the future ;-)
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/23720#discussion_r1965208913
PR Review Comment: https://git.openjdk.org/jdk/pull/23720#discussion_r1965210069
PR Review Comment: https://git.openjdk.org/jdk/pull/23720#discussion_r1965211558
More information about the build-dev
mailing list