From f69209029d76b6520c003bc167f2b742486dbb3e Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 26 Sep 2023 17:44:34 +0300 Subject: [PATCH] performance: #10173 - Use string-to-type map to initialize type for uniques, this is the major time component of unique creation --- core/src/com/unciv/models/ruleset/unique/Unique.kt | 4 ++-- core/src/com/unciv/models/ruleset/unique/UniqueType.kt | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/com/unciv/models/ruleset/unique/Unique.kt b/core/src/com/unciv/models/ruleset/unique/Unique.kt index 04bb7dc3df..5412e170ba 100644 --- a/core/src/com/unciv/models/ruleset/unique/Unique.kt +++ b/core/src/com/unciv/models/ruleset/unique/Unique.kt @@ -21,7 +21,7 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s * - for instance, in the city screen, we call every tile unique for every tile, which can lead to ANRs */ val placeholderText = text.getPlaceholderText() val params = text.getPlaceholderParameters() - val type = UniqueType.values().firstOrNull { it.placeholderText == placeholderText } + val type = UniqueType.uniqueTypeMap[placeholderText] val stats: Stats by lazy { val firstStatParam = params.firstOrNull { Stats.isStats(it) } @@ -41,7 +41,7 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s fun hasFlag(flag: UniqueFlag) = type != null && type.flags.contains(flag) fun hasTriggerConditional(): Boolean { - if(conditionals.none()) return false + if (conditionals.none()) return false return conditionals.any { conditional -> conditional.type?.targetTypes?.any { it.canAcceptUniqueTarget(UniqueTarget.TriggerCondition) || it.canAcceptUniqueTarget(UniqueTarget.UnitActionModifier) diff --git a/core/src/com/unciv/models/ruleset/unique/UniqueType.kt b/core/src/com/unciv/models/ruleset/unique/UniqueType.kt index 869e02a452..6acfda7861 100644 --- a/core/src/com/unciv/models/ruleset/unique/UniqueType.kt +++ b/core/src/com/unciv/models/ruleset/unique/UniqueType.kt @@ -1192,4 +1192,8 @@ enum class UniqueType(val text: String, vararg targets: UniqueTarget, val flags: /** Checks whether a specific [uniqueTarget] as e.g. given by [IHasUniques.getUniqueTarget] works with `this` UniqueType */ fun canAcceptUniqueTarget(uniqueTarget: UniqueTarget) = targetTypes.any { uniqueTarget.canAcceptUniqueTarget(it) } + + companion object { + val uniqueTypeMap: Map = UniqueType.values().associateBy { it.placeholderText } + } }