Still scratching a little of performance impros by avoiding checking class instance.

When StatTypeFilter is used withing Deck Editor (with all PaperCard instance in ItemPool), avoiding checking instance class improves performance a bit (saving a couple of hundreds of milliseconds).

However, when this filter with also other inventory items (pack or deck), the same code as before is executed.
This commit is contained in:
leriomaggio
2021-08-28 16:47:10 +01:00
parent 42e91b60ea
commit 7613b27e6e
2 changed files with 24 additions and 12 deletions

View File

@@ -137,13 +137,16 @@ public class ItemPool<T extends InventoryItem> implements Iterable<Entry<T, Inte
return count;
}
public final int countAll(Predicate<T> condition) {
public final <U extends InventoryItem> int countAll(Predicate<U> condition){
int count = 0;
for (Entry<T, Integer> e : this) {
if (condition.apply(e.getKey())) {
count += e.getValue();
Iterable<T> matchingKeys = Iterables.filter(this.items.keySet(), new Predicate<T>() {
@Override
public boolean apply(T item) {
return condition.apply((U)item);
}
}
});
for (T key : matchingKeys)
count += this.items.get(key);
return count;
}

View File

@@ -75,14 +75,23 @@ public abstract class StatTypeFilter<T extends InventoryItem> extends ToggleButt
if (btnPackOrDeck != null) { //support special pack/deck case
int count = items.countAll(ItemPredicate.Presets.IS_PACK_OR_DECK, InventoryItem.class);
btnPackOrDeck.setText(String.valueOf(count));
}
Iterator<StatTypes> buttonMapStatsIterator = buttonMap.keySet().iterator();
while (buttonMapStatsIterator.hasNext()){
StatTypes statTypes = buttonMapStatsIterator.next();
if (statTypes.predicate != null){
int count = items.countAll(Predicates.compose(statTypes.predicate, PaperCard.FN_GET_RULES), PaperCard.class);
buttonMap.get(statTypes).setText(String.valueOf(count));
Iterator<StatTypes> buttonMapStatsIterator = buttonMap.keySet().iterator();
while (buttonMapStatsIterator.hasNext()){
StatTypes statTypes = buttonMapStatsIterator.next();
if (statTypes.predicate != null){
count = items.countAll(Predicates.compose(statTypes.predicate, PaperCard.FN_GET_RULES), PaperCard.class);
buttonMap.get(statTypes).setText(String.valueOf(count));
}
}
} else {
Iterator<StatTypes> buttonMapStatsIterator = buttonMap.keySet().iterator();
while (buttonMapStatsIterator.hasNext()) {
StatTypes statTypes = buttonMapStatsIterator.next();
if (statTypes.predicate != null) {
int count = items.countAll(Predicates.compose(statTypes.predicate, PaperCard.FN_GET_RULES));
buttonMap.get(statTypes).setText(String.valueOf(count));
}
}
}
getWidget().revalidate();