Add BitHeaderPoolPerformancePatch for chunk serialization improvements in Build 42.21. Introduce PerThreadDeque for thread-local pooling with reduced contention and memory overhead. Include unit tests and integrate patch into the performance patch set.

This commit is contained in:
Overlord committed 2026-10-05 00:47:01 +02:00
1 parent 2081235433
commit eb09a77941
5 files changed
+352 -1

No files matched your search

@@ -0,0 +1,49 @@
package dev.overlord.zomboidforge.compat.b42.util;
import org.jspecify.annotations.Nullable;
import java.io.Serial;
import java.util.ArrayDeque;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedDeque;
/**
* An object pool that keeps one {@link ArrayDeque} per thread behind the
* {@link ConcurrentLinkedDeque} methods {@link #poll()}, {@link #offer} and {@link #size()}:
* a thread polls what it offered, without synchronisation and without the node that
* {@code ConcurrentLinkedDeque} allocates per offer. Every other method works on the (empty)
* shared deque, so it suits only callers that use these three.
*
* <p>A pool keeps at most {@value #CAPACITY} objects per thread; further offers are dropped
* and the objects left to the garbage collector. Objects taken on one thread and returned on
* another end up in the second thread's pool, up to that limit.
*/
public final class PerThreadDeque<E> extends ConcurrentLinkedDeque<E>
{
@Serial
private static final long serialVersionUID = 1L;
private static final int CAPACITY = 1024;
private final transient ThreadLocal<ArrayDeque<E>> pools = ThreadLocal.withInitial(ArrayDeque::new);
/** The calling thread's most recently offered object, or {@code null} when its pool is empty. */
@Override
public @Nullable E poll() { return this.pools.get().pollLast(); }
/** Adds {@code element} to the calling thread's pool unless it is full. Always {@code true}, as the shared deque. */
@Override
public boolean offer(E element)
{
Objects.requireNonNull(element);
ArrayDeque<E> pool = this.pools.get();
if (pool.size() < PerThreadDeque.CAPACITY) pool.addLast(element);
return true;
}
/** The size of the calling thread's pool. */
@Override
public int size() { return this.pools.get().size(); }
}
@@ -0,0 +1,70 @@
package dev.overlord.zomboidforge.compat.b42.util;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PerThreadDequeTest
{
@Test
void pollReturnsWhatThisThreadOfferedNewestFirst()
{
PerThreadDeque<String> pool = new PerThreadDeque<>();
String first = "first";
String second = "second";
assertTrue(pool.offer(first));
assertTrue(pool.offer(second));
assertSame(second, pool.poll());
assertSame(first, pool.poll());
assertNull(pool.poll());
}
@Test
void threadsDoNotSeeEachOthersObjects() throws InterruptedException
{
PerThreadDeque<String> pool = new PerThreadDeque<>();
pool.offer("main");
AtomicReference<String> polled = new AtomicReference<>("not polled");
AtomicReference<Integer> size = new AtomicReference<>(-1);
Thread other = new Thread(() -> {
polled.set(pool.poll());
pool.offer("other");
size.set(pool.size());
});
other.start();
other.join();
assertNull(polled.get());
assertEquals(1, size.get());
assertEquals("main", pool.poll());
assertNull(pool.poll());
}
@Test
void keepsAtMostTheCapacityPerThread()
{
PerThreadDeque<Integer> pool = new PerThreadDeque<>();
for (int i = 0; i < 2_000; i++) assertTrue(pool.offer(i));
assertEquals(1_024, pool.size());
assertEquals(1_023, pool.poll());
}
@Test
void rejectsNullLikeConcurrentLinkedDeque()
{
PerThreadDeque<String> pool = new PerThreadDeque<>();
assertThrows(NullPointerException.class, () -> pool.offer(null));
}
}