From 2647b24e0917958d9865dc26d00e9aeaa89520cd Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 8 Sep 2024 16:19:23 +0300 Subject: [PATCH] perf: Save resource uniques when resources change, so we only need one lookup for all resources for uniques *significant* impact, estimated at 6% of runtime (!) --- .../com/unciv/logic/civilization/Civilization.kt | 15 +++++---------- .../transients/CivInfoTransientCache.kt | 15 ++++++++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/src/com/unciv/logic/civilization/Civilization.kt b/core/src/com/unciv/logic/civilization/Civilization.kt index 615587c27d..9f553ed474 100644 --- a/core/src/com/unciv/logic/civilization/Civilization.kt +++ b/core/src/com/unciv/logic/civilization/Civilization.kt @@ -47,11 +47,7 @@ import com.unciv.models.ruleset.tile.ResourceSupplyList import com.unciv.models.ruleset.tile.ResourceType import com.unciv.models.ruleset.tile.TileImprovement import com.unciv.models.ruleset.tile.TileResource -import com.unciv.models.ruleset.unique.StateForConditionals -import com.unciv.models.ruleset.unique.TemporaryUnique -import com.unciv.models.ruleset.unique.Unique -import com.unciv.models.ruleset.unique.UniqueType -import com.unciv.models.ruleset.unique.getMatchingUniques +import com.unciv.models.ruleset.unique.* import com.unciv.models.ruleset.unit.BaseUnit import com.unciv.models.stats.Stat import com.unciv.models.stats.Stats @@ -119,6 +115,9 @@ class Civilization : IsPartOfGameInfoSerialization { @Transient var summarizedCivResourceSupply = ResourceSupplyList() + + @Transient + var civResourcesUniqueMap = UniqueMap() @Transient val cityStateFunctions = CityStateFunctions(this) @@ -535,11 +534,7 @@ class Civilization : IsPartOfGameInfoSerialization { if (religionManager.religion != null) yieldAll(religionManager.religion!!.founderBeliefUniqueMap.getMatchingUniques(uniqueType, stateForConditionals)) - yieldAll(getCivResourceSupply().asSequence() - .filter { it.amount > 0 && it.resource.uniques.isNotEmpty() } - .flatMap { it.resource.getMatchingUniques(uniqueType, stateForConditionals) } - ) - + yieldAll(civResourcesUniqueMap.getMatchingUniques(uniqueType, stateForConditionals)) yieldAll(gameInfo.ruleset.globalUniques.getMatchingUniques(uniqueType, stateForConditionals)) } diff --git a/core/src/com/unciv/logic/civilization/transients/CivInfoTransientCache.kt b/core/src/com/unciv/logic/civilization/transients/CivInfoTransientCache.kt index e703eb64e2..072a895f2a 100644 --- a/core/src/com/unciv/logic/civilization/transients/CivInfoTransientCache.kt +++ b/core/src/com/unciv/logic/civilization/transients/CivInfoTransientCache.kt @@ -15,10 +15,7 @@ import com.unciv.models.ruleset.Building import com.unciv.models.ruleset.tile.ResourceSupplyList import com.unciv.models.ruleset.tile.ResourceType import com.unciv.models.ruleset.tile.TileImprovement -import com.unciv.models.ruleset.unique.StateForConditionals -import com.unciv.models.ruleset.unique.UniqueTarget -import com.unciv.models.ruleset.unique.UniqueTriggerActivation -import com.unciv.models.ruleset.unique.UniqueType +import com.unciv.models.ruleset.unique.* import com.unciv.models.ruleset.unit.BaseUnit import com.unciv.models.stats.Stats import com.unciv.utils.DebugUtils @@ -359,8 +356,16 @@ class CivInfoTransientCache(val civInfo: Civilization) { // Check if anything has actually changed so we don't update stats for no reason - this uses List equality which means it checks the elements if (civInfo.detailedCivResources == newDetailedCivResources) return + val summarizedResourceSupply = newDetailedCivResources.sumByResource("All") + + val newResourceUniqueMap = UniqueMap() + for (resource in civInfo.summarizedCivResourceSupply) + if (resource.amount > 0) + newResourceUniqueMap.addUniques(resource.resource.uniqueObjects) + civInfo.detailedCivResources = newDetailedCivResources - civInfo.summarizedCivResourceSupply = newDetailedCivResources.sumByResource("All") + civInfo.summarizedCivResourceSupply = summarizedResourceSupply + civInfo.civResourcesUniqueMap = newResourceUniqueMap civInfo.updateStatsForNextTurn() // More or less resources = more or less happiness, with potential domino effects }