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:
1 parent
2081235433
commit
eb09a77941
5 files changed
+352
-1
No files matched your search
+2
-1
@@ -29,7 +29,8 @@ public final class B42PatchSet implements PatchSet
|
||||
new ZomboidFileSystemInitPerformancePatch(),
|
||||
new ServerMapCellBudgetPerformancePatch(),
|
||||
new WorldGenUtilsPlacementPerformancePatch(),
|
||||
new WorldGenChunkRulesPerformancePatch()
|
||||
new WorldGenChunkRulesPerformancePatch(),
|
||||
new BitHeaderPoolPerformancePatch()
|
||||
);
|
||||
|
||||
@Override
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.FieldInsnNode;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.objectweb.asm.tree.TypeInsnNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* Creates the four header pools of {@code zombie.util.io.BitHeader} as {@code PerThreadDeque}s
|
||||
* instead of {@code ConcurrentLinkedDeque}s:
|
||||
*
|
||||
* <pre>{@code
|
||||
* // and short, int, long
|
||||
* private static final ConcurrentLinkedDeque pool_byte = new PerThreadDeque();
|
||||
* }</pre>
|
||||
*
|
||||
* Every square and object a chunk save writes takes a header from a pool and returns it, from the
|
||||
* save worker threads at once; the shared deques were about a third of chunk serialization.
|
||||
* {@code PerThreadDeque} answers {@code poll}, {@code offer} and {@code size}, the only methods
|
||||
* {@code BitHeader} calls on the pools, from a pool per thread. A header is reset when it is
|
||||
* returned and gets its buffer and mode when it is taken, so which header a thread gets does not
|
||||
* change what is written.
|
||||
*/
|
||||
public final class BitHeaderPoolPerformancePatch extends PerformancePatch
|
||||
{
|
||||
private static final String DEQUE = "java/util/concurrent/ConcurrentLinkedDeque";
|
||||
private static final String POOL = "dev/overlord/zomboidforge/compat/b42/util/PerThreadDeque";
|
||||
|
||||
private static final Set<String> POOL_FIELDS = Set.of("pool_byte", "pool_short", "pool_int", "pool_long");
|
||||
|
||||
private static final Anchor.Method ANCHOR = new Anchor.Method("<clinit>", "()V");
|
||||
|
||||
public BitHeaderPoolPerformancePatch() {
|
||||
super("zombie/util/io/BitHeader", BitHeaderPoolPerformancePatch.ANCHOR, null);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node)
|
||||
{
|
||||
MethodNode method = BitHeaderPoolPerformancePatch.ANCHOR.resolve(node);
|
||||
Set<String> replaced = new TreeSet<>();
|
||||
|
||||
for (AbstractInsnNode instruction : method.instructions)
|
||||
{
|
||||
if (!(instruction instanceof TypeInsnNode creation) || creation.getOpcode() != Opcodes.NEW || !creation.desc.equals(BitHeaderPoolPerformancePatch.DEQUE)) continue;
|
||||
|
||||
// new ConcurrentLinkedDeque; dup; invokespecial <init>()V; putstatic pool_*
|
||||
List<AbstractInsnNode> next = BitHeaderPoolPerformancePatch.following(creation, 3);
|
||||
if (!(next.get(1) instanceof MethodInsnNode constructor) || constructor.getOpcode() != Opcodes.INVOKESPECIAL || !constructor.owner.equals(BitHeaderPoolPerformancePatch.DEQUE) || !constructor.desc.equals("()V"))
|
||||
throw new IllegalStateException("'" + node.name + ".<clinit>': a 'ConcurrentLinkedDeque' is not created with its no-argument constructor.");
|
||||
if (!(next.get(2) instanceof FieldInsnNode store) || store.getOpcode() != Opcodes.PUTSTATIC || !BitHeaderPoolPerformancePatch.POOL_FIELDS.contains(store.name))
|
||||
throw new IllegalStateException("'" + node.name + ".<clinit>': a 'ConcurrentLinkedDeque' is created for something other than a header pool.");
|
||||
|
||||
creation.desc = BitHeaderPoolPerformancePatch.POOL;
|
||||
constructor.owner = BitHeaderPoolPerformancePatch.POOL;
|
||||
replaced.add(store.name);
|
||||
}
|
||||
|
||||
if (!replaced.equals(BitHeaderPoolPerformancePatch.POOL_FIELDS))
|
||||
throw new IllegalStateException("'" + node.name + ".<clinit>' creates the pools " + replaced + ", expected " + new TreeSet<>(BitHeaderPoolPerformancePatch.POOL_FIELDS) + ".");
|
||||
}
|
||||
|
||||
/** The {@code count} real instructions after {@code instruction}: no labels, line numbers, or frames. */
|
||||
private static List<AbstractInsnNode> following(AbstractInsnNode instruction, int count)
|
||||
{
|
||||
List<AbstractInsnNode> following = new ArrayList<>();
|
||||
|
||||
for (AbstractInsnNode next = instruction.getNext(); next != null && following.size() < count; next = next.getNext()) {
|
||||
if (next.getOpcode() != -1) following.add(next);
|
||||
}
|
||||
|
||||
if (following.size() < count) throw new IllegalStateException("'BitHeader.<clinit>' ends after a 'new ConcurrentLinkedDeque'.");
|
||||
|
||||
return following;
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.support.ClassDefiner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.TypeInsnNode;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.objectweb.asm.Opcodes.ACC_FINAL;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||
import static org.objectweb.asm.Opcodes.DUP;
|
||||
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
|
||||
import static org.objectweb.asm.Opcodes.NEW;
|
||||
import static org.objectweb.asm.Opcodes.PUTSTATIC;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class BitHeaderPoolPerformancePatchTest
|
||||
{
|
||||
private static final String HEADER = "zombie/util/io/BitHeader";
|
||||
private static final String DEQUE = "java/util/concurrent/ConcurrentLinkedDeque";
|
||||
private static final String POOL = "dev/overlord/zomboidforge/compat/b42/util/PerThreadDeque";
|
||||
|
||||
/** {@code BitHeader} whose {@code <clinit>} creates a deque for each of {@code fields}. */
|
||||
private static ClassNode bitHeader(List<String> fields)
|
||||
{
|
||||
ClassNode node = new ClassNode();
|
||||
node.visit(V25, ACC_PUBLIC | ACC_FINAL, BitHeaderPoolPerformancePatchTest.HEADER, null, "java/lang/Object", null);
|
||||
|
||||
for (String field : fields) node.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, field, "L" + BitHeaderPoolPerformancePatchTest.DEQUE + ";", null, null);
|
||||
|
||||
MethodVisitor clinit = node.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
|
||||
for (String field : fields)
|
||||
{
|
||||
clinit.visitTypeInsn(NEW, BitHeaderPoolPerformancePatchTest.DEQUE);
|
||||
clinit.visitInsn(DUP);
|
||||
clinit.visitMethodInsn(INVOKESPECIAL, BitHeaderPoolPerformancePatchTest.DEQUE, "<init>", "()V", false);
|
||||
clinit.visitFieldInsn(PUTSTATIC, BitHeaderPoolPerformancePatchTest.HEADER, field, "L" + BitHeaderPoolPerformancePatchTest.DEQUE + ";");
|
||||
}
|
||||
clinit.visitInsn(RETURN);
|
||||
clinit.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static ClassNode bitHeader()
|
||||
{
|
||||
return BitHeaderPoolPerformancePatchTest.bitHeader(List.of("pool_byte", "pool_short", "pool_int", "pool_long"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anchorMatchesTheTarget()
|
||||
{
|
||||
new BitHeaderPoolPerformancePatch().anchor().verify(BitHeaderPoolPerformancePatchTest.bitHeader());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsThePoolsAsPerThreadDeques()
|
||||
{
|
||||
ClassNode node = BitHeaderPoolPerformancePatchTest.bitHeader();
|
||||
new BitHeaderPoolPerformancePatch().apply(node);
|
||||
|
||||
int creations = 0;
|
||||
for (AbstractInsnNode instruction : node.methods.getFirst().instructions)
|
||||
{
|
||||
if (instruction instanceof TypeInsnNode creation && creation.getOpcode() == NEW)
|
||||
{
|
||||
assertEquals(BitHeaderPoolPerformancePatchTest.POOL, creation.desc);
|
||||
creations++;
|
||||
}
|
||||
if (instruction instanceof MethodInsnNode call) assertEquals(BitHeaderPoolPerformancePatchTest.POOL, call.owner);
|
||||
}
|
||||
|
||||
assertEquals(4, creations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void patchedPoolsAreConcurrentLinkedDeques() throws ReflectiveOperationException
|
||||
{
|
||||
ClassNode node = BitHeaderPoolPerformancePatchTest.bitHeader();
|
||||
new BitHeaderPoolPerformancePatch().apply(node);
|
||||
|
||||
// Initializing runs <clinit>, which creates the runtime module's PerThreadDeques.
|
||||
ClassDefiner definer = new ClassDefiner();
|
||||
definer.define(node);
|
||||
Class<?> loaded = Class.forName(node.name.replace('/', '.'), true, definer);
|
||||
|
||||
Field pool = loaded.getDeclaredField("pool_int");
|
||||
pool.setAccessible(true);
|
||||
Object value = pool.get(null);
|
||||
|
||||
assertTrue(value instanceof ConcurrentLinkedDeque<?>);
|
||||
assertEquals(BitHeaderPoolPerformancePatchTest.POOL.replace('/', '.'), value.getClass().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenADequeIsForAnotherField()
|
||||
{
|
||||
ClassNode node = BitHeaderPoolPerformancePatchTest.bitHeader(List.of("pool_byte", "pool_short", "pool_int", "pool_long", "other"));
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new BitHeaderPoolPerformancePatch().apply(node));
|
||||
assertTrue(e.getMessage().contains("other than a header pool"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenAPoolIsMissing()
|
||||
{
|
||||
ClassNode node = BitHeaderPoolPerformancePatchTest.bitHeader(List.of("pool_byte", "pool_short", "pool_int"));
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new BitHeaderPoolPerformancePatch().apply(node));
|
||||
assertTrue(e.getMessage().contains("expected [pool_byte, pool_int, pool_long, pool_short]"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void appliesOnlyToVerifiedVersions()
|
||||
{
|
||||
BitHeaderPoolPerformancePatch patch = new BitHeaderPoolPerformancePatch();
|
||||
|
||||
assertTrue(patch.appliesTo(GameVersion.parse("42.21.0")));
|
||||
assertTrue(patch.appliesTo(GameVersion.parse("42.21.3")));
|
||||
assertFalse(patch.appliesTo(GameVersion.parse("42.20.3")));
|
||||
assertFalse(patch.appliesTo(GameVersion.parse("42.22.0")));
|
||||
}
|
||||
}
|
||||
+49
@@ -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(); }
|
||||
}
|
||||
+70
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user