Improve extra path search performance

This commit is contained in:
DBotThePony 2025-02-11 00:17:04 +07:00
parent bb9d5d9ae0
commit 614ba26da6
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -9,6 +9,7 @@ import ru.dbotthepony.mc.otm.SERVER_IS_LIVE
import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
import ru.dbotthepony.mc.otm.capability.receiveEnergy
import ru.dbotthepony.mc.otm.config.CablesConfig
import ru.dbotthepony.mc.otm.core.addAll
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.math.RelativeSide
import ru.dbotthepony.mc.otm.core.shuffle
@ -69,6 +70,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
class SegmentPath(private val a: BlockPos, private val b: BlockPos) {
val segments = LinkedHashSet<Segment>()
val nodes = HashSet<EnergyCableBlockEntity.Node>()
var shortCircuit = false
private var lastTickTransfers = 0
private var lastTick = 0
@ -81,7 +83,7 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
}
operator fun contains(node: EnergyCableBlockEntity.Node): Boolean {
return segments.any { node in it }
return node in nodes
}
override fun equals(other: Any?): Boolean {
@ -284,11 +286,13 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
fun remove(node: EnergyCableBlockEntity.Node) {
check(nodes.remove(node)) { "Tried to remove node $node from segment $this, but that node does not belong to this segment" }
paths.forEach { it.nodes.remove(node) }
throughputKnown = false
}
fun add(node: EnergyCableBlockEntity.Node) {
check(nodes.add(node)) { "Tried to add node $node to segment $this, but we already have that node added" }
paths.forEach { it.nodes.add(node) }
if (throughputKnown)
throughput = maxOf(throughput, node.energyThroughput)
@ -297,12 +301,14 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
fun add(path: SegmentPath) {
if (paths.add(path)) {
check(path.segments.add(this)) { "Path set and Segment disagree whenever $this is absent from $path" }
path.nodes.addAll(nodes)
}
}
fun remove(path: SegmentPath) {
check(paths.remove(path)) { "Path $path should already contain $this" }
check(path.segments.remove(this)) { "Path set and Segment disagree whenever $this is present in $path" }
path.nodes.removeAll(nodes)
}
// breaks "instant snapshots" of segments atm