Gjallarhorn: Implement block state global cache.

This reduces memory usage by reusing block state objects.
This commit is contained in:
Kenneth Endfinger
2022-01-08 15:04:16 -05:00
parent 81a76da809
commit 8caf3de634
2 changed files with 9 additions and 6 deletions

View File

@ -29,7 +29,7 @@ class BlockChangelog(
val location = BlockCoordinate(x.toLong(), y.toLong(), z.toLong()) val location = BlockCoordinate(x.toLong(), y.toLong(), z.toLong())
val fromBlock = if (changeIsBreak) { val fromBlock = if (changeIsBreak) {
BlockState(block) BlockState.cached(block)
} else { } else {
BlockState.AirBlock BlockState.AirBlock
} }
@ -37,7 +37,7 @@ class BlockChangelog(
val toBlock = if (changeIsBreak) { val toBlock = if (changeIsBreak) {
BlockState.AirBlock BlockState.AirBlock
} else { } else {
BlockState(block) BlockState.cached(block)
} }
BlockChange( BlockChange(

View File

@ -1,10 +1,13 @@
package cloud.kubelet.foundation.gjallarhorn.state package cloud.kubelet.foundation.gjallarhorn.state
import kotlinx.serialization.Serializable import java.util.concurrent.ConcurrentHashMap
@Serializable class BlockState(val type: String) {
data class BlockState(val type: String) {
companion object { companion object {
val AirBlock = BlockState("minecraft:air") private val cache = ConcurrentHashMap<String, BlockState>()
val AirBlock: BlockState = cached("minecraft:air")
fun cached(type: String): BlockState = cache.computeIfAbsent(type) { BlockState(type) }
} }
} }