Skip to content

Commit 900d81f

Browse files
committed
Revert "Added enumset to uniquemap to speed up getTriggeredUniques calls - should make iterating on all units and getting all unit triggers, viable :D"
This reverts commit b7a5274.
1 parent b7a5274 commit 900d81f

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

core/src/com/unciv/models/ruleset/unique/UniqueMap.kt

+9-17
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ open class UniqueMap() {
1010
// 750 including deprecated, and EnumMap creates a N-sized array where N is the number of objects in the enum
1111
private val typedUniqueMap = EnumMap<UniqueType, ArrayList<Unique>>(UniqueType::class.java)
1212

13-
// Another memory-speed tradeoff - enumset is super fast and also super cheap, but it's not nothing
14-
// This is used to speed up triggered uniques - in other words, when we want to find all uniques with a certain modifier.
15-
// Rather than mapping all uniques thus triggered, this just stores whether any unique has that trigger -
16-
// because most of the time is spent iterating on uniques, in uniquemaps that have no such trigger in the first place!
17-
private val triggerEnumSet = EnumSet.noneOf(UniqueType::class.java)
18-
1913
constructor(uniques: Sequence<Unique>) : this() {
2014
addUniques(uniques.asIterable())
2115
}
@@ -27,11 +21,10 @@ open class UniqueMap() {
2721
val existingArrayList = innerUniqueMap[unique.placeholderText]
2822
if (existingArrayList != null) existingArrayList.add(unique)
2923
else innerUniqueMap[unique.placeholderText] = arrayListOf(unique)
30-
24+
3125
if (unique.type == null) return
3226
if (typedUniqueMap[unique.type] != null) return
3327
typedUniqueMap[unique.type] = innerUniqueMap[unique.placeholderText]
34-
triggerEnumSet.add(unique.type)
3528
}
3629

3730
/** Calls [addUnique] on each item from [uniques] */
@@ -43,20 +36,20 @@ open class UniqueMap() {
4336
val existingArrayList = innerUniqueMap[unique.placeholderText]
4437
existingArrayList?.remove(unique)
4538
}
46-
39+
4740
fun clear() {
4841
innerUniqueMap.clear()
4942
typedUniqueMap.clear()
5043
}
51-
44+
5245
// Pure functions
53-
46+
5447
fun hasUnique(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals.EmptyState) =
5548
getUniques(uniqueType).any { it.conditionalsApply(state) && !it.isTimedTriggerable }
5649

5750
fun hasUnique(uniqueTag: String, state: StateForConditionals = StateForConditionals.EmptyState) =
5851
getUniques(uniqueTag).any { it.conditionalsApply(state) && !it.isTimedTriggerable }
59-
52+
6053
fun hasTagUnique(tagUnique: String) =
6154
innerUniqueMap.containsKey(tagUnique)
6255

@@ -69,7 +62,7 @@ open class UniqueMap() {
6962
?.asSequence()
7063
?: emptySequence()
7164

72-
fun getMatchingUniques(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals.EmptyState) =
65+
fun getMatchingUniques(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals.EmptyState) =
7366
getUniques(uniqueType)
7467
// Same as .filter | .flatMap, but more cpu/mem performant (7.7 GB vs ?? for test)
7568
.flatMap {
@@ -90,8 +83,8 @@ open class UniqueMap() {
9083
else -> it.getMultiplied(state)
9184
}
9285
}
93-
94-
fun hasMatchingUnique(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals.EmptyState) =
86+
87+
fun hasMatchingUnique(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals.EmptyState) =
9588
getUniques(uniqueType).any { it.conditionalsApply(state) }
9689

9790
fun hasMatchingUnique(uniqueTag: String, state: StateForConditionals = StateForConditionals.EmptyState) =
@@ -102,12 +95,11 @@ open class UniqueMap() {
10295

10396
fun getTriggeredUniques(trigger: UniqueType, stateForConditionals: StateForConditionals,
10497
triggerFilter: (Unique) -> Boolean = { true }): Sequence<Unique> {
105-
if (!triggerEnumSet.contains(trigger)) return emptySequence() // Common case - no such unique exists
10698
return getAllUniques().filter { unique ->
10799
unique.getModifiers(trigger).any(triggerFilter) && unique.conditionalsApply(stateForConditionals)
108100
}.flatMap { it.getMultiplied(stateForConditionals) }
109101
}
110-
102+
111103
companion object{
112104
val EMPTY = UniqueMap()
113105
}

0 commit comments

Comments
 (0)