Skip to content

Commit a72b8bd

Browse files
committed
user singleton lists in recipe cache/indexes where applicable
1 parent 7a12d23 commit a72b8bd

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/main/java/mekanism/common/recipe/lookup/cache/type/BaseInputCache.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ public Iterable<RECIPE> getRecipes(INPUT input) {
7575
* @param recipe Recipe to add.
7676
*/
7777
protected void addInputCache(KEY input, RECIPE recipe) {
78-
inputCache.computeIfAbsent(input, i -> new ArrayList<>()).add(recipe);
78+
if (!inputCache.containsKey(input)) {
79+
inputCache.put(input, Collections.singletonList(recipe));
80+
} else {
81+
List<RECIPE> existing = inputCache.get(input);
82+
if (existing.size() == 1) {
83+
List<RECIPE> newList = new ArrayList<>(existing);
84+
newList.add(recipe);
85+
inputCache.put(input, newList);
86+
} else {
87+
existing.add(recipe);
88+
}
89+
}
7990
}
8091

8192
/**

src/main/java/mekanism/common/recipe/lookup/cache/type/ComponentSensitiveInputCache.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.google.common.collect.Iterables;
44
import it.unimi.dsi.fastutil.Hash;
55
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
6+
import java.util.ArrayList;
67
import java.util.Collection;
78
import java.util.Collections;
8-
import java.util.HashSet;
9+
import java.util.List;
910
import java.util.Map;
10-
import java.util.Set;
1111
import mekanism.api.recipes.MekanismRecipe;
1212
import mekanism.api.recipes.ingredients.InputIngredient;
1313

@@ -22,7 +22,7 @@ public abstract class ComponentSensitiveInputCache<KEY, INPUT, INGREDIENT extend
2222
* Map of NBT based keys representing inputs to a set of the recipes that contain said input. This allows for quick contains checking by checking if a key exists, as
2323
* well as quicker recipe lookup.
2424
*/
25-
private final Map<INPUT, Set<RECIPE>> componentInputCache;
25+
private final Map<INPUT, List<RECIPE>> componentInputCache;
2626

2727
protected ComponentSensitiveInputCache(Hash.Strategy<? super INPUT> componentHashStrategy) {
2828
this.componentInputCache = new Object2ObjectOpenCustomHashMap<>(componentHashStrategy);
@@ -51,7 +51,7 @@ public boolean contains(INPUT input) {
5151
*/
5252
@Override
5353
public Iterable<RECIPE> getRecipes(INPUT input) {
54-
Set<RECIPE> nbtRecipes = componentInputCache.getOrDefault(input, Collections.emptySet());
54+
List<RECIPE> nbtRecipes = componentInputCache.getOrDefault(input, Collections.emptyList());
5555
if (nbtRecipes.isEmpty()) {
5656
return super.getRecipes(input);
5757
}
@@ -69,6 +69,17 @@ public Iterable<RECIPE> getRecipes(INPUT input) {
6969
* @param recipe Recipe to add.
7070
*/
7171
protected void addNbtInputCache(INPUT input, RECIPE recipe) {
72-
componentInputCache.computeIfAbsent(input, i -> new HashSet<>()).add(recipe);
72+
if (!componentInputCache.containsKey(input)) {
73+
componentInputCache.put(input, Collections.singletonList(recipe));
74+
} else {
75+
List<RECIPE> existing = componentInputCache.get(input);
76+
if (existing.size() == 1) {
77+
List<RECIPE> newList = new ArrayList<>(existing);
78+
newList.add(recipe);
79+
componentInputCache.put(input, newList);
80+
} else {
81+
existing.add(recipe);
82+
}
83+
}
7384
}
7485
}

0 commit comments

Comments
 (0)