Add IsoZombieNetworkComponentPerformancePatch for efficient component access by caching NetworkZombieComponent lookups. Update B42PatchSet to include the new patch and adjust related test cases.
This commit is contained in:
1 parent
acef6fd81f
commit
134901098c
8 files changed
+463
-14
No files matched your search
+8
-3
@@ -1,9 +1,13 @@
|
|||||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
package dev.overlord.zomboidforge.compat.b42;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.patches.ServerStartedPatch;
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieNetworkComponentPerformancePatch;
|
||||||
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieVehiclePerformancePatch;
|
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieVehiclePerformancePatch;
|
||||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||||
import dev.overlord.zomboidforge.patcher.PatchSet;
|
import dev.overlord.zomboidforge.patcher.PatchSet;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** Every build-time patch for Build 42, in application order. */
|
/** Every build-time patch for Build 42, in application order. */
|
||||||
@@ -11,12 +15,13 @@ public final class B42PatchSet implements PatchSet
|
|||||||
{
|
{
|
||||||
private static final List<ClassPatch> PATCHES = List.of(
|
private static final List<ClassPatch> PATCHES = List.of(
|
||||||
new ServerStartedPatch(),
|
new ServerStartedPatch(),
|
||||||
new IsoZombieVehiclePerformancePatch()
|
new IsoZombieVehiclePerformancePatch(),
|
||||||
|
new IsoZombieNetworkComponentPerformancePatch()
|
||||||
);
|
);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int build() { return 42; }
|
public int build() { return 42; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ClassPatch> patches() { return B42PatchSet.PATCHES; }
|
public @NotNull List<ClassPatch> patches() { return B42PatchSet.PATCHES; }
|
||||||
}
|
}
|
||||||
+236
@@ -0,0 +1,236 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
|
import org.objectweb.asm.Type;
|
||||||
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
|
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.JumpInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LabelNode;
|
||||||
|
import org.objectweb.asm.tree.LdcInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
import org.objectweb.asm.tree.TypeInsnNode;
|
||||||
|
import org.objectweb.asm.tree.VarInsnNode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the {@code NetworkZombieComponent} of an {@code IsoZombie} in a field instead of
|
||||||
|
* looking it up in the entity's component map on every call.
|
||||||
|
*
|
||||||
|
* <p>Every call site in {@code IsoZombie} has the same shape:
|
||||||
|
* {@code (NetworkZombieComponent) this.tryGetECSComponent(NetworkZombieComponent.class)}. The
|
||||||
|
* lookup walks the class hierarchy of the component class and reads a {@code HashMap}. The
|
||||||
|
* component is registered once by the {@code IsoObject} constructor and is never replaced or
|
||||||
|
* removed, so a value read once stays valid for the life of the zombie.
|
||||||
|
*
|
||||||
|
* <p>The patch adds the field and a private method that fills it on first use, and replaces the
|
||||||
|
* three instructions of each call site with a call to that method. A missing component is not
|
||||||
|
* stored: the method then returns {@code null} like the lookup does.
|
||||||
|
*/
|
||||||
|
public final class IsoZombieNetworkComponentPerformancePatch extends PerformancePatch
|
||||||
|
{
|
||||||
|
private static final String ZOMBIE = "zombie/characters/IsoZombie";
|
||||||
|
private static final String COMPONENT = "zombie/characters/component/NetworkZombieComponent";
|
||||||
|
|
||||||
|
private static final String LOOKUP_NAME = "tryGetECSComponent";
|
||||||
|
private static final String LOOKUP_DESC = "(Ljava/lang/Class;)Lzombie/characters/ecs/ECSComponent;";
|
||||||
|
|
||||||
|
private static final String FIELD_NAME = "zomboidForge$networkComponent";
|
||||||
|
private static final String FIELD_DESC = "L" + IsoZombieNetworkComponentPerformancePatch.COMPONENT + ";";
|
||||||
|
|
||||||
|
private static final String ACCESSOR_NAME = "zomboidForge$networkComponent";
|
||||||
|
private static final String ACCESSOR_DESC = "()" + IsoZombieNetworkComponentPerformancePatch.FIELD_DESC;
|
||||||
|
|
||||||
|
private static final Anchor.Call ANCHOR = new Anchor.Call(
|
||||||
|
new Anchor.Method("getOwner", "()Lzombie/core/raknet/UdpConnection;"),
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_DESC
|
||||||
|
);
|
||||||
|
|
||||||
|
public IsoZombieNetworkComponentPerformancePatch() {
|
||||||
|
super(IsoZombieNetworkComponentPerformancePatch.ZOMBIE, IsoZombieNetworkComponentPerformancePatch.ANCHOR, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node)
|
||||||
|
{
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.requireNoMemberNamed(node);
|
||||||
|
|
||||||
|
node.fields.add(new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC, IsoZombieNetworkComponentPerformancePatch.FIELD_NAME, IsoZombieNetworkComponentPerformancePatch.FIELD_DESC, null, null));
|
||||||
|
node.methods.add(IsoZombieNetworkComponentPerformancePatch.accessor());
|
||||||
|
|
||||||
|
for (MethodNode method : node.methods) {
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.replaceLookups(method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The patch must not collide with a member of the game class, e.g. after an update. */
|
||||||
|
private static void requireNoMemberNamed(ClassNode node)
|
||||||
|
{
|
||||||
|
boolean fieldExists = node.fields.stream()
|
||||||
|
.anyMatch(field -> field.name.equals(IsoZombieNetworkComponentPerformancePatch.FIELD_NAME));
|
||||||
|
boolean methodExists = node.methods.stream()
|
||||||
|
.anyMatch(method -> method.name.equals(IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME));
|
||||||
|
|
||||||
|
if (fieldExists || methodExists)
|
||||||
|
throw new IllegalStateException("'" + node.name + "' already has a member named '" + IsoZombieNetworkComponentPerformancePatch.FIELD_NAME + "'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>{@code
|
||||||
|
* private NetworkZombieComponent zomboidForge$networkComponent()
|
||||||
|
* {
|
||||||
|
* NetworkZombieComponent component = this.zomboidForge$networkComponent;
|
||||||
|
*
|
||||||
|
* if (component == null) {
|
||||||
|
* component = (NetworkZombieComponent) this.tryGetECSComponent(NetworkZombieComponent.class);
|
||||||
|
* this.zomboidForge$networkComponent = component;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* return component;
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
|
private static MethodNode accessor()
|
||||||
|
{
|
||||||
|
MethodNode method = new MethodNode(
|
||||||
|
Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_DESC,
|
||||||
|
null, null
|
||||||
|
);
|
||||||
|
LabelNode filled = new LabelNode();
|
||||||
|
InsnList code = method.instructions;
|
||||||
|
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||||
|
code.add(new FieldInsnNode(
|
||||||
|
Opcodes.GETFIELD,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_DESC
|
||||||
|
));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ASTORE, 1));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||||
|
code.add(new JumpInsnNode(Opcodes.IFNONNULL, filled));
|
||||||
|
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||||
|
code.add(new LdcInsnNode(Type.getObjectType(IsoZombieNetworkComponentPerformancePatch.COMPONENT)));
|
||||||
|
code.add(new MethodInsnNode(
|
||||||
|
Opcodes.INVOKEVIRTUAL,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_DESC,
|
||||||
|
false
|
||||||
|
));
|
||||||
|
code.add(new TypeInsnNode(Opcodes.CHECKCAST, IsoZombieNetworkComponentPerformancePatch.COMPONENT));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ASTORE, 1));
|
||||||
|
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||||
|
code.add(new FieldInsnNode(
|
||||||
|
Opcodes.PUTFIELD,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_DESC
|
||||||
|
));
|
||||||
|
|
||||||
|
code.add(filled);
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||||
|
code.add(new InsnNode(Opcodes.ARETURN));
|
||||||
|
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces {@code ldc NetworkZombieComponent; invokevirtual tryGetECSComponent; checkcast
|
||||||
|
* NetworkZombieComponent} with {@code invokespecial zomboidForge$networkComponent}. The
|
||||||
|
* receiver is on the operand stack before and after, and the component instead of the
|
||||||
|
* {@code Class} leaves it, so the stack stays the same. The accessor itself is skipped: it
|
||||||
|
* contains the same three instructions.
|
||||||
|
*/
|
||||||
|
private static void replaceLookups(MethodNode method)
|
||||||
|
{
|
||||||
|
if (method.name.equals(IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME)) return;
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : method.instructions.toArray())
|
||||||
|
{
|
||||||
|
if (!IsoZombieNetworkComponentPerformancePatch.isLookup(instruction)) continue;
|
||||||
|
|
||||||
|
@Nullable AbstractInsnNode load = IsoZombieNetworkComponentPerformancePatch.previousInstruction(instruction);
|
||||||
|
@Nullable AbstractInsnNode cast = IsoZombieNetworkComponentPerformancePatch.nextInstruction(instruction);
|
||||||
|
|
||||||
|
if (!IsoZombieNetworkComponentPerformancePatch.isComponentClassLiteral(load)) continue;
|
||||||
|
if (!IsoZombieNetworkComponentPerformancePatch.isComponentCast(cast)) continue;
|
||||||
|
|
||||||
|
method.instructions.remove(load);
|
||||||
|
method.instructions.remove(cast);
|
||||||
|
method.instructions.set(
|
||||||
|
instruction, new MethodInsnNode(
|
||||||
|
Opcodes.INVOKESPECIAL,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_DESC,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
private static boolean isLookup(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof MethodInsnNode call
|
||||||
|
&& call.getOpcode() == Opcodes.INVOKEVIRTUAL
|
||||||
|
&& call.owner.equals(IsoZombieNetworkComponentPerformancePatch.ZOMBIE)
|
||||||
|
&& call.name.equals(IsoZombieNetworkComponentPerformancePatch.LOOKUP_NAME)
|
||||||
|
&& call.desc.equals(IsoZombieNetworkComponentPerformancePatch.LOOKUP_DESC);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isComponentClassLiteral(@Nullable AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof LdcInsnNode ldc
|
||||||
|
&& ldc.cst instanceof Type type
|
||||||
|
&& type.getInternalName().equals(IsoZombieNetworkComponentPerformancePatch.COMPONENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isComponentCast(@Nullable AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof TypeInsnNode cast
|
||||||
|
&& cast.getOpcode() == Opcodes.CHECKCAST
|
||||||
|
&& cast.desc.equals(IsoZombieNetworkComponentPerformancePatch.COMPONENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The closest earlier instruction that is not a label, line number, or frame; {@code null} at the start of the method. */
|
||||||
|
private static @Nullable AbstractInsnNode previousInstruction(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
AbstractInsnNode previous = instruction.getPrevious();
|
||||||
|
while (previous != null && previous.getOpcode() == -1) previous = previous.getPrevious();
|
||||||
|
|
||||||
|
return previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The closest later instruction that is not a label, line number, or frame; {@code null} at the end of the method. */
|
||||||
|
private static @Nullable AbstractInsnNode nextInstruction(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
AbstractInsnNode next = instruction.getNext();
|
||||||
|
while (next != null && next.getOpcode() == -1) next = next.getNext();
|
||||||
|
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
dev.overlord.zomboidforge.compat.b42.patches.B42PatchSet
|
dev.overlord.zomboidforge.compat.b42.B42PatchSet
|
||||||
+1
@@ -1,5 +1,6 @@
|
|||||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.B42PatchSet;
|
||||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||||
import dev.overlord.zomboidforge.patcher.Hook;
|
import dev.overlord.zomboidforge.patcher.Hook;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|||||||
+1
-5
@@ -25,8 +25,6 @@ class NamingRulesTest
|
|||||||
);
|
);
|
||||||
private static final String HOOKS_PACKAGE = "dev/overlord/zomboidforge/compat/b42/hooks";
|
private static final String HOOKS_PACKAGE = "dev/overlord/zomboidforge/compat/b42/hooks";
|
||||||
|
|
||||||
private static final String PATCH_SET = "B42PatchSet";
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void patchClassesEndInPatch() throws Exception
|
void patchClassesEndInPatch() throws Exception
|
||||||
{
|
{
|
||||||
@@ -40,9 +38,7 @@ class NamingRulesTest
|
|||||||
|
|
||||||
for (String name : names)
|
for (String name : names)
|
||||||
{
|
{
|
||||||
if (!name.equals(NamingRulesTest.PATCH_SET) && !name.endsWith("Patch")) {
|
if (!name.endsWith("Patch")) problems.add("'" + patchPackage + "/" + name + "' must end in 'Patch'.");
|
||||||
problems.add("'" + patchPackage + "/" + name + "' must end in 'Patch' (or be '" + NamingRulesTest.PATCH_SET + "').");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
@@ -1,5 +1,6 @@
|
|||||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.B42PatchSet;
|
||||||
import dev.overlord.zomboidforge.compat.b42.patches.support.PackageScan;
|
import dev.overlord.zomboidforge.compat.b42.patches.support.PackageScan;
|
||||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|||||||
+209
@@ -0,0 +1,209 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.objectweb.asm.ClassReader;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.Type;
|
||||||
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.FieldInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LdcInsnNode;
|
||||||
|
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_PRIVATE;
|
||||||
|
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.ACONST_NULL;
|
||||||
|
import static org.objectweb.asm.Opcodes.ALOAD;
|
||||||
|
import static org.objectweb.asm.Opcodes.ARETURN;
|
||||||
|
import static org.objectweb.asm.Opcodes.CHECKCAST;
|
||||||
|
import static org.objectweb.asm.Opcodes.GETFIELD;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
|
||||||
|
import static org.objectweb.asm.Opcodes.POP;
|
||||||
|
import static org.objectweb.asm.Opcodes.PUTFIELD;
|
||||||
|
import static org.objectweb.asm.Opcodes.RETURN;
|
||||||
|
import static org.objectweb.asm.Opcodes.V25;
|
||||||
|
|
||||||
|
class IsoZombieNetworkComponentPerformancePatchTest
|
||||||
|
{
|
||||||
|
private static final String ZOMBIE = "zombie/characters/IsoZombie";
|
||||||
|
private static final String COMPONENT = "zombie/characters/component/NetworkZombieComponent";
|
||||||
|
private static final String OTHER = "zombie/characters/component/OtherComponent";
|
||||||
|
private static final String LOOKUP = "(Ljava/lang/Class;)Lzombie/characters/ecs/ECSComponent;";
|
||||||
|
|
||||||
|
/** {@code this.tryGetECSComponent(component.class)} cast to {@code component}, result dropped. */
|
||||||
|
private static void lookup(MethodVisitor method, String component)
|
||||||
|
{
|
||||||
|
method.visitVarInsn(ALOAD, 0);
|
||||||
|
method.visitLdcInsn(Type.getObjectType(component));
|
||||||
|
method.visitMethodInsn(INVOKEVIRTUAL, IsoZombieNetworkComponentPerformancePatchTest.ZOMBIE, "tryGetECSComponent", IsoZombieNetworkComponentPerformancePatchTest.LOOKUP, false);
|
||||||
|
method.visitTypeInsn(CHECKCAST, component);
|
||||||
|
method.visitInsn(POP);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code IsoZombie} with {@code getOwner()} and {@code setOwner()}, each looking up the
|
||||||
|
* network component, and {@code other()}, which looks up an unrelated component.
|
||||||
|
*/
|
||||||
|
private static ClassNode target()
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, IsoZombieNetworkComponentPerformancePatchTest.ZOMBIE, null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
MethodVisitor getOwner = cw.visitMethod(ACC_PUBLIC, "getOwner", "()Lzombie/core/raknet/UdpConnection;", null, null);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.lookup(getOwner, IsoZombieNetworkComponentPerformancePatchTest.COMPONENT);
|
||||||
|
getOwner.visitInsn(ACONST_NULL);
|
||||||
|
getOwner.visitInsn(ARETURN);
|
||||||
|
getOwner.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
MethodVisitor setOwner = cw.visitMethod(ACC_PUBLIC, "setOwner", "()V", null, null);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.lookup(setOwner, IsoZombieNetworkComponentPerformancePatchTest.COMPONENT);
|
||||||
|
setOwner.visitInsn(RETURN);
|
||||||
|
setOwner.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
MethodVisitor other = cw.visitMethod(ACC_PUBLIC, "other", "()V", null, null);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.lookup(other, IsoZombieNetworkComponentPerformancePatchTest.OTHER);
|
||||||
|
other.visitInsn(RETURN);
|
||||||
|
other.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
cw.visitEnd();
|
||||||
|
|
||||||
|
ClassNode node = new ClassNode();
|
||||||
|
new ClassReader(cw.toByteArray()).accept(node, 0);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MethodNode method(ClassNode node, String name)
|
||||||
|
{
|
||||||
|
return node.methods.stream().filter(method -> method.name.equals(name)).findFirst().orElseThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<MethodInsnNode> calls(MethodNode method, String name)
|
||||||
|
{
|
||||||
|
List<MethodInsnNode> calls = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : method.instructions) {
|
||||||
|
if (instruction instanceof MethodInsnNode call && call.name.equals(name)) calls.add(call);
|
||||||
|
}
|
||||||
|
|
||||||
|
return calls;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClassNode patched()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.target();
|
||||||
|
new IsoZombieNetworkComponentPerformancePatch().apply(node);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void anchorMatchesTheTarget()
|
||||||
|
{
|
||||||
|
new IsoZombieNetworkComponentPerformancePatch().anchor().verify(IsoZombieNetworkComponentPerformancePatchTest.target());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addsAPrivateFieldAndAnAccessor()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.patched();
|
||||||
|
|
||||||
|
assertEquals(1, node.fields.size());
|
||||||
|
assertEquals("zomboidForge$networkComponent", node.fields.getFirst().name);
|
||||||
|
assertEquals("L" + IsoZombieNetworkComponentPerformancePatchTest.COMPONENT + ";", node.fields.getFirst().desc);
|
||||||
|
assertTrue((node.fields.getFirst().access & ACC_PRIVATE) != 0);
|
||||||
|
|
||||||
|
MethodNode accessor = IsoZombieNetworkComponentPerformancePatchTest.method(node, "zomboidForge$networkComponent");
|
||||||
|
assertTrue((accessor.access & ACC_PRIVATE) != 0);
|
||||||
|
assertEquals("()L" + IsoZombieNetworkComponentPerformancePatchTest.COMPONENT + ";", accessor.desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void accessorFillsTheFieldOnceFromTheLookup()
|
||||||
|
{
|
||||||
|
MethodNode accessor = IsoZombieNetworkComponentPerformancePatchTest.method(IsoZombieNetworkComponentPerformancePatchTest.patched(), "zomboidForge$networkComponent");
|
||||||
|
|
||||||
|
assertEquals(1, IsoZombieNetworkComponentPerformancePatchTest.calls(accessor, "tryGetECSComponent").size());
|
||||||
|
|
||||||
|
int reads = 0;
|
||||||
|
int writes = 0;
|
||||||
|
for (AbstractInsnNode instruction : accessor.instructions)
|
||||||
|
{
|
||||||
|
if (instruction instanceof FieldInsnNode field && field.getOpcode() == GETFIELD) reads++;
|
||||||
|
if (instruction instanceof FieldInsnNode field && field.getOpcode() == PUTFIELD) writes++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(1, reads);
|
||||||
|
assertEquals(1, writes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void replacesEveryLookupOfTheNetworkComponentWithTheAccessor()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.patched();
|
||||||
|
|
||||||
|
for (String name : List.of("getOwner", "setOwner"))
|
||||||
|
{
|
||||||
|
MethodNode method = IsoZombieNetworkComponentPerformancePatchTest.method(node, name);
|
||||||
|
|
||||||
|
assertTrue(IsoZombieNetworkComponentPerformancePatchTest.calls(method, "tryGetECSComponent").isEmpty(), name);
|
||||||
|
|
||||||
|
List<MethodInsnNode> accessors = IsoZombieNetworkComponentPerformancePatchTest.calls(method, "zomboidForge$networkComponent");
|
||||||
|
assertEquals(1, accessors.size(), name);
|
||||||
|
assertEquals(INVOKESPECIAL, accessors.getFirst().getOpcode(), name);
|
||||||
|
|
||||||
|
// No class literal and no cast remain: the accessor returns the component itself.
|
||||||
|
for (AbstractInsnNode instruction : method.instructions)
|
||||||
|
{
|
||||||
|
assertFalse(instruction instanceof LdcInsnNode, name);
|
||||||
|
assertFalse(instruction.getOpcode() == CHECKCAST, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void leavesLookupsOfOtherComponentsAlone()
|
||||||
|
{
|
||||||
|
MethodNode other = IsoZombieNetworkComponentPerformancePatchTest.method(IsoZombieNetworkComponentPerformancePatchTest.patched(), "other");
|
||||||
|
|
||||||
|
assertEquals(1, IsoZombieNetworkComponentPerformancePatchTest.calls(other, "tryGetECSComponent").size());
|
||||||
|
assertTrue(IsoZombieNetworkComponentPerformancePatchTest.calls(other, "zomboidForge$networkComponent").isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void patchedClassIsWrittenWithComputedFrames()
|
||||||
|
{
|
||||||
|
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.patched().accept(writer);
|
||||||
|
|
||||||
|
assertTrue(writer.toByteArray().length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void failsWhenTheGameClassAlreadyHasTheMember()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.patched();
|
||||||
|
|
||||||
|
assertThrows(IllegalStateException.class, () -> new IsoZombieNetworkComponentPerformancePatch().apply(node));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void appliesOnlyToVerifiedVersions()
|
||||||
|
{
|
||||||
|
IsoZombieNetworkComponentPerformancePatch patch = new IsoZombieNetworkComponentPerformancePatch();
|
||||||
|
|
||||||
|
assertTrue(patch.appliesTo(GameVersion.parse("42.21.0")));
|
||||||
|
assertFalse(patch.appliesTo(GameVersion.parse("42.20.3")));
|
||||||
|
assertFalse(patch.appliesTo(GameVersion.parse("42.22.0")));
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-5
@@ -13,6 +13,7 @@ import org.objectweb.asm.tree.MethodInsnNode;
|
|||||||
import org.objectweb.asm.tree.MethodNode;
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
@@ -114,11 +115,11 @@ class IsoZombieVehiclePerformancePatchTest
|
|||||||
new IsoZombieVehiclePerformancePatch().apply(node);
|
new IsoZombieVehiclePerformancePatch().apply(node);
|
||||||
|
|
||||||
MethodNode method = IsoZombieVehiclePerformancePatchTest.isVehicleBetween(node);
|
MethodNode method = IsoZombieVehiclePerformancePatchTest.isVehicleBetween(node);
|
||||||
JumpInsnNode jump = null;
|
JumpInsnNode jump = Arrays.stream(method.instructions.toArray())
|
||||||
|
.filter(JumpInsnNode.class::isInstance)
|
||||||
for (AbstractInsnNode instruction : method.instructions) {
|
.map(JumpInsnNode.class::cast)
|
||||||
if (instruction instanceof JumpInsnNode candidate) jump = candidate;
|
.findFirst()
|
||||||
}
|
.orElseThrow();
|
||||||
|
|
||||||
// The label sits directly before the original first instruction, iconst_0 of "intersectVision = false".
|
// The label sits directly before the original first instruction, iconst_0 of "intersectVision = false".
|
||||||
AbstractInsnNode firstOriginal = jump.label.getNext();
|
AbstractInsnNode firstOriginal = jump.label.getNext();
|
||||||
|
|||||||
Reference in new issue
Block a user