Skip to content

Commit

Permalink
Optimize ingredient positions index lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 2, 2024
1 parent 27dcf7a commit 01c4d36
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectSortedMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.cyclops.commoncapabilities.api.ingredient.IIngredientMatcher;
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
import org.cyclops.cyclopscore.datastructure.MultitransformIterator;
import org.cyclops.cyclopscore.ingredient.collection.IIngredientCollapsedCollectionMutable;
Expand Down Expand Up @@ -54,9 +55,18 @@ public Iterator<PartPos> getNonEmptyPositions() {

@Override
public Iterator<PartPos> getPositions(T instance, M matchFlags) {
// Since we store ingredients by prototype in ingredientCollection,
// we can make the match flags more precise,
// and possibly improve performance of the lookup operation.
IIngredientMatcher<T, M> matcher = getComponent().getMatcher();
if (matcher.getExactMatchNoQuantityCondition().equals(matchFlags)) {
matchFlags = matcher.getExactMatchCondition();
}
M finalMatchFlags = matchFlags;

return this.prioritizedPositionsMap.values()
.stream()
.flatMap(ingredientCollection -> ingredientCollection.getAll(getPrototype(instance), matchFlags).stream())
.flatMap(ingredientCollection -> ingredientCollection.getAll(getPrototype(instance), finalMatchFlags).stream())
.flatMap(Collection::stream)
.distinct()
.iterator();
Expand Down

0 comments on commit 01c4d36

Please sign in to comment.