Read thread slots with one owner check

- Keep slot owners and values in parallel arrays so a thread in its home slot reads its value without a probe
- Skip the probe and overflow lookup until some thread has stored away from its home slot
This commit is contained in:
piexel committed 2026-11-27 00:48:50 +01:00
1 parent e0ad0b3ea9
commit f630996c22
2 files changed
+35 -18

No files matched your search

@@ -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.
* <p>
* 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<T> {
private static final int SLOT_COUNT = 256;
@@ -36,9 +42,11 @@ public final class ThreadSlots<T> {
private static final int PROBE_LIMIT = 8;
private static final int NOT_FOUND = -1;
private final AtomicReferenceArray<Slot<T>> slots = new AtomicReferenceArray<>(SLOT_COUNT);
private final AtomicReferenceArray<Thread> owners = new AtomicReferenceArray<>(SLOT_COUNT);
private final AtomicReferenceArray<T> values = new AtomicReferenceArray<>(SLOT_COUNT);
private final ConcurrentHashMap<Thread, T> overflow = new ConcurrentHashMap<>();
private final Supplier<T> factory;
private volatile boolean storedAwayFromHome;
/** A table whose threads each get a value from the factory on first use. */
public ThreadSlots(Supplier<T> factory) {
@@ -57,13 +65,13 @@ public final class ThreadSlots<T> {
/** 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<T> {
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<T> {
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<T> {
}
}
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<T> 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<T> {
}
private void store(Thread thread, T value) {
Slot<T> created = new Slot<>(thread, value);
int home = home(thread);
for (int probe = 0; probe < PROBE_LIMIT; probe++) {
int index = (home + probe) & SLOT_MASK;
Slot<T> 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<T> {
private static int home(Thread thread) {
return (int) thread.threadId() & SLOT_MASK;
}
private record Slot<T>(Thread owner, T value) {
}
}
@@ -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();
}