From 61a5635e993b46d3ae2e76edd1b104987ff92a3f Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 4 Jan 2024 17:45:29 +0100 Subject: Add Optifine cache --- .../features/fopt/OptifineCustomItemCacheKey.kt | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt (limited to 'src/main/kotlin/moe/nea/sky/features') diff --git a/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt b/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt new file mode 100644 index 0000000..ce922a3 --- /dev/null +++ b/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt @@ -0,0 +1,101 @@ +package moe.nea.sky.features.fopt + +import io.github.moulberry.notenoughupdates.deps.com.mojang.brigadier.arguments.IntegerArgumentType +import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent +import io.github.moulberry.notenoughupdates.util.brigadier.get +import io.github.moulberry.notenoughupdates.util.brigadier.thenArgumentExecute +import io.github.moulberry.notenoughupdates.util.brigadier.thenExecute +import io.github.moulberry.notenoughupdates.util.brigadier.withHelp +import moe.nea.sky.util.showMessage +import net.minecraft.command.ICommandSender +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.minecraftforge.fml.common.gameevent.TickEvent +import net.optifine.CustomItemProperties + +class OptifineCustomItemCacheKey(val itemstack: ItemStack, val type: Int) { + override fun equals(other: Any?): Boolean { + if (other !is OptifineCustomItemCacheKey) return false + return itemstack === other.itemstack && type == other.type + } + + override fun hashCode(): Int { + return System.identityHashCode(itemstack) + type * 31 + } +} + +class CacheResult(val customItemProperties: CustomItemProperties?) + +class Histogram(val maxSize: Int) : Iterable { + private val dequeue = ArrayDeque() + fun append(element: T) { + dequeue.addLast(element) + if (dequeue.size > maxSize) { + dequeue.removeFirst() + } + } + + val size get() = dequeue.size + + override fun iterator(): Iterator { + return dequeue.iterator() + } +} + +data class CacheStats(var cacheHits: Int = 0, var cacheMisses: Int = 0, var cacheCount: Int = 0) +object OptifineCustomItemCache { + private val map: MutableMap = mutableMapOf() + private val histogram = Histogram(100) + private var cacheStats = CacheStats() + + fun dumpStats(target: ICommandSender, length: Int) { + target.showMessage { + text("Optifine Cache Size:") + histogram.forEachIndexed { index, stats -> + val ago = histogram.size - index + if (ago <= length) + text("§b${ago}§e tick${s(ago)} ago: §a${stats.cacheHits}§e-§c${stats.cacheMisses}§e-§b${stats.cacheCount}") + } + } + } + + @SubscribeEvent + fun onCommands(event: RegisterBrigadierCommandEvent) { + event.command("nhopticache") { + thenExecute { + dumpStats(source, Int.MAX_VALUE) + } + thenArgumentExecute("count", IntegerArgumentType.integer()) { + dumpStats(source, this[it]) + }.withHelp("Show stats about the last count ticks") + }.withHelp("Show stats about the optifine item cache") + } + + + @SubscribeEvent + fun onTick(event: TickEvent.ClientTickEvent) { + if (event.phase == TickEvent.Phase.END) { + cacheStats.cacheCount = map.size + histogram.append(cacheStats) + cacheStats = CacheStats() + map.clear() + } + } + + @JvmStatic + fun retrieveCacheHit(itemstack: ItemStack, type: Int): CacheResult? { + val cacheResult = map[OptifineCustomItemCacheKey(itemstack, type)] + if (cacheResult == null) { + cacheStats.cacheMisses++ + } else { + cacheStats.cacheHits++ + } + return cacheResult + } + + @JvmStatic + fun storeCacheElement(itemstack: ItemStack, type: Int, properties: CacheResult) { + map[OptifineCustomItemCacheKey(itemstack, type)] = properties + } + +} \ No newline at end of file -- cgit