performance: #10173 - Use string-to-type map to initialize type for uniques, this is the major time component of unique creation

This commit is contained in:
Yair Morgenstern
2023-09-26 17:44:34 +03:00
parent 5d22482a56
commit f69209029d
2 changed files with 6 additions and 2 deletions

View File

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

View File

@ -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<String, UniqueType> = UniqueType.values().associateBy { it.placeholderText }
}
}