From bb9d5d9ae04466c43185ef5ee3922698853be28d Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 11 Feb 2025 00:09:18 +0700 Subject: [PATCH] Throttle cable path searches --- .../mc/otm/block/entity/cable/EnergyCableGraph.kt | 15 +++++++++++++-- .../ru/dbotthepony/mc/otm/config/CablesConfig.kt | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt index a5fde294b..5f2de56ea 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt @@ -8,6 +8,7 @@ import org.apache.logging.log4j.LogManager 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.math.Decimal import ru.dbotthepony.mc.otm.core.math.RelativeSide import ru.dbotthepony.mc.otm.core.shuffle @@ -446,16 +447,26 @@ class EnergyCableGraph : GraphNodeList { if (!a.canTraverse || !b.canTraverse) return listOf() + if (searchedOnTick != UNIVERSE_TICKS) { + searchedOnTick = UNIVERSE_TICKS + lastTickSearches = 0 + } + val list = pathCache.computeIfAbsent(a to b) { CacheEntry() } - if (!list.saturated) { + val maxSearches = CablesConfig.SEARCHES_PER_TICK + + if (!list.saturated && lastTickSearches <= maxSearches) { var maxThroughput = list.paths.maxOfOrNull { it.availableThroughput } ?: Decimal.ZERO - while (maxThroughput < energyToTransfer && !list.saturated) { + while (maxThroughput < energyToTransfer && !list.saturated && ++lastTickSearches <= maxSearches) { val find = findPath(a, b, list.paths, maxThroughput) if (find == null || find in list.paths) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt index 7d801f062..40559732d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt @@ -27,4 +27,10 @@ object CablesConfig : AbstractConfig("cables") { init { E.SUPERCONDUCTOR } + + val SEARCHES_PER_TICK: Int by builder + .comment("How many paths are allowed to be searched for during single tick *per* cable network") + .comment("Too low value will make huge cable networks woozy when their cabling changes (e.g. cable added or removed, or valve switched)") + .comment("Too high value will create lag spikes depending on complexity of cable network at hand when cabling changes") + .defineInRange("SEARCHER_PER_TICK", 40, 1) }