diff --git a/core/src/main/java/art/arcane/iris/generation/runtime/ThreadSlots.java b/core/src/main/java/art/arcane/iris/generation/runtime/ThreadSlots.java index 0b91a92aa..9c9cd6786 100644 --- a/core/src/main/java/art/arcane/iris/generation/runtime/ThreadSlots.java +++ b/core/src/main/java/art/arcane/iris/generation/runtime/ThreadSlots.java @@ -29,6 +29,12 @@ import java.util.function.Supplier; * while the pooled workers that filled them stay alive. A live thread never loses its value: a thread whose probed * slots all belong to other live threads keeps its value in an overflow map, which drops dead threads whenever it * takes a new one. The size therefore follows the live threads and not the worker churn. + *

+ * Owners and values sit in parallel arrays so the common lookup, a thread in its home slot, is one owner compare and + * one value read that do not depend on each other. Only the owning thread writes a slot's value, and a slot changes + * owner only once its previous owner removed its value or died, so an owner match always pairs with that owner's + * value. Until some thread has had to store away from its home slot, a thread missing from its home slot has no + * value at all, so unbound threads skip the probe too. */ public final class ThreadSlots { private static final int SLOT_COUNT = 256; @@ -36,9 +42,11 @@ public final class ThreadSlots { private static final int PROBE_LIMIT = 8; private static final int NOT_FOUND = -1; - private final AtomicReferenceArray> slots = new AtomicReferenceArray<>(SLOT_COUNT); + private final AtomicReferenceArray owners = new AtomicReferenceArray<>(SLOT_COUNT); + private final AtomicReferenceArray values = new AtomicReferenceArray<>(SLOT_COUNT); private final ConcurrentHashMap overflow = new ConcurrentHashMap<>(); private final Supplier factory; + private volatile boolean storedAwayFromHome; /** A table whose threads each get a value from the factory on first use. */ public ThreadSlots(Supplier factory) { @@ -57,13 +65,13 @@ public final class ThreadSlots { /** This thread's value, created by the factory on first use; null for an unbound thread of a bindings table. */ public T get() { Thread thread = Thread.currentThread(); - int index = find(thread); - if (index != NOT_FOUND) { - return slots.getPlain(index).value(); + int home = home(thread); + if (owners.getPlain(home) == thread) { + return values.getPlain(home); } - T overflowed = overflow.isEmpty() ? null : overflow.get(thread); - if (overflowed != null || factory == null) { - return overflowed; + T awayFromHome = storedAwayFromHome ? getAwayFromHome(thread) : null; + if (awayFromHome != null || factory == null) { + return awayFromHome; } T created = factory.get(); store(thread, created); @@ -78,7 +86,7 @@ public final class ThreadSlots { Thread thread = Thread.currentThread(); int index = find(thread); if (index != NOT_FOUND) { - slots.set(index, new Slot<>(thread, value)); + values.set(index, value); return; } if (overflow.replace(thread, value) == null) { @@ -90,7 +98,8 @@ public final class ThreadSlots { Thread thread = Thread.currentThread(); int index = find(thread); if (index != NOT_FOUND) { - slots.set(index, null); + values.set(index, null); + owners.set(index, null); return; } if (!overflow.isEmpty()) { @@ -98,12 +107,19 @@ public final class ThreadSlots { } } + private T getAwayFromHome(Thread thread) { + int index = find(thread); + if (index != NOT_FOUND) { + return values.getPlain(index); + } + return overflow.isEmpty() ? null : overflow.get(thread); + } + private int find(Thread thread) { int home = home(thread); for (int probe = 0; probe < PROBE_LIMIT; probe++) { int index = (home + probe) & SLOT_MASK; - Slot slot = slots.getPlain(index); - if (slot != null && slot.owner() == thread) { + if (owners.getPlain(index) == thread) { return index; } } @@ -111,15 +127,19 @@ public final class ThreadSlots { } private void store(Thread thread, T value) { - Slot created = new Slot<>(thread, value); int home = home(thread); for (int probe = 0; probe < PROBE_LIMIT; probe++) { int index = (home + probe) & SLOT_MASK; - Slot current = slots.get(index); - if ((current == null || !current.owner().isAlive()) && slots.compareAndSet(index, current, created)) { + Thread current = owners.get(index); + if ((current == null || !current.isAlive()) && owners.compareAndSet(index, current, thread)) { + values.set(index, value); + if (probe > 0) { + storedAwayFromHome = true; + } return; } } + storedAwayFromHome = true; overflow.keySet().removeIf(owner -> !owner.isAlive()); overflow.put(thread, value); } @@ -127,7 +147,4 @@ public final class ThreadSlots { private static int home(Thread thread) { return (int) thread.threadId() & SLOT_MASK; } - - private record Slot(Thread owner, T value) { - } } diff --git a/core/src/test/java/art/arcane/iris/generation/runtime/ThreadSlotsTest.java b/core/src/test/java/art/arcane/iris/generation/runtime/ThreadSlotsTest.java index 2200cd61d..ef808301a 100644 --- a/core/src/test/java/art/arcane/iris/generation/runtime/ThreadSlotsTest.java +++ b/core/src/test/java/art/arcane/iris/generation/runtime/ThreadSlotsTest.java @@ -121,7 +121,7 @@ public class ThreadSlotsTest { } private static int slotCount(ThreadSlots slots) throws ReflectiveOperationException { - Field field = ThreadSlots.class.getDeclaredField("slots"); + Field field = ThreadSlots.class.getDeclaredField("owners"); field.setAccessible(true); return ((AtomicReferenceArray) field.get(slots)).length(); }