Skip to content

Commit

Permalink
refactor: Use MethodHandle to prevent autoboxing allocations (RS485#47)
Browse files Browse the repository at this point in the history
* refactor: Use MethodHandle to prevent autoboxing allocations

With MethodHandle.invokeExact we can skip the autoboxing allocations
from the BCRenderTESR when looking at logistic pipes. These allocations
accounted for 5MB+ over a minute in a test world.

Co-authored-by: Raven Szewczyk <git@eigenraven.me>

* Make MethodHandles static

* cleanup: Replace try/catch with @SneakyThrows and use varargs instead of array

---------

Co-authored-by: Raven Szewczyk <git@eigenraven.me>
(cherry picked from commit e3774f3)
  • Loading branch information
szuend authored and Dream-Master committed May 20, 2024
1 parent 88313fe commit e40388e
Showing 1 changed file with 21 additions and 17 deletions.
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);
}
}

0 comments on commit e40388e

Please sign in to comment.