|
| 1 | +package com.codename1.tools.translator; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.objectweb.asm.ClassWriter; |
| 5 | +import org.objectweb.asm.MethodVisitor; |
| 6 | +import org.objectweb.asm.Opcodes; |
| 7 | + |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.time.Duration; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +class CullPerformanceTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void cullsLargeUnusedGraphQuickly() throws Exception { |
| 22 | + Parser.cleanup(); |
| 23 | + |
| 24 | + Path classesDir = Files.createTempDirectory("cull-stress-classes"); |
| 25 | + int totalNodes = 300; |
| 26 | + List<Path> generated = new ArrayList<Path>(); |
| 27 | + generated.add(writeStub(classesDir, "java/lang/Object")); |
| 28 | + |
| 29 | + for (int i = 0; i < totalNodes; i++) { |
| 30 | + generated.add(writeNode(classesDir, i, totalNodes)); |
| 31 | + } |
| 32 | + generated.add(writeEntryPoint(classesDir, totalNodes)); |
| 33 | + |
| 34 | + for (Path classFile : generated) { |
| 35 | + Parser.parse(classFile.toFile()); |
| 36 | + } |
| 37 | + |
| 38 | + List<ByteCodeClass> parsedClasses = getParsedClasses(); |
| 39 | + for (ByteCodeClass bc : parsedClasses) { |
| 40 | + bc.updateAllDependencies(); |
| 41 | + } |
| 42 | + ByteCodeClass.markDependencies(parsedClasses, null); |
| 43 | + List<ByteCodeClass> reachable = ByteCodeClass.clearUnmarked(parsedClasses); |
| 44 | + setParsedClasses(reachable); |
| 45 | + |
| 46 | + Method eliminateUnusedMethods = Parser.class.getDeclaredMethod("eliminateUnusedMethods"); |
| 47 | + eliminateUnusedMethods.setAccessible(true); |
| 48 | + |
| 49 | + assertTimeoutPreemptively(Duration.ofSeconds(10), () -> eliminateUnusedMethods.invoke(null)); |
| 50 | + |
| 51 | + long remaining = reachable.stream().filter(bc -> !bc.isEliminated()).count(); |
| 52 | + assertTrue(remaining < totalNodes / 4, "Most generated classes should be culled to keep optimization fast"); |
| 53 | + } |
| 54 | + |
| 55 | + private Path writeStub(Path classesDir, String internalName) throws Exception { |
| 56 | + ClassWriter cw = new ClassWriter(0); |
| 57 | + String superName = "java/lang/Object".equals(internalName) ? null : "java/lang/Object"; |
| 58 | + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, internalName, null, superName, null); |
| 59 | + addDefaultConstructor(cw, superName); |
| 60 | + cw.visitEnd(); |
| 61 | + return writeClass(classesDir, internalName, cw.toByteArray()); |
| 62 | + } |
| 63 | + |
| 64 | + private Path writeNode(Path classesDir, int index, int totalNodes) throws Exception { |
| 65 | + String name = "com/example/stress/Node" + index; |
| 66 | + ClassWriter cw = new ClassWriter(0); |
| 67 | + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", null); |
| 68 | + |
| 69 | + addDefaultConstructor(cw, "java/lang/Object"); |
| 70 | + |
| 71 | + MethodVisitor value = cw.visitMethod(Opcodes.ACC_PUBLIC, "value", "(I)I", null, null); |
| 72 | + value.visitCode(); |
| 73 | + value.visitVarInsn(Opcodes.ILOAD, 1); |
| 74 | + value.visitIntInsn(Opcodes.SIPUSH, index); |
| 75 | + value.visitInsn(Opcodes.IADD); |
| 76 | + |
| 77 | + if (index + 1 < totalNodes && index % 3 == 0) { |
| 78 | + String next = "com/example/stress/Node" + (index + 1); |
| 79 | + value.visitTypeInsn(Opcodes.NEW, next); |
| 80 | + value.visitInsn(Opcodes.DUP); |
| 81 | + value.visitMethodInsn(Opcodes.INVOKESPECIAL, next, "<init>", "()V", false); |
| 82 | + value.visitVarInsn(Opcodes.ILOAD, 1); |
| 83 | + value.visitMethodInsn(Opcodes.INVOKEVIRTUAL, next, "value", "(I)I", false); |
| 84 | + value.visitInsn(Opcodes.IADD); |
| 85 | + } |
| 86 | + |
| 87 | + value.visitInsn(Opcodes.IRETURN); |
| 88 | + value.visitMaxs(3, 2); |
| 89 | + value.visitEnd(); |
| 90 | + |
| 91 | + cw.visitEnd(); |
| 92 | + return writeClass(classesDir, name, cw.toByteArray()); |
| 93 | + } |
| 94 | + |
| 95 | + private Path writeEntryPoint(Path classesDir, int totalNodes) throws Exception { |
| 96 | + String name = "com/example/stress/StressEntry"; |
| 97 | + ClassWriter cw = new ClassWriter(0); |
| 98 | + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", null); |
| 99 | + |
| 100 | + addDefaultConstructor(cw, "java/lang/Object"); |
| 101 | + |
| 102 | + MethodVisitor main = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); |
| 103 | + main.visitCode(); |
| 104 | + main.visitTypeInsn(Opcodes.NEW, "com/example/stress/Node0"); |
| 105 | + main.visitInsn(Opcodes.DUP); |
| 106 | + main.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/example/stress/Node0", "<init>", "()V", false); |
| 107 | + main.visitInsn(Opcodes.ICONST_0); |
| 108 | + main.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/example/stress/Node0", "value", "(I)I", false); |
| 109 | + main.visitInsn(Opcodes.POP); |
| 110 | + main.visitInsn(Opcodes.RETURN); |
| 111 | + main.visitMaxs(3, 1); |
| 112 | + main.visitEnd(); |
| 113 | + |
| 114 | + cw.visitEnd(); |
| 115 | + return writeClass(classesDir, name, cw.toByteArray()); |
| 116 | + } |
| 117 | + |
| 118 | + private void addDefaultConstructor(ClassWriter cw, String superName) { |
| 119 | + MethodVisitor ctor = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); |
| 120 | + ctor.visitCode(); |
| 121 | + if (superName != null) { |
| 122 | + ctor.visitVarInsn(Opcodes.ALOAD, 0); |
| 123 | + ctor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "()V", false); |
| 124 | + } |
| 125 | + ctor.visitInsn(Opcodes.RETURN); |
| 126 | + ctor.visitMaxs(superName != null ? 1 : 0, 1); |
| 127 | + ctor.visitEnd(); |
| 128 | + } |
| 129 | + |
| 130 | + private Path writeClass(Path classesDir, String internalName, byte[] data) throws Exception { |
| 131 | + Path classFile = classesDir.resolve(internalName + ".class"); |
| 132 | + Files.createDirectories(classFile.getParent()); |
| 133 | + Files.write(classFile, data); |
| 134 | + return classFile; |
| 135 | + } |
| 136 | + |
| 137 | + @SuppressWarnings("unchecked") |
| 138 | + private List<ByteCodeClass> getParsedClasses() throws Exception { |
| 139 | + Field f = Parser.class.getDeclaredField("classes"); |
| 140 | + f.setAccessible(true); |
| 141 | + return (List<ByteCodeClass>) f.get(null); |
| 142 | + } |
| 143 | + |
| 144 | + private void setParsedClasses(List<ByteCodeClass> updated) throws Exception { |
| 145 | + Field f = Parser.class.getDeclaredField("classes"); |
| 146 | + f.setAccessible(true); |
| 147 | + f.set(null, updated); |
| 148 | + } |
| 149 | +} |
0 commit comments