From 9c930347c39730c3fa4c901978723e94acdd4c66 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 26 Sep 2023 16:56:16 +0300 Subject: [PATCH] performance: #10173 - Don't need to always create hashsets, don't need linked hash set --- .../models/ruleset/validation/RulesetValidator.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/com/unciv/models/ruleset/validation/RulesetValidator.kt b/core/src/com/unciv/models/ruleset/validation/RulesetValidator.kt index 1d8e495221..4ab4de321c 100644 --- a/core/src/com/unciv/models/ruleset/validation/RulesetValidator.kt +++ b/core/src/com/unciv/models/ruleset/validation/RulesetValidator.kt @@ -595,7 +595,7 @@ class RulesetValidator(val ruleset: Ruleset) { } private fun checkPromotionCircularReferences(lines: RulesetErrorList) { - fun recursiveCheck(history: LinkedHashSet, promotion: Promotion, level: Int) { + fun recursiveCheck(history: HashSet, promotion: Promotion, level: Int) { if (promotion in history) { lines.add("Circular Reference in Promotions: ${history.joinToString("→") { it.name }}→${promotion.name}", RulesetErrorSeverity.Warning) @@ -605,11 +605,16 @@ class RulesetValidator(val ruleset: Ruleset) { history.add(promotion) for (prerequisiteName in promotion.prerequisites) { val prerequisite = ruleset.unitPromotions[prerequisiteName] ?: continue - recursiveCheck(history.toCollection(linkedSetOf()), prerequisite, level + 1) + // Performance - if there's only one prerequisite, we can send this linked set as-is, since no one else will be using it + val linkedSetToPass = + if (promotion.prerequisites.size == 1) history + else history.toCollection(hashSetOf()) + recursiveCheck(linkedSetToPass, prerequisite, level + 1) } } for (promotion in ruleset.unitPromotions.values) { - recursiveCheck(linkedSetOf(), promotion, 0) + if (promotion.prerequisites.isEmpty()) continue + recursiveCheck(hashSetOf(), promotion, 0) } }