diff options
author | Linnea Gräf <nea@nea.moe> | 2024-01-04 17:45:29 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-01-04 17:55:39 +0100 |
commit | 61a5635e993b46d3ae2e76edd1b104987ff92a3f (patch) | |
tree | 3091150988d9ab47ff37aac482263aa0ce0e797f /src/main/kotlin/moe/nea/sky/features | |
parent | 62519450e2dc3434560ab2e9db1f308c0ca79f0c (diff) | |
download | neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.tar.gz neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.tar.bz2 neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.zip |
Add Optifine cache
Diffstat (limited to 'src/main/kotlin/moe/nea/sky/features')
-rw-r--r-- | src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt | 101 |
1 files changed, 101 insertions, 0 deletions
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<T>(val maxSize: Int) : Iterable<T> { + private val dequeue = ArrayDeque<T>() + fun append(element: T) { + dequeue.addLast(element) + if (dequeue.size > maxSize) { + dequeue.removeFirst() + } + } + + val size get() = dequeue.size + + override fun iterator(): Iterator<T> { + return dequeue.iterator() + } +} + +data class CacheStats(var cacheHits: Int = 0, var cacheMisses: Int = 0, var cacheCount: Int = 0) +object OptifineCustomItemCache { + private val map: MutableMap<OptifineCustomItemCacheKey, CacheResult> = mutableMapOf() + private val histogram = Histogram<CacheStats>(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 |