Add ThreadSleepPerformancePatch variants for Build 42.21 to optimize waiting loops. Replace Thread.sleep calls with hook-based alternatives. Include hooks, utility and tests.
This commit is contained in:
1 parent
529e48ed5a
commit
88cd68b5cb
12 files changed
+603
-1
No files matched your search
+74
@@ -0,0 +1,74 @@
|
||||
package dev.overlord.zomboidforge.patcher.Patches;
|
||||
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Redirects the {@code Thread.sleep(long)} calls of one method to a {@link Hook} with the same
|
||||
* signature, {@code (J)V}. For waiting loops that check a condition, sleep, and check again: the
|
||||
* hook sleeps shorter or can be woken, while the loop's condition stays the vanilla one. Fails
|
||||
* unless the method has exactly the expected number of sleeps. {@link #appliesTo} is left to the
|
||||
* subclass, as for every {@link PerformancePatch}.
|
||||
*/
|
||||
public abstract class ThreadSleepPerformancePatch extends PerformancePatch
|
||||
{
|
||||
private static final String SLEEP = "(J)V";
|
||||
|
||||
private final Anchor.Method method;
|
||||
private final int sleeps;
|
||||
private final Hook hook;
|
||||
|
||||
/**
|
||||
* @param target internal name of the class
|
||||
* @param method the method with the waiting loop
|
||||
* @param sleeps the number of {@code Thread.sleep(long)} calls in it
|
||||
* @param hook the replacement for {@code Thread.sleep(long)}, {@code (J)V}
|
||||
*/
|
||||
protected ThreadSleepPerformancePatch(String target, Anchor.Method method, int sleeps, Hook hook)
|
||||
{
|
||||
super(target, method, hook);
|
||||
|
||||
if (!hook.desc().equals(ThreadSleepPerformancePatch.SLEEP))
|
||||
throw new IllegalArgumentException("The hook '" + hook.owner() + "." + hook.name() + hook.desc() + "' must have the signature of 'Thread.sleep(long)', (J)V.");
|
||||
if (sleeps < 1)
|
||||
throw new IllegalArgumentException("A method with " + sleeps + " sleeps has nothing to redirect.");
|
||||
|
||||
this.method = method;
|
||||
this.sleeps = sleeps;
|
||||
this.hook = hook;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node)
|
||||
{
|
||||
MethodNode method = this.method.resolve(node);
|
||||
List<MethodInsnNode> found = new ArrayList<>();
|
||||
|
||||
for (AbstractInsnNode instruction : method.instructions)
|
||||
{
|
||||
if (instruction instanceof MethodInsnNode call && call.getOpcode() == Opcodes.INVOKESTATIC
|
||||
&& call.owner.equals("java/lang/Thread") && call.name.equals("sleep") && call.desc.equals(ThreadSleepPerformancePatch.SLEEP))
|
||||
found.add(call);
|
||||
}
|
||||
|
||||
if (found.size() != this.sleeps)
|
||||
throw new IllegalStateException("Expected " + this.sleeps + " call(s) to 'Thread.sleep(long)' in '" + node.name + "." + method.name + method.desc + "', found " + found.size() + ".");
|
||||
|
||||
for (MethodInsnNode call : found)
|
||||
{
|
||||
call.owner = this.hook.owner();
|
||||
call.name = this.hook.name();
|
||||
}
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package dev.overlord.zomboidforge.patcher;
|
||||
|
||||
import dev.overlord.zomboidforge.api.GameVersion;
|
||||
import dev.overlord.zomboidforge.patcher.Patches.ThreadSleepPerformancePatch;
|
||||
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
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.INVOKESTATIC;
|
||||
import static org.objectweb.asm.Opcodes.LCONST_1;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class ThreadSleepPerformancePatchTest
|
||||
{
|
||||
private static final Hook HOOK = new Hook("t/WaitHooks", "shortSleep", "(J)V");
|
||||
|
||||
private static final class Patch extends ThreadSleepPerformancePatch
|
||||
{
|
||||
Patch(int sleeps, Hook hook) { super("t/Target", new Anchor.Method("waitLoop", "()V"), sleeps, hook); }
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) { return true; }
|
||||
}
|
||||
|
||||
/** {@code t/Target.waitLoop()} with {@code sleeps} calls of {@code Thread.sleep(1L)} and one of {@code Thread.yield()}. */
|
||||
private static ClassNode target(int sleeps)
|
||||
{
|
||||
ClassNode node = new ClassNode();
|
||||
node.visit(V25, ACC_PUBLIC, "t/Target", null, "java/lang/Object", null);
|
||||
|
||||
MethodVisitor method = node.visitMethod(ACC_PUBLIC, "waitLoop", "()V", null, null);
|
||||
for (int i = 0; i < sleeps; i++)
|
||||
{
|
||||
method.visitInsn(LCONST_1);
|
||||
method.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V", false);
|
||||
}
|
||||
method.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "yield", "()V", false);
|
||||
method.visitInsn(RETURN);
|
||||
method.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static List<String> calls(ClassNode node)
|
||||
{
|
||||
List<String> calls = new ArrayList<>();
|
||||
|
||||
for (AbstractInsnNode instruction : node.methods.getFirst().instructions) {
|
||||
if (instruction instanceof MethodInsnNode call) calls.add(call.owner + "." + call.name + call.desc);
|
||||
}
|
||||
|
||||
return calls;
|
||||
}
|
||||
|
||||
@Test
|
||||
void redirectsEverySleepAndNothingElse()
|
||||
{
|
||||
ClassNode node = ThreadSleepPerformancePatchTest.target(2);
|
||||
new Patch(2, ThreadSleepPerformancePatchTest.HOOK).apply(node);
|
||||
|
||||
assertEquals(List.of("t/WaitHooks.shortSleep(J)V", "t/WaitHooks.shortSleep(J)V", "java/lang/Thread.yield()V"), ThreadSleepPerformancePatchTest.calls(node));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsForAnotherNumberOfSleeps()
|
||||
{
|
||||
ClassNode node = ThreadSleepPerformancePatchTest.target(1);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new Patch(2, ThreadSleepPerformancePatchTest.HOOK).apply(node));
|
||||
assertTrue(e.getMessage().contains("Expected 2 call(s)") && e.getMessage().contains("found 1"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAHookWithAnotherSignature()
|
||||
{
|
||||
assertThrows(IllegalArgumentException.class, () -> new Patch(1, new Hook("t/WaitHooks", "shortSleep", "(I)V")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsZeroSleeps()
|
||||
{
|
||||
assertThrows(IllegalArgumentException.class, () -> new Patch(0, ThreadSleepPerformancePatchTest.HOOK));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user