diff --git a/src/java.base/share/classes/jdk/internal/foreign/Utils.java b/src/java.base/share/classes/jdk/internal/foreign/Utils.java index fb321fbc61240..d5fe693d311ff 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/Utils.java +++ b/src/java.base/share/classes/jdk/internal/foreign/Utils.java @@ -37,6 +37,8 @@ import java.lang.invoke.VarHandle; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; import jdk.internal.access.SharedSecrets; @@ -88,6 +90,24 @@ public static MemorySegment alignUp(MemorySegment ms, long alignment) { } public static VarHandle makeSegmentViewVarHandle(ValueLayout layout, boolean nested) { + final class VarHandleCache { + private static final Map HANDLE_MAP_TOPLEVEL = new ConcurrentHashMap<>(); + private static final Map HANDLE_MAP_NESTED = new ConcurrentHashMap<>(); + } + return nested ? + VarHandleCache.HANDLE_MAP_NESTED.computeIfAbsent(layout.withoutName(), Utils::makeNestedSegmentViewVarHandle) : + VarHandleCache.HANDLE_MAP_TOPLEVEL.computeIfAbsent(layout.withoutName(), Utils::makeToplevelSegmentViewVarHandle); + } + + private static VarHandle makeToplevelSegmentViewVarHandle(ValueLayout layout) { + return makeSegmentViewVarHandleInternal(layout, false); + } + + private static VarHandle makeNestedSegmentViewVarHandle(ValueLayout layout) { + return makeSegmentViewVarHandleInternal(layout, true); + } + + private static VarHandle makeSegmentViewVarHandleInternal(ValueLayout layout, boolean nested) { Class baseCarrier = layout.carrier(); if (layout.carrier() == MemorySegment.class) { baseCarrier = switch ((int) ValueLayout.ADDRESS.byteSize()) { diff --git a/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java b/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java index 275c6c0eccfdf..1098c2e9a8e4e 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java +++ b/src/java.base/share/classes/jdk/internal/foreign/layout/ValueLayouts.java @@ -160,13 +160,9 @@ static boolean isValidCarrier(Class carrier) { @ForceInline public final VarHandle varHandle() { - final class VarHandleCache { - private static final Map HANDLE_MAP = new ConcurrentHashMap<>(); - } if (handle == null) { // this store to stable field is safe, because return value of 'makeMemoryAccessVarHandle' has stable identity - handle = VarHandleCache.HANDLE_MAP - .computeIfAbsent(withoutName(), l -> Utils.makeSegmentViewVarHandle(l, false)); + handle = Utils.makeSegmentViewVarHandle((ValueLayout) this, false); } return handle; }