Add RolesSavePerformancePatch to optimize role saving in Build 42.21. Use single SQLite transaction to reduce redundant database operations. Include hooks, tests, and integrate the patch into the performance patch set.

This commit is contained in:
Overlord committed 2026-10-05 11:24:59 +02:00
1 parent dd113961e6
commit 529e48ed5a
7 files changed
+518 -2

No files matched your search

@@ -4,14 +4,17 @@ 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.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import org.objectweb.asm.tree.VarInsnNode;
import java.util.Arrays;
/** Replaces the body of a method of the target class with a call to a {@link Hook}. */
/** Replaces the body of a method of the target class with a call to a {@link Hook}, or wraps it in hook calls. */
public final class MethodBody
{
private MethodBody() {}
@@ -56,6 +59,70 @@ public final class MethodBody
method.instructions.add(new InsnNode(type.getReturnType().getOpcode(Opcodes.IRETURN)));
}
/**
* Wraps the static void method {@code method}: renames it to {@code renamed} and adds a method
* with its name, descriptor and access that runs
*
* <pre>{@code
* before();
* try { renamed(arguments); }
* finally { after(); }
* }</pre>
*
* {@code before} and {@code after} take no arguments. Fails for instance methods, methods
* that return a value, and when {@code node} already has a method named {@code renamed}.
*/
public static void wrap(ClassNode node, MethodNode method, String renamed, Hook before, Hook after)
{
String name = "'" + node.name + "." + method.name + method.desc + "'";
if ((method.access & Opcodes.ACC_STATIC) == 0)
throw new IllegalStateException(name + " is not static.");
if (Type.getReturnType(method.desc) != Type.VOID_TYPE)
throw new IllegalStateException(name + " returns a value.");
if (node.methods.stream().anyMatch(other -> other.name.equals(renamed)))
throw new IllegalStateException("'" + node.name + "' already has a method '" + renamed + "'.");
MethodNode wrapper = new MethodNode(method.access, method.name, method.desc, method.signature, method.exceptions.toArray(String[]::new));
method.name = renamed;
LabelNode start = new LabelNode();
LabelNode end = new LabelNode();
LabelNode handler = new LabelNode();
InsnList code = wrapper.instructions;
int slot = 0;
code.add(MethodBody.call(before));
code.add(start);
for (Type argument : Type.getArgumentTypes(method.desc))
{
code.add(new VarInsnNode(argument.getOpcode(Opcodes.ILOAD), slot));
slot += argument.getSize();
}
code.add(new MethodInsnNode(Opcodes.INVOKESTATIC, node.name, renamed, method.desc, (node.access & Opcodes.ACC_INTERFACE) != 0));
code.add(end);
code.add(MethodBody.call(after));
code.add(new InsnNode(Opcodes.RETURN));
// finally on the exception path: run after(), rethrow; the exception goes to the first free slot.
code.add(handler);
code.add(new VarInsnNode(Opcodes.ASTORE, slot));
code.add(MethodBody.call(after));
code.add(new VarInsnNode(Opcodes.ALOAD, slot));
code.add(new InsnNode(Opcodes.ATHROW));
wrapper.tryCatchBlocks.add(new TryCatchBlockNode(start, end, handler, null));
node.methods.add(wrapper);
}
private static MethodInsnNode call(Hook hook)
{
if (!hook.desc().equals("()V"))
throw new IllegalStateException("The hook '" + hook.owner() + "." + hook.name() + hook.desc() + "' must take no arguments and return nothing.");
return new MethodInsnNode(Opcodes.INVOKESTATIC, hook.owner(), hook.name(), hook.desc(), false);
}
/** Whether the method reads or writes local variable 0, which holds {@code this}. */
private static boolean usesThis(MethodNode method)
{
@@ -6,8 +6,10 @@ import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -18,9 +20,11 @@ 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.ICONST_0;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.LCONST_0;
import static org.objectweb.asm.Opcodes.LLOAD;
import static org.objectweb.asm.Opcodes.LRETURN;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.RETURN;
@@ -41,6 +45,17 @@ public class MethodBodyTest
public static void record(String value) { MethodBodyTest.recorded = value; }
/** Calls of the hooks below, in order. */
public static final List<String> EVENTS = new ArrayList<>();
public static void before() { MethodBodyTest.EVENTS.add("before"); }
public static void after() { MethodBodyTest.EVENTS.add("after"); }
public static void work(long value, String text) { MethodBodyTest.EVENTS.add("work " + value + " " + text); }
public static void fail() { throw new IllegalStateException("failed"); }
/** Defines classes from nodes, with the test classes as parent so the hooks resolve. */
private static final class Definer extends ClassLoader
{
@@ -177,4 +192,80 @@ public class MethodBodyTest
);
assertTrue(e.getMessage().contains("has no body"), e.getMessage());
}
/** {@code t/Target} with {@code public static void run(long, String)} calling {@code body} with its arguments or none. */
private static ClassNode wrappable(String body, String bodyDesc)
{
ClassNode node = MethodBodyTest.target(ACC_PUBLIC | ACC_STATIC, "placeholder", "()V", false);
MethodVisitor run = node.visitMethod(ACC_PUBLIC | ACC_STATIC, "run", "(JLjava/lang/String;)V", null, null);
if (bodyDesc.equals("(JLjava/lang/String;)V"))
{
run.visitVarInsn(LLOAD, 0);
run.visitVarInsn(ALOAD, 2);
}
run.visitMethodInsn(INVOKESTATIC, MethodBodyTest.HOOKS, body, bodyDesc, false);
run.visitInsn(RETURN);
run.visitMaxs(0, 0);
return node;
}
private static final Hook BEFORE = new Hook(MethodBodyTest.HOOKS, "before", "()V");
private static final Hook AFTER = new Hook(MethodBodyTest.HOOKS, "after", "()V");
@Test
void wrapRunsBeforeTheOriginalWithItsArgumentsThenAfter() throws ReflectiveOperationException
{
ClassNode node = MethodBodyTest.wrappable("work", "(JLjava/lang/String;)V");
MethodBody.wrap(node, MethodBodyTest.method(node, "run"), "original$run", MethodBodyTest.BEFORE, MethodBodyTest.AFTER);
MethodBodyTest.EVENTS.clear();
MethodBodyTest.run(node, "run", new Class<?>[] { long.class, String.class }, 42L, "abc");
assertEquals(List.of("before", "work 42 abc", "after"), MethodBodyTest.EVENTS);
}
@Test
void wrapRunsAfterAndRethrowsWhenTheOriginalThrows()
{
ClassNode node = MethodBodyTest.wrappable("fail", "()V");
MethodBody.wrap(node, MethodBodyTest.method(node, "run"), "original$run", MethodBodyTest.BEFORE, MethodBodyTest.AFTER);
MethodBodyTest.EVENTS.clear();
InvocationTargetException e = assertThrows(InvocationTargetException.class, () ->
MethodBodyTest.run(node, "run", new Class<?>[] { long.class, String.class }, 1L, "x")
);
assertTrue(e.getCause() instanceof IllegalStateException);
assertEquals("failed", e.getCause().getMessage());
assertEquals(List.of("before", "after"), MethodBodyTest.EVENTS);
}
@Test
void wrapKeepsTheOriginalUnderTheNewName()
{
ClassNode node = MethodBodyTest.wrappable("work", "(JLjava/lang/String;)V");
MethodBody.wrap(node, MethodBodyTest.method(node, "run"), "original$run", MethodBodyTest.BEFORE, MethodBodyTest.AFTER);
assertEquals(1, node.methods.stream().filter(method -> method.name.equals("run")).count());
assertEquals(1, node.methods.stream().filter(method -> method.name.equals("original$run") && method.desc.equals("(JLjava/lang/String;)V")).count());
}
@Test
void wrapFailsForInstanceMethodsValuesTakenNamesAndHooksWithArguments()
{
ClassNode instance = MethodBodyTest.target(ACC_PUBLIC, "run", "()V", false);
assertThrows(IllegalStateException.class, () -> MethodBody.wrap(instance, MethodBodyTest.method(instance, "run"), "original$run", MethodBodyTest.BEFORE, MethodBodyTest.AFTER));
ClassNode value = MethodBodyTest.target(ACC_PUBLIC | ACC_STATIC, "run", "()I", false);
assertThrows(IllegalStateException.class, () -> MethodBody.wrap(value, MethodBodyTest.method(value, "run"), "original$run", MethodBodyTest.BEFORE, MethodBodyTest.AFTER));
ClassNode taken = MethodBodyTest.wrappable("work", "(JLjava/lang/String;)V");
assertThrows(IllegalStateException.class, () -> MethodBody.wrap(taken, MethodBodyTest.method(taken, "run"), "placeholder", MethodBodyTest.BEFORE, MethodBodyTest.AFTER));
ClassNode arguments = MethodBodyTest.wrappable("work", "(JLjava/lang/String;)V");
Hook withArgs = new Hook(MethodBodyTest.HOOKS, "record", "(Ljava/lang/String;)V");
assertThrows(IllegalStateException.class, () -> MethodBody.wrap(arguments, MethodBodyTest.method(arguments, "run"), "original$run", withArgs, MethodBodyTest.AFTER));
}
}