From 01c4d36f1ea0c02a85c647a5a539ee78f8c579a7 Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Fri, 1 Nov 2024 16:42:03 +0100 Subject: [PATCH] Optimize ingredient positions index lookups Closes CyclopsMC/IntegratedTunnels#307 --- .../core/network/IngredientPositionsIndex.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cyclops/integrateddynamics/core/network/IngredientPositionsIndex.java b/src/main/java/org/cyclops/integrateddynamics/core/network/IngredientPositionsIndex.java index a3336e46f5c..cebf5140df3 100644 --- a/src/main/java/org/cyclops/integrateddynamics/core/network/IngredientPositionsIndex.java +++ b/src/main/java/org/cyclops/integrateddynamics/core/network/IngredientPositionsIndex.java @@ -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; @@ -54,9 +55,18 @@ public Iterator getNonEmptyPositions() { @Override public Iterator 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 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();