From b67b92de451c1fed372a44c8cb17d9df85e6ae0f Mon Sep 17 00:00:00 2001 From: yairm210 Date: Thu, 25 Apr 2024 14:15:01 +0300 Subject: [PATCH] Perf: memory optimizations --- .../unciv/logic/civilization/diplomacy/DiplomacyManager.kt | 4 ++-- core/src/com/unciv/models/ruleset/unit/BaseUnit.kt | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt b/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt index cb9e9871c6..9350fa3e48 100644 --- a/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt +++ b/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt @@ -415,9 +415,9 @@ class DiplomacyManager() : IsPartOfGameInfoSerialization { } /** Returns the [civilizations][Civilization] that know about both sides ([civInfo] and [otherCiv]) */ - fun getCommonKnownCivs(): Set = civInfo.getKnownCivs().toSet().intersect(otherCiv().getKnownCivs().toSet()) + fun getCommonKnownCivs(): Set = civInfo.getKnownCivs().asIterable().intersect(otherCiv().getKnownCivs().toSet()) - fun getCommonKnownCivsWithSpectators(): Set = civInfo.getKnownCivsWithSpectators().toSet().intersect(otherCiv().getKnownCivsWithSpectators().toSet()) + fun getCommonKnownCivsWithSpectators(): Set = civInfo.getKnownCivsWithSpectators().asIterable().intersect(otherCiv().getKnownCivsWithSpectators().toSet()) /** Returns true when the [civInfo]'s territory is considered allied for [otherCiv]. * This includes friendly and allied city-states and the open border treaties. */ diff --git a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt index 75277c6a9e..c7b19b1222 100644 --- a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt +++ b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt @@ -114,6 +114,11 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction { if (! ::ruleset.isInitialized) { // Not sure if this will ever actually happen, but better safe than sorry return ourUniques } + val typeUniques = type.getMatchingUniques(uniqueType, stateForConditionals) + // Memory optimization - very rarely do we actually get uniques from both sources, + // and sequence addition is expensive relative to the rare case that we'll actually need it + if (ourUniques.none()) return typeUniques + if (typeUniques.none()) return ourUniques return ourUniques + type.getMatchingUniques(uniqueType, stateForConditionals) } @@ -423,6 +428,7 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction { return false } + fun isRanged() = rangedStrength > 0 fun isMelee() = !isRanged() && strength > 0 fun isMilitary() = isRanged() || isMelee()