Skip to content

Commit

Permalink
Avoid using TR in RemapInjectables (#19)
Browse files Browse the repository at this point in the history
* Avoid using TR in RemapInjectables

Since this transform only remaps a single class, we can use ASM's
more lightweight ClassRemapper to avoid TR's costly remapping.

This transform is present in essentially all Architectury dev
env runtimes (if runtime transformation is enabled), so this
optimisation has a major performance boost if there are no
other active TinyRemapperTransformers. This is true for the
default Fabric setup, but Forge setups additionally use
TransformForgeEnvironment.

For a simple Fabric setup, this led to class transformation
being about two orders of magnitude faster on my machine in
the Architectury API Fabric dev env (transformation time
from x·10ms to x·100µs).

* Add back isInjectInjectables check
  • Loading branch information
Juuxel authored Jun 5, 2023
1 parent 9c50798 commit 64f4523
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@

import com.google.common.base.MoreObjects;
import com.google.gson.JsonObject;
import dev.architectury.tinyremapper.IMappingProvider;
import dev.architectury.transformer.transformers.base.TinyRemapperTransformer;
import dev.architectury.transformer.transformers.base.ClassEditTransformer;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.tree.ClassNode;

import java.io.File;
import java.util.Collections;
import java.util.List;

/**
* Remap architectury injectables calls to the injected classes.
*/
public class RemapInjectables implements TinyRemapperTransformer {
public class RemapInjectables implements ClassEditTransformer {
public static final String EXPECT_PLATFORM_LEGACY = "Lme/shedaniel/architectury/ExpectPlatform;";
public static final String EXPECT_PLATFORM_LEGACY2 = "Lme/shedaniel/architectury/annotations/ExpectPlatform;";
public static final String EXPECT_PLATFORM = "Ldev/architectury/injectables/annotations/ExpectPlatform;";
public static final String EXPECT_PLATFORM_TRANSFORMED = "Ldev/architectury/injectables/annotations/ExpectPlatform$Transformed;";
public static final String PLATFORM_ONLY_LEGACY = "Lme/shedaniel/architectury/annotations/PlatformOnly;";
public static final String PLATFORM_ONLY = "Ldev/architectury/injectables/annotations/PlatformOnly;";
private static final String ARCHITECTURY_TARGET = "dev/architectury/injectables/targets/ArchitecturyTarget";
private String uniqueIdentifier = null;

@Override
Expand All @@ -51,16 +53,23 @@ public void supplyProperties(JsonObject json) {
}

@Override
public List<IMappingProvider> collectMappings() throws Exception {
if (isInjectInjectables()) {
return Collections.singletonList(sink -> {
sink.acceptClass(
"dev/architectury/injectables/targets/ArchitecturyTarget",
MoreObjects.firstNonNull(uniqueIdentifier, getUniqueIdentifier()) + "/PlatformMethods"
);
});
}
return Collections.emptyList();
public ClassNode doEdit(String name, ClassNode node) {
if (!isInjectInjectables()) return node; // no need to edit the class
String newName = MoreObjects.firstNonNull(uniqueIdentifier, getUniqueIdentifier()) + "/PlatformMethods";
ClassNode newNode = new ClassNode();
Remapper remapper = new Remapper() {
@Override
public String map(String internalName) {
if (ARCHITECTURY_TARGET.equals(internalName)) {
return newName;
}

return internalName;
}
};
ClassVisitor cv = new ClassRemapper(newNode, remapper);
node.accept(cv);
return newNode;
}

public static String getUniqueIdentifier() {
Expand Down

0 comments on commit 64f4523

Please sign in to comment.