Skip to content

Commit

Permalink
Improve performance and memory size of the ingredient filter (#2734)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Jan 25, 2023
1 parent 3aa7239 commit 3b8f226
Show file tree
Hide file tree
Showing 30 changed files with 1,379 additions and 788 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Expand Up @@ -16,5 +16,5 @@ curse_project_id=238222

# Version
version_major=7
version_minor=7
version_patch=1
version_minor=8
version_patch=0
13 changes: 6 additions & 7 deletions src/main/java/mezz/jei/collect/IngredientSet.java
@@ -1,17 +1,17 @@
package mezz.jei.collect;

import javax.annotation.Nullable;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.subtypes.UidContext;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.subtypes.UidContext;

public class IngredientSet<V> extends AbstractSet<V> {
public static <V> IngredientSet<V> create(IIngredientHelper<V> ingredientHelper, UidContext context) {
final Function<V, String> uidGenerator = v -> ingredientHelper.getUniqueId(v, context);
Expand Down Expand Up @@ -59,9 +59,8 @@ public boolean contains(Object o) {
return ingredients.containsKey(uid);
}

@Nullable
public V getByUid(String uid) {
return ingredients.get(uid);
public Optional<V> getByUid(String uid) {
return Optional.ofNullable(ingredients.get(uid));
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/mezz/jei/collect/MultiMap.java
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMultimap;

Expand Down Expand Up @@ -52,6 +53,16 @@ public Set<K> keySet() {
return map.keySet();
}

public Collection<V> allValues() {
return this.map.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

public void clear() {
map.clear();
}

public ImmutableMultimap<K, V> toImmutable() {
ImmutableMultimap.Builder<K, V> builder = ImmutableMultimap.builder();
for (Map.Entry<K, T> entry : map.entrySet()) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/mezz/jei/config/BookmarkConfig.java
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import mezz.jei.api.ingredients.subtypes.UidContext;
Expand Down Expand Up @@ -111,9 +112,9 @@ private <T> String getUid(IIngredientManager ingredientManager, IIngredientListE
@Nullable
private Object getUnknownIngredientByUid(IngredientManager ingredientManager, Collection<IIngredientType<?>> ingredientTypes, String uid) {
for (IIngredientType<?> ingredientType : ingredientTypes) {
Object ingredient = ingredientManager.getIngredientByUid(ingredientType, uid);
if (ingredient != null) {
return ingredient;
Optional<?> ingredient = ingredientManager.getIngredientByUid(ingredientType, uid);
if (ingredient.isPresent()) {
return ingredient.get();
}
}
return null;
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/mezz/jei/config/EditModeConfig.java
Expand Up @@ -9,6 +9,8 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.subtypes.UidContext;
Expand Down Expand Up @@ -71,7 +73,8 @@ public <V> void addIngredientToConfigBlacklist(IngredientFilter ingredientFilter
// combine item-level blacklist into wildcard-level ones
if (blacklistType == IngredientBlacklistType.ITEM) {
final String uid = getIngredientUid(ingredient, IngredientBlacklistType.ITEM, ingredientHelper);
List<IIngredientListElementInfo<V>> elementsToBeBlacklisted = ingredientFilter.getMatches(ingredient, ingredientHelper, (input) -> getIngredientUid(input, IngredientBlacklistType.WILDCARD, ingredientHelper));
List<IIngredientListElementInfo<V>> elementsToBeBlacklisted = ingredientFilter.searchForMatchingElement(ingredient, ingredientHelper, (input) -> getIngredientUid(input, IngredientBlacklistType.WILDCARD, ingredientHelper))
.collect(Collectors.toList());
if (areAllBlacklisted(elementsToBeBlacklisted, ingredientHelper, uid)) {
if (addIngredientToConfigBlacklist(ingredientFilter, ingredient, IngredientBlacklistType.WILDCARD, ingredientHelper)) {
saveBlacklist();
Expand All @@ -89,7 +92,8 @@ private <V> boolean addIngredientToConfigBlacklist(IngredientFilter ingredientFi

// remove lower-level blacklist entries when a higher-level one is added
if (blacklistType == IngredientBlacklistType.WILDCARD) {
List<IIngredientListElementInfo<V>> elementsToBeBlacklisted = ingredientFilter.getMatches(ingredient, ingredientHelper, (input) -> getIngredientUid(input, blacklistType, ingredientHelper));
List<IIngredientListElementInfo<V>> elementsToBeBlacklisted = ingredientFilter.searchForMatchingElement(ingredient, ingredientHelper, (input) -> getIngredientUid(input, blacklistType, ingredientHelper))
.collect(Collectors.toList());
for (IIngredientListElementInfo<V> elementToBeBlacklistedInfo : elementsToBeBlacklisted) {
IIngredientListElement<V> elementToBeBlacklisted = elementToBeBlacklistedInfo.getElement();
V ingredientToBeBlacklisted = elementToBeBlacklisted.getIngredient();
Expand Down Expand Up @@ -119,21 +123,24 @@ private <V> boolean areAllBlacklisted(List<IIngredientListElementInfo<V>> elemen
public <V> void removeIngredientFromConfigBlacklist(IngredientFilter ingredientFilter, IIngredientManager ingredientManager, V ingredient, IngredientBlacklistType blacklistType, IIngredientHelper<V> ingredientHelper) {
boolean updated = false;

Function<V, String> uidFunction = (input) -> getIngredientUid(input, IngredientBlacklistType.WILDCARD, ingredientHelper);
if (blacklistType == IngredientBlacklistType.ITEM) {
// deconstruct any wildcard blacklist since we are removing one element from it
final String wildUid = getIngredientUid(ingredient, IngredientBlacklistType.WILDCARD, ingredientHelper);
if (blacklist.contains(wildUid)) {
updated = true;
blacklist.remove(wildUid);
List<IIngredientListElementInfo<V>> modMatches = ingredientFilter.getMatches(ingredient, ingredientHelper, (input) -> getIngredientUid(input, IngredientBlacklistType.WILDCARD, ingredientHelper));
List<IIngredientListElementInfo<V>> modMatches = ingredientFilter.searchForMatchingElement(ingredient, ingredientHelper, uidFunction)
.collect(Collectors.toList());
for (IIngredientListElementInfo<V> modMatch : modMatches) {
IIngredientListElement<V> element = modMatch.getElement();
addIngredientToConfigBlacklist(ingredientFilter, element.getIngredient(), IngredientBlacklistType.ITEM, ingredientHelper);
}
}
} else if (blacklistType == IngredientBlacklistType.WILDCARD) {
// remove any item-level blacklist on items that match this wildcard
List<IIngredientListElementInfo<V>> modMatches = ingredientFilter.getMatches(ingredient, ingredientHelper, (input) -> getIngredientUid(input, IngredientBlacklistType.WILDCARD, ingredientHelper));
List<IIngredientListElementInfo<V>> modMatches = ingredientFilter.searchForMatchingElement(ingredient, ingredientHelper, uidFunction)
.collect(Collectors.toList());
for (IIngredientListElementInfo<V> modMatch : modMatches) {
IIngredientListElement<V> element = modMatch.getElement();
V matchIngredient = element.getIngredient();
Expand Down

0 comments on commit 3b8f226

Please sign in to comment.