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 35a5325..fc8dd30 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
@@ -30,7 +30,8 @@ public final class B42PatchSet implements PatchSet
new ServerMapCellBudgetPerformancePatch(),
new WorldGenUtilsPlacementPerformancePatch(),
new WorldGenChunkRulesPerformancePatch(),
- new BitHeaderPoolPerformancePatch()
+ new BitHeaderPoolPerformancePatch(),
+ new IsoChunkSaveDirectoryPerformancePatch()
);
@Override
diff --git a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/IsoChunkSaveDirectoryPerformancePatch.java b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/IsoChunkSaveDirectoryPerformancePatch.java
new file mode 100644
index 0000000..3af34df
--- /dev/null
+++ b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/IsoChunkSaveDirectoryPerformancePatch.java
@@ -0,0 +1,85 @@
+package dev.overlord.zomboidforge.compat.b42.perf;
+
+import dev.overlord.zomboidforge.api.GameVersion;
+import dev.overlord.zomboidforge.patcher.Anchor;
+import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch;
+
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.tree.AbstractInsnNode;
+import org.objectweb.asm.tree.ClassNode;
+import org.objectweb.asm.tree.JumpInsnNode;
+import org.objectweb.asm.tree.MethodNode;
+import org.objectweb.asm.tree.VarInsnNode;
+
+import java.util.List;
+
+/**
+ * Removes the folder check before every chunk save in {@code IsoChunk.Save(boolean)}:
+ *
+ *
{@code
+ * File testDir = ChunkMapFilenames.instance.getDir(Core.gameSaveWorld);
+ * if (!testDir.exists()) { // removed
+ * testDir.mkdir();
+ * }
+ * }
+ *
+ * {@code getDir} returns {@code /map/}, which nothing reads or writes; chunk files
+ * are saved under {@code //map//}, whose folders {@code getFilename} creates.
+ * {@code /map} does not exist, so the {@code mkdir} fails on every save: one {@code stat}
+ * and one failed {@code mkdir} per chunk. The {@code getDir} call stays; it initializes the cache
+ * folder of {@code ChunkMapFilenames} on first use.
+ */
+public final class IsoChunkSaveDirectoryPerformancePatch extends PerformancePatch
+{
+ /** {@code testDir = ...getDir(world); if (!testDir.exists()) testDir.mkdir();} */
+ private static final Anchor.Sequence ANCHOR = new Anchor.Sequence(
+ new Anchor.Method("Save", "(Z)V"),
+ new Anchor.Instruction.Invoke("zombie/ChunkMapFilenames", "getDir", "(Ljava/lang/String;)Ljava/io/File;"),
+ new Anchor.Instruction.Opcode(Opcodes.ASTORE),
+ new Anchor.Instruction.Opcode(Opcodes.ALOAD),
+ new Anchor.Instruction.Invoke("java/io/File", "exists", "()Z"),
+ new Anchor.Instruction.Opcode(Opcodes.IFNE),
+ new Anchor.Instruction.Opcode(Opcodes.ALOAD),
+ new Anchor.Instruction.Invoke("java/io/File", "mkdir", "()Z"),
+ new Anchor.Instruction.Opcode(Opcodes.POP)
+ );
+
+ public IsoChunkSaveDirectoryPerformancePatch() {
+ super("zombie/iso/IsoChunk", IsoChunkSaveDirectoryPerformancePatch.ANCHOR, null);
+ }
+
+ //
+
+ @Override
+ public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
+
+ @Override
+ public void apply(ClassNode node)
+ {
+ MethodNode method = IsoChunkSaveDirectoryPerformancePatch.ANCHOR.in().resolve(node);
+ List matched = IsoChunkSaveDirectoryPerformancePatch.ANCHOR.resolve(node);
+ int testDir = ((VarInsnNode) matched.get(1)).var;
+ JumpInsnNode skip = (JumpInsnNode) matched.get(4);
+
+ if (((VarInsnNode) matched.get(2)).var != testDir || ((VarInsnNode) matched.get(5)).var != testDir)
+ throw new IllegalStateException("'" + node.name + ".Save': the check does not use the folder that 'getDir' returned.");
+
+ if (IsoChunkSaveDirectoryPerformancePatch.nextInstruction(skip.label) != IsoChunkSaveDirectoryPerformancePatch.nextInstruction(matched.getLast()))
+ throw new IllegalStateException("'" + node.name + ".Save': the folder check does not jump to the end of its block.");
+
+ // The if-block: from loading testDir for exists() to the pop after mkdir(). Labels stay,
+ // so the jump target and the local variable table remain valid.
+ for (AbstractInsnNode instruction : matched.subList(2, matched.size())) method.instructions.remove(instruction);
+ }
+
+ /** The closest later instruction that is not a label, line number, or frame. */
+ private static AbstractInsnNode nextInstruction(AbstractInsnNode instruction)
+ {
+ AbstractInsnNode next = instruction.getNext();
+ while (next != null && next.getOpcode() == -1) next = next.getNext();
+
+ if (next == null) throw new IllegalStateException("'IsoChunk.Save' ends after the folder check.");
+
+ return next;
+ }
+}
diff --git a/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/IsoChunkSaveDirectoryPerformancePatchTest.java b/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/IsoChunkSaveDirectoryPerformancePatchTest.java
new file mode 100644
index 0000000..0a4d5d1
--- /dev/null
+++ b/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/IsoChunkSaveDirectoryPerformancePatchTest.java
@@ -0,0 +1,193 @@
+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.junit.jupiter.api.io.TempDir;
+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.MethodInsnNode;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+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.ALOAD;
+import static org.objectweb.asm.Opcodes.ARETURN;
+import static org.objectweb.asm.Opcodes.ASTORE;
+import static org.objectweb.asm.Opcodes.DUP;
+import static org.objectweb.asm.Opcodes.GETSTATIC;
+import static org.objectweb.asm.Opcodes.IFNE;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+import static org.objectweb.asm.Opcodes.NEW;
+import static org.objectweb.asm.Opcodes.POP;
+import static org.objectweb.asm.Opcodes.RETURN;
+import static org.objectweb.asm.Opcodes.V25;
+
+class IsoChunkSaveDirectoryPerformancePatchTest
+{
+ private static final String CHUNK = "zombie/iso/IsoChunk";
+ private static final String FILENAMES = "zombie/ChunkMapFilenames";
+
+ /**
+ * {@code IsoChunk} with a reduced {@code Save(boolean)}:
+ *
+ * {@code
+ * public static String world;
+ *
+ * public void Save(boolean bPreventChunkReuse)
+ * {
+ * File testDir = new ChunkMapFilenames().getDir(world);
+ * if (!testDir.exists()) testDir.mkdir();
+ * }
+ * }
+ */
+ private static ClassNode chunk()
+ {
+ return IsoChunkSaveDirectoryPerformancePatchTest.chunk(2);
+ }
+
+ /** {@link #chunk()} whose {@code mkdir} call uses the local {@code mkdirLocal} (the folder is in 2). */
+ private static ClassNode chunk(int mkdirLocal)
+ {
+ ClassNode node = IsoChunkSaveDirectoryPerformancePatchTest.emptyClass(IsoChunkSaveDirectoryPerformancePatchTest.CHUNK);
+ node.visitField(ACC_PUBLIC | ACC_STATIC, "world", "Ljava/lang/String;", null, null);
+
+ MethodVisitor save = node.visitMethod(ACC_PUBLIC, "Save", "(Z)V", null, null);
+ Label exists = new Label();
+
+ save.visitTypeInsn(NEW, IsoChunkSaveDirectoryPerformancePatchTest.FILENAMES);
+ save.visitInsn(DUP);
+ save.visitMethodInsn(INVOKESPECIAL, IsoChunkSaveDirectoryPerformancePatchTest.FILENAMES, "", "()V", false);
+ save.visitFieldInsn(GETSTATIC, IsoChunkSaveDirectoryPerformancePatchTest.CHUNK, "world", "Ljava/lang/String;");
+ save.visitMethodInsn(INVOKEVIRTUAL, IsoChunkSaveDirectoryPerformancePatchTest.FILENAMES, "getDir", "(Ljava/lang/String;)Ljava/io/File;", false);
+ save.visitVarInsn(ASTORE, 2);
+ save.visitVarInsn(ALOAD, 2);
+ save.visitMethodInsn(INVOKEVIRTUAL, "java/io/File", "exists", "()Z", false);
+ save.visitJumpInsn(IFNE, exists);
+ save.visitVarInsn(ALOAD, mkdirLocal);
+ save.visitMethodInsn(INVOKEVIRTUAL, "java/io/File", "mkdir", "()Z", false);
+ save.visitInsn(POP);
+ save.visitLabel(exists);
+ save.visitInsn(RETURN);
+ save.visitMaxs(0, 0);
+
+ return node;
+ }
+
+ /** {@code ChunkMapFilenames} whose {@code getDir(path)} is {@code new File(path)}. */
+ private static ClassNode filenames()
+ {
+ ClassNode node = IsoChunkSaveDirectoryPerformancePatchTest.emptyClass(IsoChunkSaveDirectoryPerformancePatchTest.FILENAMES);
+ MethodVisitor getDir = node.visitMethod(ACC_PUBLIC, "getDir", "(Ljava/lang/String;)Ljava/io/File;", null, null);
+
+ getDir.visitTypeInsn(NEW, "java/io/File");
+ getDir.visitInsn(DUP);
+ getDir.visitVarInsn(ALOAD, 1);
+ getDir.visitMethodInsn(INVOKESPECIAL, "java/io/File", "", "(Ljava/lang/String;)V", false);
+ getDir.visitInsn(ARETURN);
+ getDir.visitMaxs(0, 0);
+
+ return node;
+ }
+
+ private static ClassNode emptyClass(String name)
+ {
+ ClassNode node = new ClassNode();
+ node.visit(V25, ACC_PUBLIC, name, null, "java/lang/Object", null);
+
+ MethodVisitor init = node.visitMethod(ACC_PUBLIC, "", "()V", null, null);
+ init.visitVarInsn(ALOAD, 0);
+ init.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V", false);
+ init.visitInsn(RETURN);
+ init.visitMaxs(0, 0);
+
+ return node;
+ }
+
+ /** Runs {@code Save(false)} of {@code chunk} with {@code folder} as the checked folder. */
+ private static void save(ClassNode chunk, Path folder) throws ReflectiveOperationException
+ {
+ ClassDefiner definer = new ClassDefiner();
+ definer.define(IsoChunkSaveDirectoryPerformancePatchTest.filenames());
+ Class> loaded = definer.define(chunk);
+
+ loaded.getField("world").set(null, folder.toString());
+ loaded.getMethod("Save", boolean.class).invoke(loaded.getConstructor().newInstance(), false);
+ }
+
+ @Test
+ void anchorMatchesTheTarget()
+ {
+ new IsoChunkSaveDirectoryPerformancePatch().anchor().verify(IsoChunkSaveDirectoryPerformancePatchTest.chunk());
+ }
+
+ @Test
+ void vanillaCreatesTheFolder(@TempDir Path temp) throws ReflectiveOperationException
+ {
+ Path folder = temp.resolve("map");
+ IsoChunkSaveDirectoryPerformancePatchTest.save(IsoChunkSaveDirectoryPerformancePatchTest.chunk(), folder);
+
+ assertTrue(Files.isDirectory(folder));
+ }
+
+ @Test
+ void patchedSaveKeepsGetDirAndDropsTheCheck(@TempDir Path temp) throws ReflectiveOperationException
+ {
+ ClassNode chunk = IsoChunkSaveDirectoryPerformancePatchTest.chunk();
+ new IsoChunkSaveDirectoryPerformancePatch().apply(chunk);
+
+ List calls = new ArrayList<>();
+ for (AbstractInsnNode instruction : chunk.methods.getLast().instructions) {
+ if (instruction instanceof MethodInsnNode call) calls.add(call.name);
+ }
+ assertEquals(List.of("", "getDir"), calls);
+
+ Path folder = temp.resolve("map");
+ IsoChunkSaveDirectoryPerformancePatchTest.save(chunk, folder);
+
+ assertFalse(Files.exists(folder));
+ }
+
+ @Test
+ void failsWhenTheCheckUsesAnotherFolder()
+ {
+ ClassNode chunk = IsoChunkSaveDirectoryPerformancePatchTest.chunk(1);
+
+ IllegalStateException e = assertThrows(IllegalStateException.class, () -> new IsoChunkSaveDirectoryPerformancePatch().apply(chunk));
+ assertTrue(e.getMessage().contains("does not use the folder"), e.getMessage());
+ }
+
+ @Test
+ void failsWithoutTheFolderCheck()
+ {
+ ClassNode chunk = IsoChunkSaveDirectoryPerformancePatchTest.emptyClass(IsoChunkSaveDirectoryPerformancePatchTest.CHUNK);
+ MethodVisitor save = chunk.visitMethod(ACC_PUBLIC, "Save", "(Z)V", null, null);
+ save.visitInsn(RETURN);
+ save.visitMaxs(0, 0);
+
+ assertThrows(IllegalStateException.class, () -> new IsoChunkSaveDirectoryPerformancePatch().apply(chunk));
+ }
+
+ @Test
+ void appliesOnlyToVerifiedVersions()
+ {
+ IsoChunkSaveDirectoryPerformancePatch patch = new IsoChunkSaveDirectoryPerformancePatch();
+
+ 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")));
+ }
+}