mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 13:31:32 +00:00
Gjallarhorn: Implement block state global cache.
This reduces memory usage by reusing block state objects.
This commit is contained in:
@ -29,7 +29,7 @@ class BlockChangelog(
|
||||
val location = BlockCoordinate(x.toLong(), y.toLong(), z.toLong())
|
||||
|
||||
val fromBlock = if (changeIsBreak) {
|
||||
BlockState(block)
|
||||
BlockState.cached(block)
|
||||
} else {
|
||||
BlockState.AirBlock
|
||||
}
|
||||
@ -37,7 +37,7 @@ class BlockChangelog(
|
||||
val toBlock = if (changeIsBreak) {
|
||||
BlockState.AirBlock
|
||||
} else {
|
||||
BlockState(block)
|
||||
BlockState.cached(block)
|
||||
}
|
||||
|
||||
BlockChange(
|
||||
|
@ -1,10 +1,13 @@
|
||||
package cloud.kubelet.foundation.gjallarhorn.state
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
@Serializable
|
||||
data class BlockState(val type: String) {
|
||||
class BlockState(val type: String) {
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user