Skip to content

Commit 5ef6389

Browse files
committed
Cache iota renderers to improve performance
1 parent 77d6498 commit 5ef6389

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
2727
- Added (official) support for inserting iota-holding items other than foci into the Focal Frame.
2828
- A single Focal Frame item can now be used like a bundle to insert/remove items in your inventory.
2929
- The Focal Frame now gives comparator output and can be broken by pushing it with a piston.
30+
- Splicing Table iota renderers are now cached to improve performance.
3031

3132
### Fixed
3233

Common/src/main/java/gay/object/hexdebug/api/client/splicing/SplicingTableIotaRenderer.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
import net.minecraft.client.gui.GuiGraphics;
99
import net.minecraft.client.gui.components.Tooltip;
1010
import net.minecraft.network.chat.Component;
11+
import org.jetbrains.annotations.ApiStatus;
1112
import org.jetbrains.annotations.NotNull;
1213

1314
public abstract class SplicingTableIotaRenderer {
1415
@NotNull
1516
private final IotaType<?> type;
1617
@NotNull
1718
private final SplicingTableIotaClientView iota;
18-
private final int x;
19-
private final int y;
19+
private int x;
20+
private int y;
2021

2122
public SplicingTableIotaRenderer(
2223
@NotNull IotaType<?> type,
@@ -44,10 +45,20 @@ public final int getX() {
4445
return x;
4546
}
4647

48+
@ApiStatus.OverrideOnly
49+
public void setX(int x) {
50+
this.x = x;
51+
}
52+
4753
public final int getY() {
4854
return y;
4955
}
5056

57+
@ApiStatus.OverrideOnly
58+
public void setY(int y) {
59+
this.y = y;
60+
}
61+
5162
/** Renders one frame of this iota. */
5263
public abstract void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick);
5364

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/List.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class ListRenderer(
2828
return super.buildTooltip()
2929
.addAdvancedLine(SplicingTableScreen.tooltipText("length", listTag.size))
3030
}
31+
32+
override fun setX(x: Int) {
33+
super.setX(x)
34+
inner?.x = x
35+
}
36+
37+
override fun setY(y: Int) {
38+
super.setY(y)
39+
inner?.y = y
40+
}
3141
}
3242

3343
class ListRendererProvider(private val inner: SplicingTableIotaRendererProvider) : SplicingTableIotaRendererProvider {

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/widgets/BaseIotaButton.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package gay.`object`.hexdebug.gui.splicing.widgets
22

33
import at.petrak.hexcasting.api.casting.iota.IotaType
44
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
5+
import com.google.common.cache.CacheBuilder
56
import com.mojang.blaze3d.systems.RenderSystem
67
import gay.`object`.hexdebug.HexDebug
78
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
89
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderers
910
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
1011
import net.minecraft.client.gui.GuiGraphics
12+
import net.minecraft.client.gui.components.Tooltip
1113
import net.minecraft.network.chat.Component
1214

1315
abstract class BaseIotaButton(x: Int, y: Int) : HexagonButton(
@@ -59,14 +61,25 @@ abstract class BaseIotaButton(x: Int, y: Int) : HexagonButton(
5961

6062
active = true
6163

62-
try {
63-
renderer = SplicingTableIotaRenderers.getProvider(iotaType)
64-
?.createRenderer(iotaType, iotaView, x, y)
65-
66-
tooltip = renderer?.createTooltip()
67-
} catch (e: Exception) {
68-
HexDebug.LOGGER.error("Caught exception while preparing renderer for ${iotaType.typeName().string}", e)
69-
renderer = null
64+
val (renderer, tooltip) = rendererCache.get(iotaView) {
65+
try {
66+
val renderer = SplicingTableIotaRenderers.getProvider(iotaType)
67+
?.createRenderer(iotaType, iotaView, x, y)
68+
renderer to renderer?.createTooltip()
69+
} catch (e: Exception) {
70+
HexDebug.LOGGER.error("Caught exception while preparing renderer for ${iotaType.typeName().string}", e)
71+
null to null
72+
}
7073
}
74+
renderer?.x = x
75+
renderer?.y = y
76+
this.renderer = renderer
77+
this.tooltip = tooltip
78+
}
79+
80+
companion object {
81+
private val rendererCache = CacheBuilder.newBuilder()
82+
.maximumSize(HexIotaTypes.MAX_SERIALIZATION_TOTAL * 2L)
83+
.build<SplicingTableIotaClientView, Pair<SplicingTableIotaRenderer?, Tooltip?>>()
7184
}
7285
}

0 commit comments

Comments
 (0)