From 12404165a9e7ff5142d17fb52c712a425ee0125a Mon Sep 17 00:00:00 2001 From: liach Date: Thu, 21 Sep 2023 16:44:49 +0800 Subject: [PATCH] byte array var handles can share a common base class --- .../classes/java/lang/invoke/VarHandle.java | 7 +--- .../lang/invoke/VarHandleByteArrayBase.java | 34 ++++++++++++++++++- .../classes/java/lang/invoke/VarHandles.java | 9 ----- .../X-VarHandleByteArrayView.java.template | 32 ++++------------- 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandle.java b/src/java.base/share/classes/java/lang/invoke/VarHandle.java index f5fae207b93a9..6f792918a2974 100644 --- a/src/java.base/share/classes/java/lang/invoke/VarHandle.java +++ b/src/java.base/share/classes/java/lang/invoke/VarHandle.java @@ -474,12 +474,7 @@ public abstract sealed class VarHandle implements Constable permits IndirectVarHandle, LazyInitializingVarHandle, VarHandleSegmentViewBase, - VarHandleByteArrayAsChars.ByteArrayViewVarHandle, - VarHandleByteArrayAsDoubles.ByteArrayViewVarHandle, - VarHandleByteArrayAsFloats.ByteArrayViewVarHandle, - VarHandleByteArrayAsInts.ByteArrayViewVarHandle, - VarHandleByteArrayAsLongs.ByteArrayViewVarHandle, - VarHandleByteArrayAsShorts.ByteArrayViewVarHandle, + VarHandleByteArrayBase, VarHandleBooleans.Array, VarHandleBooleans.FieldInstanceReadOnly, VarHandleBooleans.FieldStaticReadOnly, diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandleByteArrayBase.java b/src/java.base/share/classes/java/lang/invoke/VarHandleByteArrayBase.java index 34f582e39e797..225567ce27722 100644 --- a/src/java.base/share/classes/java/lang/invoke/VarHandleByteArrayBase.java +++ b/src/java.base/share/classes/java/lang/invoke/VarHandleByteArrayBase.java @@ -26,7 +26,13 @@ import jdk.internal.access.JavaNioAccess; import jdk.internal.access.SharedSecrets; +import jdk.internal.foreign.AbstractMemorySegmentImpl; +import jdk.internal.foreign.MemorySessionImpl; import jdk.internal.misc.ScopedMemoryAccess; +import jdk.internal.vm.annotation.ForceInline; + +import java.lang.foreign.MemorySegment; +import java.nio.ByteBuffer; import static java.lang.invoke.MethodHandleStatics.UNSAFE; @@ -34,7 +40,19 @@ * The base class for generated byte array and byte buffer view * implementations */ -abstract class VarHandleByteArrayBase { +abstract sealed class VarHandleByteArrayBase extends VarHandle + permits VarHandleByteArrayAsChars.ArrayHandle, + VarHandleByteArrayAsChars.ByteBufferHandle, + VarHandleByteArrayAsDoubles.ArrayHandle, + VarHandleByteArrayAsDoubles.ByteBufferHandle, + VarHandleByteArrayAsFloats.ArrayHandle, + VarHandleByteArrayAsFloats.ByteBufferHandle, + VarHandleByteArrayAsInts.ArrayHandle, + VarHandleByteArrayAsInts.ByteBufferHandle, + VarHandleByteArrayAsLongs.ArrayHandle, + VarHandleByteArrayAsLongs.ByteBufferHandle, + VarHandleByteArrayAsShorts.ArrayHandle, + VarHandleByteArrayAsShorts.ByteBufferHandle { static final boolean BE = UNSAFE.isBigEndian(); @@ -42,6 +60,20 @@ abstract class VarHandleByteArrayBase { static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess(); + final boolean be; + + VarHandleByteArrayBase(VarForm form, boolean be, boolean exact) { + super(form, exact); + this.be = be; + } + + @ForceInline + static MemorySessionImpl session(ByteBuffer bb) { + MemorySegment segment = NIO_ACCESS.bufferSegment(bb); + return segment != null ? + ((AbstractMemorySegmentImpl)segment).sessionImpl() : null; + } + static IllegalStateException newIllegalStateExceptionForMisalignedAccess(int index) { return new IllegalStateException("Misaligned access at index: " + index); } diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandles.java b/src/java.base/share/classes/java/lang/invoke/VarHandles.java index c0db5ddb866c1..10eac7ea7f157 100644 --- a/src/java.base/share/classes/java/lang/invoke/VarHandles.java +++ b/src/java.base/share/classes/java/lang/invoke/VarHandles.java @@ -36,8 +36,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import java.util.stream.Stream; import static java.lang.invoke.MethodHandleStatics.UNSAFE; @@ -46,13 +44,6 @@ final class VarHandles { - static ClassValue> ADDRESS_FACTORIES = new ClassValue<>() { - @Override - protected ConcurrentMap computeValue(Class type) { - return new ConcurrentHashMap<>(); - } - }; - static VarHandle makeFieldHandle(MemberName f, Class refc, boolean isWriteAllowedOnFinalFields) { if (!f.isStatic()) { long foffset = MethodHandleNatives.objectFieldOffset(f); diff --git a/src/java.base/share/classes/java/lang/invoke/X-VarHandleByteArrayView.java.template b/src/java.base/share/classes/java/lang/invoke/X-VarHandleByteArrayView.java.template index a75e693f9bd61..1c93974690c18 100644 --- a/src/java.base/share/classes/java/lang/invoke/X-VarHandleByteArrayView.java.template +++ b/src/java.base/share/classes/java/lang/invoke/X-VarHandleByteArrayView.java.template @@ -24,13 +24,10 @@ */ package java.lang.invoke; -import jdk.internal.foreign.AbstractMemorySegmentImpl; -import jdk.internal.foreign.MemorySessionImpl; import jdk.internal.misc.Unsafe; import jdk.internal.util.Preconditions; import jdk.internal.vm.annotation.ForceInline; -import java.lang.foreign.MemorySegment; import java.nio.ByteBuffer; import java.nio.ReadOnlyBufferException; import java.util.Objects; @@ -39,7 +36,7 @@ import static java.lang.invoke.MethodHandleStatics.UNSAFE; #warn -final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase { +final class VarHandleByteArrayAs$Type$s { static final int ALIGN = $BoxType$.BYTES - 1; @@ -47,32 +44,22 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase { @ForceInline static $rawType$ convEndian(boolean big, $type$ v) { $rawType$ rv = $Type$.$type$ToRaw$RawType$Bits(v); - return big == BE ? rv : $RawBoxType$.reverseBytes(rv); + return big == VarHandleByteArrayBase.BE ? rv : $RawBoxType$.reverseBytes(rv); } @ForceInline static $type$ convEndian(boolean big, $rawType$ rv) { - rv = big == BE ? rv : $RawBoxType$.reverseBytes(rv); + rv = big == VarHandleByteArrayBase.BE ? rv : $RawBoxType$.reverseBytes(rv); return $Type$.$rawType$BitsTo$Type$(rv); } #else[floatingPoint] @ForceInline static $type$ convEndian(boolean big, $type$ n) { - return big == BE ? n : $BoxType$.reverseBytes(n); + return big == VarHandleByteArrayBase.BE ? n : $BoxType$.reverseBytes(n); } #end[floatingPoint] - - static abstract sealed class ByteArrayViewVarHandle extends VarHandle { - final boolean be; - - ByteArrayViewVarHandle(VarForm form, boolean be, boolean exact) { - super(form, exact); - this.be = be; - } - } - - static final class ArrayHandle extends ByteArrayViewVarHandle { + static final class ArrayHandle extends VarHandleByteArrayBase { ArrayHandle(boolean be) { this(be, false); @@ -568,7 +555,7 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase { } - static final class ByteBufferHandle extends ByteArrayViewVarHandle { + static final class ByteBufferHandle extends VarHandleByteArrayBase { ByteBufferHandle(boolean be) { this(be, false); @@ -602,13 +589,6 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase { return Preconditions.checkIndex(index, bb.limit() - ALIGN, null); } - @ForceInline - static MemorySessionImpl session(ByteBuffer bb) { - MemorySegment segment = NIO_ACCESS.bufferSegment(bb); - return segment != null ? - ((AbstractMemorySegmentImpl)segment).sessionImpl() : null; - } - @ForceInline static int indexRO(ByteBuffer bb, int index) { if (bb.isReadOnly())