mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 03:31:32 +00:00
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:
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
|
* 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
|
* 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.
|
* 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> {
|
public final class ThreadSlots<T> {
|
||||||
private static final int SLOT_COUNT = 256;
|
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 PROBE_LIMIT = 8;
|
||||||
private static final int NOT_FOUND = -1;
|
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 ConcurrentHashMap<Thread, T> overflow = new ConcurrentHashMap<>();
|
||||||
private final Supplier<T> factory;
|
private final Supplier<T> factory;
|
||||||
|
private volatile boolean storedAwayFromHome;
|
||||||
|
|
||||||
/** A table whose threads each get a value from the factory on first use. */
|
/** A table whose threads each get a value from the factory on first use. */
|
||||||
public ThreadSlots(Supplier<T> factory) {
|
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. */
|
/** This thread's value, created by the factory on first use; null for an unbound thread of a bindings table. */
|
||||||
public T get() {
|
public T get() {
|
||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
int index = find(thread);
|
int home = home(thread);
|
||||||
if (index != NOT_FOUND) {
|
if (owners.getPlain(home) == thread) {
|
||||||
return slots.getPlain(index).value();
|
return values.getPlain(home);
|
||||||
}
|
}
|
||||||
T overflowed = overflow.isEmpty() ? null : overflow.get(thread);
|
T awayFromHome = storedAwayFromHome ? getAwayFromHome(thread) : null;
|
||||||
if (overflowed != null || factory == null) {
|
if (awayFromHome != null || factory == null) {
|
||||||
return overflowed;
|
return awayFromHome;
|
||||||
}
|
}
|
||||||
T created = factory.get();
|
T created = factory.get();
|
||||||
store(thread, created);
|
store(thread, created);
|
||||||
@@ -78,7 +86,7 @@ public final class ThreadSlots<T> {
|
|||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
int index = find(thread);
|
int index = find(thread);
|
||||||
if (index != NOT_FOUND) {
|
if (index != NOT_FOUND) {
|
||||||
slots.set(index, new Slot<>(thread, value));
|
values.set(index, value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (overflow.replace(thread, value) == null) {
|
if (overflow.replace(thread, value) == null) {
|
||||||
@@ -90,7 +98,8 @@ public final class ThreadSlots<T> {
|
|||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
int index = find(thread);
|
int index = find(thread);
|
||||||
if (index != NOT_FOUND) {
|
if (index != NOT_FOUND) {
|
||||||
slots.set(index, null);
|
values.set(index, null);
|
||||||
|
owners.set(index, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!overflow.isEmpty()) {
|
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) {
|
private int find(Thread thread) {
|
||||||
int home = home(thread);
|
int home = home(thread);
|
||||||
for (int probe = 0; probe < PROBE_LIMIT; probe++) {
|
for (int probe = 0; probe < PROBE_LIMIT; probe++) {
|
||||||
int index = (home + probe) & SLOT_MASK;
|
int index = (home + probe) & SLOT_MASK;
|
||||||
Slot<T> slot = slots.getPlain(index);
|
if (owners.getPlain(index) == thread) {
|
||||||
if (slot != null && slot.owner() == thread) {
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,15 +127,19 @@ public final class ThreadSlots<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void store(Thread thread, T value) {
|
private void store(Thread thread, T value) {
|
||||||
Slot<T> created = new Slot<>(thread, value);
|
|
||||||
int home = home(thread);
|
int home = home(thread);
|
||||||
for (int probe = 0; probe < PROBE_LIMIT; probe++) {
|
for (int probe = 0; probe < PROBE_LIMIT; probe++) {
|
||||||
int index = (home + probe) & SLOT_MASK;
|
int index = (home + probe) & SLOT_MASK;
|
||||||
Slot<T> current = slots.get(index);
|
Thread current = owners.get(index);
|
||||||
if ((current == null || !current.owner().isAlive()) && slots.compareAndSet(index, current, created)) {
|
if ((current == null || !current.isAlive()) && owners.compareAndSet(index, current, thread)) {
|
||||||
|
values.set(index, value);
|
||||||
|
if (probe > 0) {
|
||||||
|
storedAwayFromHome = true;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
storedAwayFromHome = true;
|
||||||
overflow.keySet().removeIf(owner -> !owner.isAlive());
|
overflow.keySet().removeIf(owner -> !owner.isAlive());
|
||||||
overflow.put(thread, value);
|
overflow.put(thread, value);
|
||||||
}
|
}
|
||||||
@@ -127,7 +147,4 @@ public final class ThreadSlots<T> {
|
|||||||
private static int home(Thread thread) {
|
private static int home(Thread thread) {
|
||||||
return (int) thread.threadId() & SLOT_MASK;
|
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 {
|
private static int slotCount(ThreadSlots<?> slots) throws ReflectiveOperationException {
|
||||||
Field field = ThreadSlots.class.getDeclaredField("slots");
|
Field field = ThreadSlots.class.getDeclaredField("owners");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
return ((AtomicReferenceArray<?>) field.get(slots)).length();
|
return ((AtomicReferenceArray<?>) field.get(slots)).length();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user