Add BackupHooks and ParallelZip for Build 42.21. Implement optimized backup file creation with parallel processing. Include hooks, utilities, and unit tests for validation.
ci / image (42, public) (push) Successful in 3m38s

This commit is contained in:
Overlord committed 2026-10-05 13:38:08 +02:00
1 parent 7ffcbdbb06
commit 07dc3f0fad
9 files changed
+1153 -5

No files matched your search

@@ -17,6 +17,7 @@ public final class B42PatchSet implements PatchSet
new ServerStartedPatch(),
new ServerTickPatch(),
new ServerStoppingPatch(),
new ZipBackupSinglePassPerformancePatch(),
new BackupStoragePatch(),
new CommandLookupPatch(),
new CommandListPatch(),
@@ -0,0 +1,60 @@
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.MethodBody;
import dev.overlord.zomboidforge.patcher.patches.PerformancePatch;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
/**
* Replaces the body of {@code ZipBackup.makeBackupFile(String, BackupTypes)} with a call to
* {@code BackupHooks.makeBackupFile}, which writes the same zip in one pass:
*
* <pre>{@code
* private static void makeBackupFile(String serverName, BackupTypes type)
* {
* BackupHooks.makeBackupFile(serverName, type);
* }
* }</pre>
*
* Vanilla's {@code ParallelScatterZipCreator} copies every file into a temporary file per thread
* and then into the zip: about three times the size of the save written per backup. For an
* explored world of 880 MiB in 365,538 files, the startup backup takes 15.2 to 16.8 s and writes
* 2.9 GB; in one pass 7.5 s and 1.0 GB, with the same entries. The save folder is read under
* {@code IsoChunk.WriteLock}, as vanilla; chunk saves wait for it half as long.
*
* <p>Registered before {@code BackupStoragePatch}, which renames this method and wraps it: the
* anchor requires vanilla's body, and the storage hooks stay around the whole backup.
*/
public final class ZipBackupSinglePassPerformancePatch extends PerformancePatch
{
private static final Anchor.Call ANCHOR = new Anchor.Call(
new Anchor.Method("makeBackupFile", "(Ljava/lang/String;Lzombie/core/backup/ZipBackup$BackupTypes;)V"),
"org/apache/commons/compress/archivers/zip/ParallelScatterZipCreator", "writeTo", "(Lorg/apache/commons/compress/archivers/zip/ZipArchiveOutputStream;)V"
);
// BackupTypes is private: the hook takes it as Object.
private static final Hook HOOK = new Hook(
"dev/overlord/zomboidforge/compat/b42/hooks/BackupHooks", "makeBackupFile", "(Ljava/lang/String;Ljava/lang/Object;)V"
);
public ZipBackupSinglePassPerformancePatch() {
super("zombie/core/backup/ZipBackup", ZipBackupSinglePassPerformancePatch.ANCHOR, ZipBackupSinglePassPerformancePatch.HOOK);
}
//
@Override
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
@Override
public void apply(ClassNode node)
{
MethodNode method = ZipBackupSinglePassPerformancePatch.ANCHOR.in().resolve(node);
MethodBody.delegate(node, method, ZipBackupSinglePassPerformancePatch.HOOK);
}
}
@@ -0,0 +1,157 @@
package dev.overlord.zomboidforge.compat.b42.perf;
import dev.overlord.zomboidforge.api.GameVersion;
import dev.overlord.zomboidforge.compat.b42.B42PatchSet;
import dev.overlord.zomboidforge.compat.b42.patches.BackupStoragePatch;
import dev.overlord.zomboidforge.compat.b42.patches.support.ClassDefiner;
import dev.overlord.zomboidforge.patcher.ClassPatch;
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.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.ACC_STATIC;
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.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V25;
class ZipBackupSinglePassPerformancePatchTest
{
private static final String ZIP_BACKUP = "zombie/core/backup/ZipBackup";
private static final String DESC = "(Ljava/lang/String;Lzombie/core/backup/ZipBackup$BackupTypes;)V";
private static final String SCATTER = "org/apache/commons/compress/archivers/zip/ParallelScatterZipCreator";
private static final String STREAM = "org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream";
/** {@code ZipBackup} with a reduced {@code makeBackupFile}: {@code ((ParallelScatterZipCreator) null).writeTo(null);} */
private static ClassNode zipBackup()
{
ClassNode node = new ClassNode();
node.visit(V25, ACC_PUBLIC, ZipBackupSinglePassPerformancePatchTest.ZIP_BACKUP, null, "java/lang/Object", null);
MethodVisitor make = node.visitMethod(ACC_PRIVATE | ACC_STATIC, "makeBackupFile", ZipBackupSinglePassPerformancePatchTest.DESC, null, null);
make.visitInsn(ACONST_NULL);
make.visitTypeInsn(CHECKCAST, ZipBackupSinglePassPerformancePatchTest.SCATTER);
make.visitInsn(ACONST_NULL);
make.visitTypeInsn(CHECKCAST, ZipBackupSinglePassPerformancePatchTest.STREAM);
make.visitMethodInsn(INVOKEVIRTUAL, ZipBackupSinglePassPerformancePatchTest.SCATTER, "writeTo", "(L" + ZipBackupSinglePassPerformancePatchTest.STREAM + ";)V", false);
make.visitInsn(RETURN);
make.visitMaxs(0, 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<String> calls(MethodNode method)
{
List<String> calls = new ArrayList<>();
for (AbstractInsnNode instruction : method.instructions) {
if (instruction instanceof MethodInsnNode call) calls.add(call.owner.substring(call.owner.lastIndexOf('/') + 1) + "." + call.name);
}
return calls;
}
@Test
void anchorMatchesTheTarget()
{
new ZipBackupSinglePassPerformancePatch().anchor().verify(ZipBackupSinglePassPerformancePatchTest.zipBackup());
}
@Test
void replacesTheBodyWithTheHookCall()
{
ClassNode node = ZipBackupSinglePassPerformancePatchTest.zipBackup();
new ZipBackupSinglePassPerformancePatch().apply(node);
List<AbstractInsnNode> instructions = new ArrayList<>();
for (AbstractInsnNode instruction : ZipBackupSinglePassPerformancePatchTest.method(node, "makeBackupFile").instructions) {
if (instruction.getOpcode() != -1) instructions.add(instruction);
}
assertEquals(List.of(ALOAD, ALOAD, INVOKESTATIC, RETURN), instructions.stream().map(AbstractInsnNode::getOpcode).toList());
MethodInsnNode call = (MethodInsnNode) instructions.get(2);
assertEquals("dev/overlord/zomboidforge/compat/b42/hooks/BackupHooks", call.owner);
assertEquals("makeBackupFile", call.name);
assertEquals("(Ljava/lang/String;Ljava/lang/Object;)V", call.desc);
}
@Test
void patchedClassIsValid() throws ClassNotFoundException
{
ClassNode node = ZipBackupSinglePassPerformancePatchTest.zipBackup();
new ZipBackupSinglePassPerformancePatch().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);
}
/** As in {@link B42PatchSet}: this patch first, then the storage wrapper around the hook call. */
@Test
void storagePatchWrapsTheReplacedBody()
{
ClassNode node = ZipBackupSinglePassPerformancePatchTest.zipBackup();
BackupStoragePatch storage = new BackupStoragePatch();
new ZipBackupSinglePassPerformancePatch().apply(node);
storage.anchor().verify(node);
storage.apply(node);
assertEquals(List.of("StorageHooks.backupRunning", "ZipBackup.zomboidForge$makeBackupFile", "StorageHooks.backupRunning", "StorageHooks.backupRunning"),
ZipBackupSinglePassPerformancePatchTest.calls(ZipBackupSinglePassPerformancePatchTest.method(node, "makeBackupFile")));
assertEquals(List.of("BackupHooks.makeBackupFile"),
ZipBackupSinglePassPerformancePatchTest.calls(ZipBackupSinglePassPerformancePatchTest.method(node, "zomboidForge$makeBackupFile")));
}
@Test
void registeredBeforeTheStoragePatch()
{
List<String> names = new B42PatchSet().patches().stream().map(ClassPatch::getClass).map(Class::getSimpleName).toList();
assertTrue(names.indexOf("ZipBackupSinglePassPerformancePatch") >= 0);
assertTrue(names.indexOf("ZipBackupSinglePassPerformancePatch") < names.indexOf("BackupStoragePatch"));
}
/** After the storage patch, {@code makeBackupFile} is the wrapper, which the hook must not replace. */
@Test
void anchorFailsOnTheStorageWrapper()
{
ClassNode node = ZipBackupSinglePassPerformancePatchTest.zipBackup();
new BackupStoragePatch().apply(node);
assertThrows(IllegalStateException.class, () -> new ZipBackupSinglePassPerformancePatch().anchor().verify(node));
}
@Test
void appliesOnlyToVerifiedVersions()
{
ZipBackupSinglePassPerformancePatch patch = new ZipBackupSinglePassPerformancePatch();
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")));
}
}