Optimize ZombieVoronoi.evaluateNoise with a hook-based implementation for Build 42.21. Replace distance calculations with a single-pass method, avoiding boxed sorting. Add associated tests and integrate new patch into the performance patch set.
This commit is contained in:
1 parent
05eed7e205
commit
900f65b591
6 files changed
+576
-1
No files matched your search
+2
-1
@@ -24,7 +24,8 @@ public final class B42PatchSet implements PatchSet
|
||||
new IsoZombieVehiclePerformancePatch(),
|
||||
new IsoZombieNetworkComponentPerformancePatch(),
|
||||
new IsoZombieSpotChancePerformancePatch(),
|
||||
new IsoGameCharacterStateMachinePerformancePatch()
|
||||
new IsoGameCharacterStateMachinePerformancePatch(),
|
||||
new ZombieVoronoiPerformancePatch()
|
||||
);
|
||||
|
||||
@Override
|
||||
|
||||
+121
@@ -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:
|
||||
*
|
||||
* <pre>{@code
|
||||
* private double evaluateNoise(
|
||||
* double x, double y, Random rng, List<Coord> points
|
||||
* ) {
|
||||
* return ZombieVoronoiHooks.evaluateNoise(
|
||||
* this.closestPoint,
|
||||
* this.seed,
|
||||
* this.numberPoints,
|
||||
* x, y, rng, points
|
||||
* );
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* 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 + "'.");
|
||||
}
|
||||
}
|
||||
+188
@@ -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}:
|
||||
*
|
||||
* <pre>{@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);
|
||||
* }
|
||||
* }</pre>
|
||||
*/
|
||||
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, "<init>", "()V", null, null);
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
init.visitInsn(RETURN);
|
||||
init.visitMaxs(0, 0);
|
||||
|
||||
MethodVisitor noise = node.visitMethod(noiseAccess, "evaluateNoise", ZombieVoronoiPerformancePatchTest.NOISE, null, null);
|
||||
Label start = new Label();
|
||||
Label end = new Label();
|
||||
noise.visitLabel(start);
|
||||
noise.visitVarInsn(ALOAD, 0);
|
||||
noise.visitFieldInsn(GETFIELD, ZombieVoronoiPerformancePatchTest.VORONOI, "closestPoint", "L" + ZombieVoronoiPerformancePatchTest.SELECTION + ";");
|
||||
noise.visitVarInsn(DLOAD, 1);
|
||||
noise.visitVarInsn(DLOAD, 3);
|
||||
noise.visitVarInsn(ALOAD, 6);
|
||||
noise.visitMethodInsn(INVOKEINTERFACE, ZombieVoronoiPerformancePatchTest.SELECTION, "getClosest", "(DDLjava/util/List;)D", true);
|
||||
noise.visitInsn(DRETURN);
|
||||
noise.visitLabel(end);
|
||||
noise.visitLocalVariable("unused", "J", null, start, end, 7);
|
||||
noise.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static ClassNode voronoi()
|
||||
{
|
||||
return ZombieVoronoiPerformancePatchTest.voronoi(true, ACC_PRIVATE);
|
||||
}
|
||||
|
||||
private static MethodNode evaluateNoise(ClassNode node)
|
||||
{
|
||||
return node.methods.stream().filter(method -> method.name.equals("evaluateNoise")).findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
/** The real instructions: no labels, line numbers, or frames. */
|
||||
private static List<AbstractInsnNode> instructions(MethodNode method)
|
||||
{
|
||||
List<AbstractInsnNode> instructions = new ArrayList<>();
|
||||
|
||||
for (AbstractInsnNode instruction : method.instructions) {
|
||||
if (instruction.getOpcode() != -1) instructions.add(instruction);
|
||||
}
|
||||
|
||||
return instructions;
|
||||
}
|
||||
|
||||
@Test
|
||||
void anchorMatchesTheTarget()
|
||||
{
|
||||
new ZombieVoronoiPerformancePatch().anchor().verify(ZombieVoronoiPerformancePatchTest.voronoi());
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacesTheBodyWithTheHookCall()
|
||||
{
|
||||
ClassNode node = ZombieVoronoiPerformancePatchTest.voronoi();
|
||||
new ZombieVoronoiPerformancePatch().apply(node);
|
||||
|
||||
List<AbstractInsnNode> instructions = ZombieVoronoiPerformancePatchTest.instructions(ZombieVoronoiPerformancePatchTest.evaluateNoise(node));
|
||||
|
||||
assertEquals(
|
||||
List.of(ALOAD, GETFIELD, ALOAD, GETFIELD, ALOAD, GETFIELD, DLOAD, DLOAD, ALOAD, ALOAD, INVOKESTATIC, DRETURN),
|
||||
instructions.stream().map(AbstractInsnNode::getOpcode).toList()
|
||||
);
|
||||
assertEquals(
|
||||
List.of("closestPoint", "seed", "numberPoints"),
|
||||
instructions.stream().filter(FieldInsnNode.class::isInstance).map(field -> ((FieldInsnNode) field).name).toList()
|
||||
);
|
||||
|
||||
MethodInsnNode call = (MethodInsnNode) instructions.get(10);
|
||||
assertEquals(ZombieVoronoiPerformancePatchTest.HOOKS, call.owner);
|
||||
assertEquals("evaluateNoise", call.name);
|
||||
assertEquals("(L" + ZombieVoronoiPerformancePatchTest.SELECTION + ";JIDDLjava/util/Random;Ljava/util/List;)D", call.desc);
|
||||
assertFalse(call.itf);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropsTheLocalVariablesOfTheOldBody()
|
||||
{
|
||||
ClassNode node = ZombieVoronoiPerformancePatchTest.voronoi();
|
||||
new ZombieVoronoiPerformancePatch().apply(node);
|
||||
|
||||
assertNull(ZombieVoronoiPerformancePatchTest.evaluateNoise(node).localVariables);
|
||||
}
|
||||
|
||||
@Test
|
||||
void patchedClassIsValid() throws ClassNotFoundException
|
||||
{
|
||||
ClassNode node = ZombieVoronoiPerformancePatchTest.voronoi();
|
||||
new ZombieVoronoiPerformancePatch().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 failsWithoutTheSeedField()
|
||||
{
|
||||
ClassNode node = ZombieVoronoiPerformancePatchTest.voronoi(false, ACC_PRIVATE);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new ZombieVoronoiPerformancePatch().apply(node));
|
||||
assertTrue(e.getMessage().contains("'" + ZombieVoronoiPerformancePatchTest.VORONOI + ".seed J'"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTheMethodIsStatic()
|
||||
{
|
||||
ClassNode node = ZombieVoronoiPerformancePatchTest.voronoi(true, ACC_PRIVATE | ACC_STATIC);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> new ZombieVoronoiPerformancePatch().apply(node));
|
||||
}
|
||||
|
||||
@Test
|
||||
void appliesOnlyToVerifiedVersions()
|
||||
{
|
||||
ZombieVoronoiPerformancePatch patch = new ZombieVoronoiPerformancePatch();
|
||||
|
||||
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")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user