Add MethodBody implementation for method delegation and TileRules utility for optimised tile rule matching and caching. Include patches for WorldGenChunk performance and corresponding tests.

This commit is contained in:
Overlord committed 2026-10-04 23:19:08 +02:00
1 parent e932c7401a
commit 2081235433
10 files changed
+1029 -1

No files matched your search

@@ -27,7 +27,9 @@ public final class B42PatchSet implements PatchSet
new IsoGameCharacterStateMachinePerformancePatch(),
new ZombieVoronoiPerformancePatch(),
new ZomboidFileSystemInitPerformancePatch(),
new ServerMapCellBudgetPerformancePatch()
new ServerMapCellBudgetPerformancePatch(),
new WorldGenUtilsPlacementPerformancePatch(),
new WorldGenChunkRulesPerformancePatch()
);
@Override
@@ -0,0 +1,76 @@
package dev.overlord.zomboidforge.compat.b42.perf;
import dev.overlord.zomboidforge.api.GameVersion;
import dev.overlord.zomboidforge.patcher.Anchor;
import dev.overlord.zomboidforge.patcher.Hook;
import dev.overlord.zomboidforge.patcher.MethodBody;
import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
/**
* The tile rules of {@code WorldGenChunk} with cached compiled patterns, like
* {@link WorldGenUtilsPlacementPerformancePatch}:
*
* <ul>
* <li>the body of {@code isProtected(List, String)} becomes
* {@code return WorldGenHooks.isProtected(protectedTiles, tile);}</li>
* <li>in {@code cleanChunk(IsoChunk, String, String)}, {@code floorMaterial.matches(mat)}
* becomes {@code WorldGenHooks.matches(floorMaterial, mat)}.</li>
* </ul>
*/
public final class WorldGenChunkRulesPerformancePatch extends PerformancePatch
{
private static final String STRING = "java/lang/String";
private static final String MATCHES = "(Ljava/lang/String;)Z";
private static final String HOOKS = "dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks";
private static final Anchor.Call ANCHOR = new Anchor.Call(
new Anchor.Method("isProtected", "(Ljava/util/List;Ljava/lang/String;)Z"),
WorldGenChunkRulesPerformancePatch.STRING, "matches", WorldGenChunkRulesPerformancePatch.MATCHES
);
private static final Anchor.Call CLEAN_CHUNK = new Anchor.Call(
new Anchor.Method("cleanChunk", "(Lzombie/iso/IsoChunk;Ljava/lang/String;Ljava/lang/String;)V"),
WorldGenChunkRulesPerformancePatch.STRING, "matches", WorldGenChunkRulesPerformancePatch.MATCHES
);
private static final Hook HOOK = new Hook(
WorldGenChunkRulesPerformancePatch.HOOKS, "isProtected", "(Ljava/util/List;Ljava/lang/String;)Z"
);
private static final Hook MATCHES_HOOK = new Hook(
WorldGenChunkRulesPerformancePatch.HOOKS, "matches", "(Ljava/lang/String;Ljava/lang/String;)Z"
);
public WorldGenChunkRulesPerformancePatch() {
super("zombie/iso/worldgen/WorldGenChunk", WorldGenChunkRulesPerformancePatch.ANCHOR, WorldGenChunkRulesPerformancePatch.HOOK);
}
//
@Override
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
@Override
public void apply(ClassNode node)
{
MethodNode isProtected = WorldGenChunkRulesPerformancePatch.ANCHOR.in().resolve(node);
MethodNode cleanChunk = WorldGenChunkRulesPerformancePatch.CLEAN_CHUNK.in().resolve(node);
MethodInsnNode matches = WorldGenChunkRulesPerformancePatch.CLEAN_CHUNK.resolve(node);
MethodBody.delegate(node, isProtected, WorldGenChunkRulesPerformancePatch.HOOK);
// The hook takes the receiver as its first argument: the stack is unchanged.
cleanChunk.instructions.set(matches, new MethodInsnNode(
Opcodes.INVOKESTATIC,
WorldGenChunkRulesPerformancePatch.MATCHES_HOOK.owner(),
WorldGenChunkRulesPerformancePatch.MATCHES_HOOK.name(),
WorldGenChunkRulesPerformancePatch.MATCHES_HOOK.desc(),
false
));
}
}
@@ -0,0 +1,56 @@
package dev.overlord.zomboidforge.compat.b42.perf;
import dev.overlord.zomboidforge.api.GameVersion;
import dev.overlord.zomboidforge.patcher.Anchor;
import dev.overlord.zomboidforge.patcher.Hook;
import dev.overlord.zomboidforge.patcher.MethodBody;
import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
/**
* Replaces the body of {@code WorldGenUtils.canPlace(List, String)} with a call to
* {@code WorldGenHooks.canPlace}, which caches the compiled rules:
*
* <pre>{@code
* public boolean canPlace(List placement, String floorName)
* {
* return WorldGenHooks.canPlace(placement, floorName);
* }
* }</pre>
*
* Vanilla builds and compiles a regular expression per rule per call, for every square of a new
* chunk, on the chunk loader thread. Together with {@link WorldGenChunkRulesPerformancePatch}
* this halves the time to create a new chunk and removes most of the garbage the server
* produces while players explore; {@link ServerMapCellBudgetPerformancePatch} keeps the faster
* cells from bunching up on the main thread.
*/
public final class WorldGenUtilsPlacementPerformancePatch extends PerformancePatch
{
private static final Anchor.Call ANCHOR = new Anchor.Call(
new Anchor.Method("canPlace", "(Ljava/util/List;Ljava/lang/String;)Z"),
"java/lang/String", "matches", "(Ljava/lang/String;)Z"
);
private static final Hook HOOK = new Hook(
"dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks", "canPlace", "(Ljava/util/List;Ljava/lang/String;)Z"
);
public WorldGenUtilsPlacementPerformancePatch() {
super("zombie/iso/worldgen/WorldGenUtils", WorldGenUtilsPlacementPerformancePatch.ANCHOR, WorldGenUtilsPlacementPerformancePatch.HOOK);
}
//
@Override
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
@Override
public void apply(ClassNode node)
{
MethodNode method = WorldGenUtilsPlacementPerformancePatch.ANCHOR.in().resolve(node);
MethodBody.delegate(node, method, WorldGenUtilsPlacementPerformancePatch.HOOK);
}
}
@@ -0,0 +1,160 @@
package dev.overlord.zomboidforge.compat.b42.perf;
import dev.overlord.zomboidforge.api.GameVersion;
import dev.overlord.zomboidforge.compat.b42.patches.support.ClassDefiner;
import dev.overlord.zomboidforge.compat.b42.patches.support.PackageScan;
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.MethodNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.ACONST_NULL;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V25;
class WorldGenChunkRulesPerformancePatchTest
{
private static final String CHUNK = "zombie/iso/worldgen/WorldGenChunk";
private static final String HOOKS = "dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks";
private static final String MATCHES = "(Ljava/lang/String;)Z";
/**
* {@code WorldGenChunk} with a reduced {@code isProtected} ({@code return tile.matches(null);})
* and, when {@code withCleanChunk}, a reduced {@code cleanChunk}
* ({@code material.matches(filter);}).
*/
private static ClassNode chunk(boolean withCleanChunk)
{
ClassNode node = new ClassNode();
node.visit(V25, ACC_PUBLIC, WorldGenChunkRulesPerformancePatchTest.CHUNK, null, "java/lang/Object", null);
MethodVisitor constructor = node.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
constructor.visitVarInsn(ALOAD, 0);
constructor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
constructor.visitInsn(RETURN);
constructor.visitMaxs(0, 0);
MethodVisitor isProtected = node.visitMethod(ACC_PUBLIC, "isProtected", "(Ljava/util/List;Ljava/lang/String;)Z", null, null);
isProtected.visitVarInsn(ALOAD, 2);
isProtected.visitInsn(ACONST_NULL);
isProtected.visitTypeInsn(CHECKCAST, "java/lang/String");
isProtected.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", WorldGenChunkRulesPerformancePatchTest.MATCHES, false);
isProtected.visitInsn(IRETURN);
isProtected.visitMaxs(0, 0);
if (withCleanChunk)
{
MethodVisitor cleanChunk = node.visitMethod(ACC_PUBLIC, "cleanChunk", "(Lzombie/iso/IsoChunk;Ljava/lang/String;Ljava/lang/String;)V", null, null);
cleanChunk.visitVarInsn(ALOAD, 2);
cleanChunk.visitVarInsn(ALOAD, 3);
cleanChunk.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", WorldGenChunkRulesPerformancePatchTest.MATCHES, false);
cleanChunk.visitInsn(POP);
cleanChunk.visitInsn(RETURN);
cleanChunk.visitMaxs(0, 0);
}
return node;
}
private static List<MethodInsnNode> calls(ClassNode node, String methodName)
{
MethodNode method = node.methods.stream().filter(m -> m.name.equals(methodName)).findFirst().orElseThrow();
List<MethodInsnNode> calls = new ArrayList<>();
for (AbstractInsnNode instruction : method.instructions) {
if (instruction instanceof MethodInsnNode call) calls.add(call);
}
return calls;
}
@Test
void anchorMatchesTheTarget()
{
new WorldGenChunkRulesPerformancePatch().anchor().verify(WorldGenChunkRulesPerformancePatchTest.chunk(true));
}
@Test
void replacesIsProtectedAndTheMatchesCallOfCleanChunk()
{
ClassNode node = WorldGenChunkRulesPerformancePatchTest.chunk(true);
new WorldGenChunkRulesPerformancePatch().apply(node);
List<MethodInsnNode> isProtected = WorldGenChunkRulesPerformancePatchTest.calls(node, "isProtected");
assertEquals(1, isProtected.size());
assertEquals(INVOKESTATIC, isProtected.getFirst().getOpcode());
assertEquals(WorldGenChunkRulesPerformancePatchTest.HOOKS, isProtected.getFirst().owner);
assertEquals("isProtected", isProtected.getFirst().name);
List<MethodInsnNode> cleanChunk = WorldGenChunkRulesPerformancePatchTest.calls(node, "cleanChunk");
assertEquals(1, cleanChunk.size());
assertEquals(INVOKESTATIC, cleanChunk.getFirst().getOpcode());
assertEquals(WorldGenChunkRulesPerformancePatchTest.HOOKS, cleanChunk.getFirst().owner);
assertEquals("matches", cleanChunk.getFirst().name);
assertEquals("(Ljava/lang/String;Ljava/lang/String;)Z", cleanChunk.getFirst().desc);
}
@Test
void patchedClassIsValid() throws ClassNotFoundException
{
ClassNode node = WorldGenChunkRulesPerformancePatchTest.chunk(true);
new WorldGenChunkRulesPerformancePatch().apply(node);
// Initializing links the class, which runs the bytecode verifier on both changed methods.
ClassDefiner definer = new ClassDefiner();
definer.define(node);
Class.forName(node.name.replace('/', '.'), true, definer);
}
@Test
void failsWithoutCleanChunk()
{
ClassNode node = WorldGenChunkRulesPerformancePatchTest.chunk(false);
assertThrows(IllegalStateException.class, () -> new WorldGenChunkRulesPerformancePatch().apply(node));
}
/** {@code HookExistenceTest} checks the patch's {@code hook()}; this is the second one it calls. */
@Test
void matchesHookExistsAsPublicStaticMethod() throws IOException
{
ClassNode hooks = PackageScan.read(WorldGenChunkRulesPerformancePatchTest.HOOKS);
assertTrue(hooks.methods.stream().anyMatch(method ->
method.name.equals("matches")
&& method.desc.equals("(Ljava/lang/String;Ljava/lang/String;)Z")
&& (method.access & ACC_PUBLIC) != 0 && (method.access & ACC_STATIC) != 0
));
}
@Test
void appliesOnlyToVerifiedVersions()
{
WorldGenChunkRulesPerformancePatch patch = new WorldGenChunkRulesPerformancePatch();
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")));
}
}
@@ -0,0 +1,134 @@
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.MethodNode;
import java.util.ArrayList;
import java.util.List;
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_PUBLIC;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.ICONST_0;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V25;
class WorldGenUtilsPlacementPerformancePatchTest
{
private static final String UTILS = "zombie/iso/worldgen/WorldGenUtils";
/**
* {@code WorldGenUtils} with a reduced {@code canPlace}:
* {@code return floorName.matches((String) placement.get(0));}, reading {@code this} first
* when {@code usesThis}.
*/
private static ClassNode utils(boolean usesThis)
{
ClassNode node = new ClassNode();
node.visit(V25, ACC_PUBLIC, WorldGenUtilsPlacementPerformancePatchTest.UTILS, null, "java/lang/Object", null);
MethodVisitor constructor = node.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
constructor.visitVarInsn(ALOAD, 0);
constructor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
constructor.visitInsn(RETURN);
constructor.visitMaxs(0, 0);
MethodVisitor canPlace = node.visitMethod(ACC_PUBLIC, "canPlace", "(Ljava/util/List;Ljava/lang/String;)Z", null, null);
if (usesThis)
{
canPlace.visitVarInsn(ALOAD, 0);
canPlace.visitInsn(POP);
}
canPlace.visitVarInsn(ALOAD, 2);
canPlace.visitVarInsn(ALOAD, 1);
canPlace.visitInsn(ICONST_0);
canPlace.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;", true);
canPlace.visitTypeInsn(CHECKCAST, "java/lang/String");
canPlace.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", "(Ljava/lang/String;)Z", false);
canPlace.visitInsn(IRETURN);
canPlace.visitMaxs(0, 0);
return node;
}
private static List<AbstractInsnNode> instructions(ClassNode node)
{
MethodNode canPlace = node.methods.stream().filter(method -> method.name.equals("canPlace")).findFirst().orElseThrow();
List<AbstractInsnNode> instructions = new ArrayList<>();
for (AbstractInsnNode instruction : canPlace.instructions) {
if (instruction.getOpcode() != -1) instructions.add(instruction);
}
return instructions;
}
@Test
void anchorMatchesTheTarget()
{
new WorldGenUtilsPlacementPerformancePatch().anchor().verify(WorldGenUtilsPlacementPerformancePatchTest.utils(false));
}
@Test
void replacesTheBodyWithTheHookCall()
{
ClassNode node = WorldGenUtilsPlacementPerformancePatchTest.utils(false);
new WorldGenUtilsPlacementPerformancePatch().apply(node);
List<AbstractInsnNode> instructions = WorldGenUtilsPlacementPerformancePatchTest.instructions(node);
assertEquals(List.of(ALOAD, ALOAD, INVOKESTATIC, IRETURN), instructions.stream().map(AbstractInsnNode::getOpcode).toList());
MethodInsnNode call = (MethodInsnNode) instructions.get(2);
assertEquals("dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks", call.owner);
assertEquals("canPlace", call.name);
assertEquals("(Ljava/util/List;Ljava/lang/String;)Z", call.desc);
}
@Test
void patchedClassIsValid() throws ClassNotFoundException
{
ClassNode node = WorldGenUtilsPlacementPerformancePatchTest.utils(false);
new WorldGenUtilsPlacementPerformancePatch().apply(node);
// Initializing links the class, which runs the bytecode verifier on the new body.
ClassDefiner definer = new ClassDefiner();
definer.define(node);
Class.forName(node.name.replace('/', '.'), true, definer);
}
@Test
void failsWhenTheBodyUsesThis()
{
ClassNode node = WorldGenUtilsPlacementPerformancePatchTest.utils(true);
assertThrows(IllegalStateException.class, () -> new WorldGenUtilsPlacementPerformancePatch().apply(node));
}
@Test
void appliesOnlyToVerifiedVersions()
{
WorldGenUtilsPlacementPerformancePatch patch = new WorldGenUtilsPlacementPerformancePatch();
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")));
}
}