Add performance improvements for ZomboidFileSystem.init: implement MediaScan and ZomboidFileSystemHooks for efficient file system traversal, and replace directory checks with a single-pass approach. Integrate new patch into the performance patch set, and include unit tests for behavior validation.
This commit is contained in:
1 parent
05a4b9dbd9
commit
38a15db299
6 files changed
+671
-1
No files matched your search
+2
-1
@@ -25,7 +25,8 @@ public final class B42PatchSet implements PatchSet
|
||||
new IsoZombieNetworkComponentPerformancePatch(),
|
||||
new IsoZombieSpotChancePerformancePatch(),
|
||||
new IsoGameCharacterStateMachinePerformancePatch(),
|
||||
new ZombieVoronoiPerformancePatch()
|
||||
new ZombieVoronoiPerformancePatch(),
|
||||
new ZomboidFileSystemInitPerformancePatch()
|
||||
);
|
||||
|
||||
@Override
|
||||
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
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.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.objectweb.asm.tree.VarInsnNode;
|
||||
|
||||
/**
|
||||
* Replaces the call {@code this.searchFolders(workdir)} in {@code ZomboidFileSystem.init} with
|
||||
* {@code ZomboidFileSystemHooks.searchAndIndex}, which also does the work of the loop after it:
|
||||
*
|
||||
* <pre>{@code
|
||||
* ZomboidFileSystemHooks.searchAndIndex(this, this.workdir.canonicalFile, this.loadList, this.allAbsolutePaths);
|
||||
* for (int n = 0; n < this.loadList.size(); ++n) { ... } // loadList is empty now
|
||||
* this.loadList.clear();
|
||||
* }</pre>
|
||||
*
|
||||
* Vanilla checks each of the ~39,000 entries under {@code media/} for being a folder twice, and
|
||||
* builds two URIs per entry for its relative path; the hook walks the folder once.
|
||||
*/
|
||||
public final class ZomboidFileSystemInitPerformancePatch extends PerformancePatch
|
||||
{
|
||||
private static final String OWNER = "zombie/ZomboidFileSystem";
|
||||
private static final Anchor.Method INIT = new Anchor.Method("init", "()V");
|
||||
|
||||
private static final Anchor.Call ANCHOR = new Anchor.Call(
|
||||
ZomboidFileSystemInitPerformancePatch.INIT,
|
||||
ZomboidFileSystemInitPerformancePatch.OWNER, "searchFolders", "(Ljava/io/File;)V"
|
||||
);
|
||||
|
||||
/** The loop the hook takes over: the relative path of each entry. */
|
||||
private static final Anchor.Call RELATIVE_PATH = new Anchor.Call(
|
||||
ZomboidFileSystemInitPerformancePatch.INIT,
|
||||
ZomboidFileSystemInitPerformancePatch.OWNER, "getRelativeFile", "(Ljava/lang/String;)Ljava/lang/String;"
|
||||
);
|
||||
|
||||
/** {@code this.loadList.clear()} after the loop. */
|
||||
private static final Anchor.Call CLEAR = new Anchor.Call(
|
||||
ZomboidFileSystemInitPerformancePatch.INIT,
|
||||
"java/util/List", "clear", "()V"
|
||||
);
|
||||
|
||||
private static final Hook HOOK = new Hook(
|
||||
"dev/overlord/zomboidforge/compat/b42/hooks/ZomboidFileSystemHooks", "searchAndIndex",
|
||||
"(L" + ZomboidFileSystemInitPerformancePatch.OWNER + ";Ljava/io/File;Ljava/util/List;Ljava/util/Set;)V"
|
||||
);
|
||||
|
||||
public ZomboidFileSystemInitPerformancePatch() {
|
||||
super(ZomboidFileSystemInitPerformancePatch.OWNER, ZomboidFileSystemInitPerformancePatch.ANCHOR, ZomboidFileSystemInitPerformancePatch.HOOK);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node)
|
||||
{
|
||||
MethodNode method = ZomboidFileSystemInitPerformancePatch.INIT.resolve(node);
|
||||
MethodInsnNode call = ZomboidFileSystemInitPerformancePatch.ANCHOR.resolve(node);
|
||||
|
||||
ZomboidFileSystemInitPerformancePatch.RELATIVE_PATH.verify(node);
|
||||
ZomboidFileSystemInitPerformancePatch.CLEAR.verify(node);
|
||||
ZomboidFileSystemInitPerformancePatch.requireField(node, "loadList", "Ljava/util/List;");
|
||||
ZomboidFileSystemInitPerformancePatch.requireField(node, "allAbsolutePaths", "Ljava/util/Set;");
|
||||
|
||||
int callIndex = method.instructions.indexOf(call);
|
||||
if (callIndex > method.instructions.indexOf(ZomboidFileSystemInitPerformancePatch.RELATIVE_PATH.resolve(node)))
|
||||
throw new IllegalStateException("'" + node.name + ".init': 'searchFolders' is called after 'getRelativeFile'.");
|
||||
|
||||
// The stack holds this and the folder for the virtual call; the hook takes both, then the two fields.
|
||||
method.instructions.insertBefore(call, ZomboidFileSystemInitPerformancePatch.loadFields(node.name));
|
||||
method.instructions.set(call, new MethodInsnNode(
|
||||
Opcodes.INVOKESTATIC,
|
||||
ZomboidFileSystemInitPerformancePatch.HOOK.owner(),
|
||||
ZomboidFileSystemInitPerformancePatch.HOOK.name(),
|
||||
ZomboidFileSystemInitPerformancePatch.HOOK.desc(),
|
||||
false
|
||||
));
|
||||
}
|
||||
|
||||
/** {@code this.loadList, this.allAbsolutePaths}. */
|
||||
private static InsnList loadFields(String owner)
|
||||
{
|
||||
InsnList code = new InsnList();
|
||||
|
||||
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "loadList", "Ljava/util/List;"));
|
||||
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "allAbsolutePaths", "Ljava/util/Set;"));
|
||||
|
||||
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 + "'.");
|
||||
}
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
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.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.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.ACONST_NULL;
|
||||
import static org.objectweb.asm.Opcodes.ALOAD;
|
||||
import static org.objectweb.asm.Opcodes.CHECKCAST;
|
||||
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.INVOKEVIRTUAL;
|
||||
import static org.objectweb.asm.Opcodes.POP;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class ZomboidFileSystemInitPerformancePatchTest
|
||||
{
|
||||
private static final String FILE_SYSTEM = "zombie/ZomboidFileSystem";
|
||||
private static final String HOOKS = "dev/overlord/zomboidforge/compat/b42/hooks/ZomboidFileSystemHooks";
|
||||
|
||||
/**
|
||||
* {@code ZomboidFileSystem} with a reduced {@code init}:
|
||||
*
|
||||
* <pre>{@code
|
||||
* public void init()
|
||||
* {
|
||||
* this.searchFolders((File) null); // when search is true
|
||||
* this.getRelativeFile((String) null); // when relative is true
|
||||
* this.searchFolders((File) null); // when searchLast is true
|
||||
* this.loadList.clear();
|
||||
* }
|
||||
* }</pre>
|
||||
*/
|
||||
private static ClassNode fileSystem(boolean withSets, boolean search, boolean relative, boolean searchLast)
|
||||
{
|
||||
ClassNode node = new ClassNode();
|
||||
node.visit(V25, ACC_PUBLIC | ACC_FINAL, ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM, null, "java/lang/Object", null);
|
||||
|
||||
node.visitField(ACC_PRIVATE | ACC_FINAL, "loadList", "Ljava/util/List;", null, null);
|
||||
if (withSets) node.visitField(ACC_PRIVATE | ACC_FINAL, "allAbsolutePaths", "Ljava/util/Set;", null, null);
|
||||
|
||||
MethodVisitor constructor = node.visitMethod(ACC_PRIVATE, "<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 init = node.visitMethod(ACC_PUBLIC, "init", "()V", null, null);
|
||||
if (search) ZomboidFileSystemInitPerformancePatchTest.callSearchFolders(init);
|
||||
if (relative)
|
||||
{
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitInsn(ACONST_NULL);
|
||||
init.visitTypeInsn(CHECKCAST, "java/lang/String");
|
||||
init.visitMethodInsn(INVOKEVIRTUAL, ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM, "getRelativeFile", "(Ljava/lang/String;)Ljava/lang/String;", false);
|
||||
init.visitInsn(POP);
|
||||
}
|
||||
if (searchLast) ZomboidFileSystemInitPerformancePatchTest.callSearchFolders(init);
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitFieldInsn(GETFIELD, ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM, "loadList", "Ljava/util/List;");
|
||||
init.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "clear", "()V", true);
|
||||
init.visitInsn(RETURN);
|
||||
init.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static void callSearchFolders(MethodVisitor init)
|
||||
{
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitInsn(ACONST_NULL);
|
||||
init.visitTypeInsn(CHECKCAST, "java/io/File");
|
||||
init.visitMethodInsn(INVOKEVIRTUAL, ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM, "searchFolders", "(Ljava/io/File;)V", false);
|
||||
}
|
||||
|
||||
private static ClassNode fileSystem()
|
||||
{
|
||||
return ZomboidFileSystemInitPerformancePatchTest.fileSystem(true, true, true, false);
|
||||
}
|
||||
|
||||
private static MethodNode init(ClassNode node)
|
||||
{
|
||||
return node.methods.stream().filter(method -> method.name.equals("init")).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 ZomboidFileSystemInitPerformancePatch().anchor().verify(ZomboidFileSystemInitPerformancePatchTest.fileSystem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacesTheCallWithTheHookAndPassesBothFields()
|
||||
{
|
||||
ClassNode node = ZomboidFileSystemInitPerformancePatchTest.fileSystem();
|
||||
new ZomboidFileSystemInitPerformancePatch().apply(node);
|
||||
|
||||
List<AbstractInsnNode> instructions = ZomboidFileSystemInitPerformancePatchTest.instructions(ZomboidFileSystemInitPerformancePatchTest.init(node));
|
||||
|
||||
assertEquals(
|
||||
List.of(ALOAD, ACONST_NULL, CHECKCAST, ALOAD, GETFIELD, ALOAD, GETFIELD, INVOKESTATIC),
|
||||
instructions.subList(0, 8).stream().map(AbstractInsnNode::getOpcode).toList()
|
||||
);
|
||||
assertEquals("loadList", ((FieldInsnNode) instructions.get(4)).name);
|
||||
assertEquals("allAbsolutePaths", ((FieldInsnNode) instructions.get(6)).name);
|
||||
|
||||
MethodInsnNode call = (MethodInsnNode) instructions.get(7);
|
||||
assertEquals(ZomboidFileSystemInitPerformancePatchTest.HOOKS, call.owner);
|
||||
assertEquals("searchAndIndex", call.name);
|
||||
assertEquals("(L" + ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM + ";Ljava/io/File;Ljava/util/List;Ljava/util/Set;)V", call.desc);
|
||||
assertFalse(call.itf);
|
||||
|
||||
// The loop after the call stays as it is.
|
||||
assertTrue(instructions.stream().anyMatch(instruction -> instruction instanceof MethodInsnNode m && m.name.equals("getRelativeFile")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void patchedClassIsValid() throws ClassNotFoundException
|
||||
{
|
||||
ClassNode node = ZomboidFileSystemInitPerformancePatchTest.fileSystem();
|
||||
new ZomboidFileSystemInitPerformancePatch().apply(node);
|
||||
|
||||
// Initializing links the class, which runs the bytecode verifier on the changed method.
|
||||
ClassDefiner definer = new ClassDefiner();
|
||||
definer.define(node);
|
||||
Class.forName(node.name.replace('/', '.'), true, definer);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWithoutTheLoopAfterTheCall()
|
||||
{
|
||||
ClassNode node = ZomboidFileSystemInitPerformancePatchTest.fileSystem(true, true, false, false);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> new ZomboidFileSystemInitPerformancePatch().apply(node));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTheCallComesAfterTheLoop()
|
||||
{
|
||||
ClassNode node = ZomboidFileSystemInitPerformancePatchTest.fileSystem(true, false, true, true);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new ZomboidFileSystemInitPerformancePatch().apply(node));
|
||||
assertTrue(e.getMessage().contains("after 'getRelativeFile'"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWithoutTheAbsolutePathsField()
|
||||
{
|
||||
ClassNode node = ZomboidFileSystemInitPerformancePatchTest.fileSystem(false, true, true, false);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new ZomboidFileSystemInitPerformancePatch().apply(node));
|
||||
assertTrue(e.getMessage().contains("'" + ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM + ".allAbsolutePaths Ljava/util/Set;'"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void appliesOnlyToVerifiedVersions()
|
||||
{
|
||||
ZomboidFileSystemInitPerformancePatch patch = new ZomboidFileSystemInitPerformancePatch();
|
||||
|
||||
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