diff --git a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java index c28494a..c056012 100644 --- a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java +++ b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java @@ -24,7 +24,8 @@ public final class B42PatchSet implements PatchSet new IsoZombieVehiclePerformancePatch(), new IsoZombieNetworkComponentPerformancePatch(), new IsoZombieSpotChancePerformancePatch(), - new IsoGameCharacterStateMachinePerformancePatch() + new IsoGameCharacterStateMachinePerformancePatch(), + new ZombieVoronoiPerformancePatch() ); @Override diff --git a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/ZombieVoronoiPerformancePatch.java b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/ZombieVoronoiPerformancePatch.java new file mode 100644 index 0000000..30e6f02 --- /dev/null +++ b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/ZombieVoronoiPerformancePatch.java @@ -0,0 +1,121 @@ +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.Patches.PerformancePatch; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.InsnList; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.VarInsnNode; + +/** + * Replaces the body of {@code ZombieVoronoi.evaluateNoise(double, double, Random, List)} with a + * call to {@code ZombieVoronoiHooks.evaluateNoise}, which returns the same values: + * + *
{@code
+ * private double evaluateNoise(
+ * double x, double y, Random rng, List points
+ * ) {
+ * return ZombieVoronoiHooks.evaluateNoise(
+ * this.closestPoint,
+ * this.seed,
+ * this.numberPoints,
+ * x, y, rng, points
+ * );
+ * }
+ * }
+ *
+ * The world generation evaluates it 1,024 times per cell at startup; each call creates 9
+ * {@code Coord}s and lets {@code ClosestSelectionType.getClosest} sort their boxed distances.
+ * The hook computes the distances in one pass.
+ */
+public final class ZombieVoronoiPerformancePatch extends PerformancePatch
+{
+ private static final String SELECTION = "zombie/iso/worldgen/zombie/ClosestSelection";
+
+ private static final Anchor.Call ANCHOR = new Anchor.Call(
+ new Anchor.Method("evaluateNoise", "(DDLjava/util/Random;Ljava/util/List;)D"),
+ ZombieVoronoiPerformancePatch.SELECTION, "getClosest", "(DDLjava/util/List;)D"
+ );
+
+ private static final Hook HOOK = new Hook(
+ "dev/overlord/zomboidforge/compat/b42/hooks/ZombieVoronoiHooks", "evaluateNoise",
+ "(L" + ZombieVoronoiPerformancePatch.SELECTION + ";JIDDLjava/util/Random;Ljava/util/List;)D"
+ );
+
+ public ZombieVoronoiPerformancePatch() {
+ super("zombie/iso/worldgen/zombie/ZombieVoronoi", ZombieVoronoiPerformancePatch.ANCHOR, ZombieVoronoiPerformancePatch.HOOK);
+ }
+
+ //
+
+ @Override
+ public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
+
+ @Override
+ public void apply(ClassNode node)
+ {
+ MethodNode method = ZombieVoronoiPerformancePatch.ANCHOR.in().resolve(node);
+
+ if ((method.access & Opcodes.ACC_STATIC) != 0)
+ throw new IllegalStateException("'" + node.name + "." + method.name + method.desc + "' is static.");
+
+ ZombieVoronoiPerformancePatch.requireField(node, "closestPoint", "L" + ZombieVoronoiPerformancePatch.SELECTION + ";");
+ ZombieVoronoiPerformancePatch.requireField(node, "seed", "J");
+ ZombieVoronoiPerformancePatch.requireField(node, "numberPoints", "I");
+
+ // The local variable table and annotations refer to labels of the old body.
+ method.instructions.clear();
+ method.tryCatchBlocks.clear();
+ method.localVariables = null;
+ method.visibleLocalVariableAnnotations = null;
+ method.invisibleLocalVariableAnnotations = null;
+ method.instructions.add(ZombieVoronoiPerformancePatch.callHook(node.name));
+ }
+
+ /**
+ * {@code return hook(this.closestPoint, this.seed, this.numberPoints, x, y, rng, points);}.
+ * Locals: {@code this} 0, {@code x} 1-2, {@code y} 3-4, {@code rng} 5, {@code points} 6.
+ */
+ private static InsnList callHook(String owner)
+ {
+ InsnList code = new InsnList();
+
+ code.add(new VarInsnNode(Opcodes.ALOAD, 0));
+ code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "closestPoint", "L" + ZombieVoronoiPerformancePatch.SELECTION + ";"));
+ code.add(new VarInsnNode(Opcodes.ALOAD, 0));
+ code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "seed", "J"));
+ code.add(new VarInsnNode(Opcodes.ALOAD, 0));
+ code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "numberPoints", "I"));
+ code.add(new VarInsnNode(Opcodes.DLOAD, 1));
+ code.add(new VarInsnNode(Opcodes.DLOAD, 3));
+ code.add(new VarInsnNode(Opcodes.ALOAD, 5));
+ code.add(new VarInsnNode(Opcodes.ALOAD, 6));
+ code.add(new MethodInsnNode(
+ Opcodes.INVOKESTATIC,
+ ZombieVoronoiPerformancePatch.HOOK.owner(),
+ ZombieVoronoiPerformancePatch.HOOK.name(),
+ ZombieVoronoiPerformancePatch.HOOK.desc(),
+ false
+ ));
+ code.add(new InsnNode(Opcodes.DRETURN));
+
+ return code;
+ }
+
+ private static void requireField(ClassNode node, String name, String desc)
+ {
+ for (FieldNode field : node.fields) {
+ if (field.name.equals(name) && field.desc.equals(desc) && (field.access & Opcodes.ACC_STATIC) == 0) return;
+ }
+
+ throw new IllegalStateException("Expected the instance field '" + node.name + "." + name + " " + desc + "'.");
+ }
+}
diff --git a/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/ZombieVoronoiPerformancePatchTest.java b/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/ZombieVoronoiPerformancePatchTest.java
new file mode 100644
index 0000000..811787d
--- /dev/null
+++ b/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/ZombieVoronoiPerformancePatchTest.java
@@ -0,0 +1,188 @@
+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.Label;
+import org.objectweb.asm.MethodVisitor;
+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 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.assertNull;
+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.ALOAD;
+import static org.objectweb.asm.Opcodes.DLOAD;
+import static org.objectweb.asm.Opcodes.DRETURN;
+import static org.objectweb.asm.Opcodes.GETFIELD;
+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.RETURN;
+import static org.objectweb.asm.Opcodes.V25;
+
+class ZombieVoronoiPerformancePatchTest
+{
+ private static final String VORONOI = "zombie/iso/worldgen/zombie/ZombieVoronoi";
+ private static final String SELECTION = "zombie/iso/worldgen/zombie/ClosestSelection";
+ private static final String HOOKS = "dev/overlord/zomboidforge/compat/b42/hooks/ZombieVoronoiHooks";
+ private static final String NOISE = "(DDLjava/util/Random;Ljava/util/List;)D";
+
+ /**
+ * {@code ZombieVoronoi} with its three fields and a reduced {@code evaluateNoise}:
+ *
+ * {@code
+ * private double evaluateNoise(double x, double y, Random rng, List points)
+ * {
+ * long unused = this.seed; // a local beyond the parameters
+ * return this.closestPoint.getClosest(x, y, points);
+ * }
+ * }
+ */
+ private static ClassNode voronoi(boolean withSeed, int noiseAccess)
+ {
+ ClassNode node = new ClassNode();
+ node.visit(V25, ACC_PUBLIC, ZombieVoronoiPerformancePatchTest.VORONOI, null, "java/lang/Object", null);
+
+ if (withSeed) node.visitField(ACC_PRIVATE | ACC_FINAL, "seed", "J", null, null);
+ node.visitField(ACC_PRIVATE | ACC_FINAL, "numberPoints", "I", null, null);
+ node.visitField(ACC_PRIVATE | ACC_FINAL, "closestPoint", "L" + ZombieVoronoiPerformancePatchTest.SELECTION + ";", null, null);
+
+ MethodVisitor init = node.visitMethod(ACC_PUBLIC, "Vanilla clears {@code points}, adds {@code numberPoints} {@link Coord}s for each of the 9 + * sectors around {@code (x, y)}, reseeding {@code rng} per sector, and returns + * {@code closestPoint.getClosest(x, y, points)}. The three {@link ClosestSelectionType}s map the + * points to boxed squared distances, sort them, and return the smallest, the second smallest, + * or the second minus the smallest. + * + *
Here the same random numbers in the same order feed the same distance expression, and one
+ * pass keeps the two smallest distances in the order of {@code Double.compare}, which
+ * {@code sorted()} uses. The results are identical without the {@code Coord}s, the boxing and
+ * the sort. {@code points} stays untouched: the only caller, {@code evaluateCellNoise}, passes a
+ * list it does not read. Any other selection gets the filled list as in vanilla.
+ */
+public final class VoronoiNoise
+{
+ private VoronoiNoise() {}
+
+ public static double evaluate(
+ ClosestSelection selection, long seed, int numberPoints,
+ double x, double y, Random rng, List