Skip to content

Commit

Permalink
Optimize ingredient positions index
Browse files Browse the repository at this point in the history
This improves performance with Integrated Tunnels and Terminals.
  • Loading branch information
rubensworks committed Oct 28, 2024
1 parent 1d88724 commit 61b7372
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.cyclops.cyclopscore.ingredient.collection.IIngredientMapMutable;
import org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionHelpers;
import org.cyclops.cyclopscore.ingredient.collection.IngredientHashMap;
import org.cyclops.cyclopscore.ingredient.collection.IngredientMapSingleClassified;
import org.cyclops.integrateddynamics.api.ingredient.IIngredientPositionsIndex;
import org.cyclops.integrateddynamics.api.part.PartPos;
import org.cyclops.integrateddynamics.api.part.PrioritizedPartPos;
Expand Down Expand Up @@ -66,7 +67,7 @@ public Iterator<PartPos> getPositions(T instance, M matchFlags) {
public void addPosition(T instance, PrioritizedPartPos pos) {
IIngredientMapMutable<T, M, ObjectOpenHashSet<PartPos>> positionsMap = this.prioritizedPositionsMap.get(getInternalPriority(pos));
if (positionsMap == null) {
positionsMap = new IngredientHashMap<>(getComponent());
positionsMap = createCollapsedMap(getComponent());
this.prioritizedPositionsMap.put(getInternalPriority(pos), positionsMap);
}

Expand All @@ -80,6 +81,17 @@ public void addPosition(T instance, PrioritizedPartPos pos) {
set.add(pos.getPartPos());
}

// If this would ever be needed in other places as well, let's move this to IngredientCollectionHelpers
protected static <T, M> IIngredientMapMutable<T, M, ObjectOpenHashSet<PartPos>> createCollapsedMap(IngredientComponent<T, M> ingredientComponent) {
if (ingredientComponent.getCategoryTypes().size() == 1) {
return new IngredientHashMap<>(ingredientComponent);
}
return new IngredientMapSingleClassified<>(
ingredientComponent,
() -> new IngredientHashMap<>(ingredientComponent),
ingredientComponent.getCategoryTypes().get(0));
}

@Override
public void removePosition(T instance, PrioritizedPartPos pos) {
IIngredientMapMutable<T, M, ObjectOpenHashSet<PartPos>> positionsMap = this.prioritizedPositionsMap.get(getInternalPriority(pos));
Expand Down

0 comments on commit 61b7372

Please sign in to comment.