Use linked hash set for cable internals, which improves iteration performance
This commit is contained in:
parent
655a9c541e
commit
6fb9f4474e
@ -1,6 +1,9 @@
|
|||||||
package ru.dbotthepony.mc.otm.block.entity.cable
|
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.Reference2ObjectOpenHashMap
|
||||||
|
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet
|
||||||
import net.minecraft.core.BlockPos
|
import net.minecraft.core.BlockPos
|
||||||
import org.apache.logging.log4j.LogManager
|
import org.apache.logging.log4j.LogManager
|
||||||
import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
|
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) {
|
class SegmentPath(private val a: BlockPos, private val b: BlockPos) {
|
||||||
val segments = HashSet<Segment>()
|
val segments = ObjectLinkedOpenHashSet<Segment>()
|
||||||
private var shortCircuit = false
|
private var shortCircuit = false
|
||||||
private var lastTickTransfers = 0
|
private var lastTickTransfers = 0
|
||||||
private var lastTick = 0
|
private var lastTick = 0
|
||||||
@ -79,11 +82,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
return this === other || other is SegmentPath && segments == other.segments
|
return this === other || other is SegmentPath && segments == other.segments
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
fun transfer(amount: Decimal, simulate: Boolean, instantSnapshot: Reference2ObjectOpenHashMap<Segment, Decimal>): Decimal {
|
||||||
return segments.hashCode()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun transfer(amount: Decimal, simulate: Boolean, instantSnapshot: MutableMap<Segment, Decimal>): Decimal {
|
|
||||||
if (!amount.isPositive || shortCircuit) {
|
if (!amount.isPositive || shortCircuit) {
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
}
|
}
|
||||||
@ -125,8 +124,8 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
nodes.add(node)
|
nodes.add(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val nodes = HashSet<EnergyCableBlockEntity.Node>()
|
private val nodes = ObjectLinkedOpenHashSet<EnergyCableBlockEntity.Node>()
|
||||||
private val paths = HashSet<SegmentPath>()
|
private val paths = ReferenceOpenHashSet<SegmentPath>()
|
||||||
|
|
||||||
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
|
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
|
||||||
return node in nodes
|
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) {
|
if (lastTick != UNIVERSE_TICKS) {
|
||||||
transferredLastTick = Decimal.ZERO
|
transferredLastTick = Decimal.ZERO
|
||||||
lastTick = UNIVERSE_TICKS
|
lastTick = UNIVERSE_TICKS
|
||||||
@ -194,7 +193,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
|
|||||||
return throughput - currentTransferred
|
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) {
|
if (lastTick != UNIVERSE_TICKS) {
|
||||||
transferredLastTick = Decimal.ZERO
|
transferredLastTick = Decimal.ZERO
|
||||||
lastTick = UNIVERSE_TICKS
|
lastTick = UNIVERSE_TICKS
|
||||||
|
Loading…
Reference in New Issue
Block a user