Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use MethodHandle to prevent autoboxing allocations #47

Merged
merged 3 commits into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/main/java/logisticspipes/proxy/buildcraft/BCRenderTESR.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package logisticspipes.proxy.buildcraft;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;

import buildcraft.transport.TileGenericPipe;
Expand All @@ -10,32 +12,34 @@

public class BCRenderTESR implements IBCRenderTESR {

private final Method renderGatesWires;
private final Method renderPluggables;

@SneakyThrows(Exception.class)
BCRenderTESR() {
renderGatesWires = PipeRendererTESR.class.getDeclaredMethod(
"renderGatesWires",
new Class[] { TileGenericPipe.class, double.class, double.class, double.class });
renderGatesWires.setAccessible(true);
renderPluggables = PipeRendererTESR.class.getDeclaredMethod(
"renderPluggables",
new Class[] { TileGenericPipe.class, double.class, double.class, double.class });
renderPluggables.setAccessible(true);
private static final MethodHandle renderGatesWires;
private static final MethodHandle renderPluggables;

static {
MethodHandles.Lookup lookup = MethodHandles.lookup();
renderGatesWires = getHandle(lookup, "renderGatesWires");
renderPluggables = getHandle(lookup, "renderPluggables");
}

@SneakyThrows
private static MethodHandle getHandle(MethodHandles.Lookup lookup, String methodName) {
Method method = PipeRendererTESR.class
.getDeclaredMethod(methodName, TileGenericPipe.class, double.class, double.class, double.class);
method.setAccessible(true);
return lookup.unreflect(method);
}

@Override
@SneakyThrows(Exception.class)
@SneakyThrows(Throwable.class)
public void renderWires(LogisticsTileGenericPipe pipe, double x, double y, double z) {
TileGenericPipe tgPipe = (TileGenericPipe) pipe.tilePart.getOriginal();
renderGatesWires.invoke(PipeRendererTESR.INSTANCE, tgPipe, x, y, z);
renderGatesWires.invokeExact(PipeRendererTESR.INSTANCE, tgPipe, x, y, z);
}

@Override
@SneakyThrows(Exception.class)
@SneakyThrows(Throwable.class)
public void dynamicRenderPluggables(LogisticsTileGenericPipe pipe, double x, double y, double z) {
TileGenericPipe tgPipe = (TileGenericPipe) pipe.tilePart.getOriginal();
renderPluggables.invoke(PipeRendererTESR.INSTANCE, tgPipe, x, y, z);
renderPluggables.invokeExact(PipeRendererTESR.INSTANCE, tgPipe, x, y, z);
}
}
Loading