perf(memory): Don't clone units when cloning tiles for 'alternative history' checks

This commit is contained in:
yairm210 2024-12-01 19:42:58 +02:00
parent b1fe404222
commit ef78cd95f9
5 changed files with 10 additions and 8 deletions

View File

@ -439,7 +439,7 @@ class WorkerAutomation(
val removedImprovement = if (removedObject == tile.improvement) removedObject else null
if (removedFeature != null || removedImprovement != null) {
val newTile = tile.clone()
val newTile = tile.clone(addUnits = false)
newTile.setTerrainTransients()
if (removedFeature != null)
newTile.removeTerrainFeature(removedFeature)

View File

@ -193,7 +193,7 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable {
//endregion
fun clone(): Tile {
fun clone(/** For stat diff checks, units are meaningless */ addUnits:Boolean = true): Tile {
val toReturn = Tile()
toReturn.tileMap = tileMap
toReturn.ruleset = ruleset
@ -203,9 +203,11 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable {
toReturn.isLand = isLand
toReturn.isWater = isWater
toReturn.isOcean = isOcean
if (militaryUnit != null) toReturn.militaryUnit = militaryUnit!!.clone()
if (civilianUnit != null) toReturn.civilianUnit = civilianUnit!!.clone()
for (airUnit in airUnits) toReturn.airUnits.add(airUnit.clone())
if (addUnits) {
if (militaryUnit != null) toReturn.militaryUnit = militaryUnit!!.clone()
if (civilianUnit != null) toReturn.civilianUnit = civilianUnit!!.clone()
for (airUnit in airUnits) toReturn.airUnits.add(airUnit.clone())
}
toReturn.position = position.cpy()
toReturn.baseTerrain = baseTerrain
toReturn.terrainFeatures = terrainFeatures // immutable lists can be directly passed around

View File

@ -117,7 +117,7 @@ class TileImprovementFunctions(val tile: Tile) {
tile.ruleset.tileRemovals.firstOrNull { it.name == Constants.remove + feature } }
if (featureRemovals.isEmpty()) return false
if (featureRemovals.any { it !in knownFeatureRemovals }) return false
val clonedTile = tile.clone()
val clonedTile = tile.clone(addUnits = false)
clonedTile.setTerrainFeatures(tile.terrainFeatures.filterNot {
feature -> featureRemovals.any { it.name.removePrefix(Constants.remove) == feature } })
return clonedTile.improvementFunctions.canImprovementBeBuiltHere(improvement, resourceIsVisible, knownFeatureRemovals, stateForConditionals)

View File

@ -273,7 +273,7 @@ class TileStatFunctions(val tile: Tile) {
val currentStats = currentTileStats
?: getTileStats(city, observingCiv, cityUniqueCache)
val tileClone = tile.clone()
val tileClone = tile.clone(addUnits = false)
tileClone.setTerrainTransients()
tileClone.setImprovement(improvement.name)

View File

@ -89,7 +89,7 @@ class ImprovementPickerScreen(
// clone tileInfo without "top" feature if it could be removed
// Keep this copy around for speed
tileWithoutLastTerrain = tile.clone()
tileWithoutLastTerrain = tile.clone(addUnits = false)
tileWithoutLastTerrain.setTerrainTransients()
if (Constants.remove + tileWithoutLastTerrain.lastTerrain.name in ruleset.tileImprovements) {
tileWithoutLastTerrain.removeTerrainFeature(tileWithoutLastTerrain.lastTerrain.name)