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 (!)
This commit is contained in:
yairm210 2024-09-08 16:19:23 +03:00
parent 7c46b3533b
commit 2647b24e09
2 changed files with 15 additions and 15 deletions

View File

@ -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))
}

View File

@ -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
}