Skip to content

Commit bd5772c

Browse files
committed
more efficient turbo transformer
1 parent 2c65097 commit bd5772c

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

src/main/java/com/falsepattern/lib/internal/asm/transformers/ConfigOrderTransformer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNod
6464
}
6565

6666
@Override
67-
public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
67+
public boolean transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
6868
val cn = classNode.getNode();
6969
if (cn == null)
70-
return;
70+
return false;
7171
int order = 0;
72+
boolean changed = false;
7273
outer:
7374
for (val field : cn.fields) {
7475
if ((field.access & Opcodes.ACC_PUBLIC) == 0
@@ -85,6 +86,8 @@ public void transformClass(@NotNull String className, @NotNull ClassNodeHandle c
8586
val annVisitor = field.visitAnnotation(DESC_ORDER, true);
8687
annVisitor.visit("value", order++);
8788
annVisitor.visitEnd();
89+
changed = true;
8890
}
91+
return changed;
8992
}
9093
}

src/main/java/com/falsepattern/lib/internal/asm/transformers/MixinPluginTransformer.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,43 @@ public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNod
7373
if (IMIXINPLUGIN.equals(className) ||
7474
IMIXINCONFIGPLUGIN.equals(className))
7575
return true;
76+
77+
if (!classNode.isPresent())
78+
return false;
79+
7680
if (classNode.isOriginal()) {
7781
val meta = classNode.getOriginalMetadata();
7882
if (meta != null && meta.interfacesCount == 0)
7983
return false;
8084
}
85+
8186
val cn = classNode.getNode();
8287
if (cn == null)
8388
return false;
84-
return cn.interfaces.stream()
85-
.anyMatch((i) -> IMIXINPLUGIN_INTERNAL.equals(i) ||
86-
IMIXINCONFIGPLUGIN_INTERNAL.equals(i));
89+
90+
for (String i : cn.interfaces) {
91+
if (IMIXINPLUGIN_INTERNAL.equals(i) || IMIXINCONFIGPLUGIN_INTERNAL.equals(i)) {
92+
return true;
93+
}
94+
}
95+
96+
return false;
8797
}
8898

8999
@Override
90-
public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
100+
public boolean transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
91101
val cn = classNode.getNode();
92102
if (cn == null)
93-
return;
103+
return false;
94104
if (IMIXINCONFIGPLUGIN.equals(className)) {
95-
extractMixinConfigPluginData(cn);
105+
return extractMixinConfigPluginData(cn);
96106
} else {
97-
transformPlugin(cn);
107+
return transformPlugin(cn);
98108
}
99109
}
100110

101-
private static void extractMixinConfigPluginData(ClassNode cn) {
111+
private static boolean extractMixinConfigPluginData(ClassNode cn) {
112+
boolean changed = false;
102113
for (val method : cn.methods) {
103114
switch (method.name) {
104115
case "preApply":
@@ -114,29 +125,39 @@ private static void extractMixinConfigPluginData(ClassNode cn) {
114125
for (val local : method.localVariables) {
115126
if (local.desc.contains("ClassNode;")) {
116127
local.desc = local.desc.replaceAll("L[a-zA-Z/$]+[a-zA-Z$]+/ClassNode;", CLASSNODE_REAL);
128+
changed = true;
117129
}
118130
}
119131
}
120132
}
133+
return changed;
121134
}
122135

123-
private static void transformPlugin(ClassNode cn) {
136+
private static boolean transformPlugin(ClassNode cn) {
124137
if (PREAPPLY_DESC == null) {
125138
PREAPPLY_DESC = extractMethodWithReflection("preApply");
126139
}
127140
if (POSTAPPLY_DESC == null) {
128141
POSTAPPLY_DESC = extractMethodWithReflection("postApply");
129142
}
143+
boolean changed = false;
130144
for (val method : cn.methods) {
131145
switch (method.name) {
132146
case "preApply":
133-
method.desc = PREAPPLY_DESC;
147+
if (!PREAPPLY_DESC.equals(method.desc)) {
148+
method.desc = PREAPPLY_DESC;
149+
changed = true;
150+
}
134151
break;
135152
case "postApply":
136-
method.desc = POSTAPPLY_DESC;
153+
if (!POSTAPPLY_DESC.equals(method.desc)) {
154+
method.desc = POSTAPPLY_DESC;
155+
changed = true;
156+
}
137157
break;
138158
}
139159
}
160+
return changed;
140161
}
141162

142163
private static String extractMethodWithReflection(String m) {

src/main/java/com/falsepattern/lib/internal/asm/transformers/TypeDiscovererModuleInfoSilencer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNod
4848
}
4949

5050
@Override
51-
public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
51+
public boolean transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
5252
val cn = classNode.getNode();
5353
if (cn == null)
54-
return;
54+
return false;
5555
for (val method : cn.methods) {
5656
if (!method.name.equals("<clinit>")) {
5757
continue;
@@ -63,10 +63,11 @@ public void transformClass(@NotNull String className, @NotNull ClassNodeHandle c
6363
val ldc = (LdcInsnNode) insn;
6464
if (ldc.cst.equals("[^\\s\\$]+(\\$[^\\s]+)?\\.class$")) {
6565
ldc.cst = "(?!module-info)[^\\s\\$]+(\\$[^\\s]+)?\\.class$";
66-
return;
66+
return true;
6767
}
6868
}
6969
}
7070
}
71+
return false;
7172
}
7273
}

src/main/java/com/falsepattern/lib/turboasm/TransformerUtil.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,26 @@
3232
public class TransformerUtil {
3333
private static final Logger LOG = LogManager.getLogger("ASM");
3434
public static boolean executeTransformers(String transformedName, ClassNodeHandle handle, List<TurboClassTransformer> transformers) {
35-
boolean didTransformation = false;
35+
boolean modified = false;
3636
for (val transformer: transformers) {
3737
try {
3838
if (transformer.shouldTransformClass(transformedName, handle)) {
39-
if (!didTransformation) {
40-
LOG.debug("Transforming {}!", transformedName);
39+
LOG.trace("Transforming {} with {}, owner: {}", transformedName, transformer.name(), transformer.owner());
40+
if (transformer.transformClass(transformedName, handle)) {
41+
LOG.trace("Transformed.");
42+
modified = true;
43+
} else {
44+
LOG.trace("No change.");
4145
}
42-
didTransformation = true;
43-
transformer.transformClass(transformedName, handle);
44-
LOG.debug("Successfully transformed class {} with {}, owner: {}", transformedName, transformer.name(), transformer.owner());
4546
}
4647
} catch (Exception e) {
4748
LOG.error("Failed to transform class {} with {}, owner: {}", transformedName, transformer.name(), transformer.owner());
4849
LOG.error("Exception stacktrace:", e);
4950
}
5051
}
51-
return didTransformation;
52+
if (modified) {
53+
LOG.trace("Transformed class {}", transformedName);
54+
}
55+
return modified;
5256
}
5357
}

src/main/java/com/falsepattern/lib/turboasm/TurboClassTransformer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public interface TurboClassTransformer {
5858
* (Optionally) transform a given class. No ClassReader flags are used for maximum efficiency, so stack frames are not expanded.
5959
* @param className The name of the transformed class (in the dot-separated format).
6060
* @param classNode The handle to the lazily ASM-parsed class to modify, and metadata used for class writing.
61+
* @return True if the class has been modified in any way by this transformer. If all transformers return false,
62+
* then the ClassNode instance will not be re-serialized.
6163
*/
6264
@Expose
63-
void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode);
65+
boolean transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode);
6466
}

0 commit comments

Comments
 (0)