Use linked hash set for cable internals, which improves iteration performance

This commit is contained in:
DBotThePony 2025-01-15 13:08:49 +07:00
parent 655a9c541e
commit 6fb9f4474e
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -1,6 +1,9 @@
package ru.dbotthepony.mc.otm.block.entity.cable
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet
import net.minecraft.core.BlockPos
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
@ -63,7 +66,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
}
class SegmentPath(private val a: BlockPos, private val b: BlockPos) {
val segments = HashSet<Segment>()
val segments = ObjectLinkedOpenHashSet<Segment>()
private var shortCircuit = false
private var lastTickTransfers = 0
private var lastTick = 0
@ -79,11 +82,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
return this === other || other is SegmentPath && segments == other.segments
}
override fun hashCode(): Int {
return segments.hashCode()
}
fun transfer(amount: Decimal, simulate: Boolean, instantSnapshot: MutableMap<Segment, Decimal>): Decimal {
fun transfer(amount: Decimal, simulate: Boolean, instantSnapshot: Reference2ObjectOpenHashMap<Segment, Decimal>): Decimal {
if (!amount.isPositive || shortCircuit) {
return Decimal.ZERO
}
@ -125,8 +124,8 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
nodes.add(node)
}
private val nodes = HashSet<EnergyCableBlockEntity.Node>()
private val paths = HashSet<SegmentPath>()
private val nodes = ObjectLinkedOpenHashSet<EnergyCableBlockEntity.Node>()
private val paths = ReferenceOpenHashSet<SegmentPath>()
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
return node in nodes
@ -183,7 +182,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
}
}
fun throughput(instantSnapshot: Map<Segment, Decimal>): Decimal {
fun throughput(instantSnapshot: Reference2ObjectOpenHashMap<Segment, Decimal>): Decimal {
if (lastTick != UNIVERSE_TICKS) {
transferredLastTick = Decimal.ZERO
lastTick = UNIVERSE_TICKS
@ -194,7 +193,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
return throughput - currentTransferred
}
fun transfer(amount: Decimal, instantSnapshot: MutableMap<Segment, Decimal>): Decimal {
fun transfer(amount: Decimal, instantSnapshot: Reference2ObjectOpenHashMap<Segment, Decimal>): Decimal {
if (lastTick != UNIVERSE_TICKS) {
transferredLastTick = Decimal.ZERO
lastTick = UNIVERSE_TICKS