mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-13 17:04:03 +00:00
Optimize TooltipItemFilter; ItemFilter limited cache (#565)
Co-authored-by: slprime <history-21@yandex.ru> Co-authored-by: Martin Robertz <dream-master@gmx.net>
This commit is contained in:
3 files changed
+88
-48
No files matched your search
@@ -3,8 +3,10 @@ package codechicken.nei;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -86,6 +88,47 @@ public class SearchTokenParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ItemFilterCache implements ItemFilter {
|
||||
|
||||
public ItemFilter filter;
|
||||
private final ItemStackMap<Boolean> states = new ItemStackMap<>();
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
public ItemFilterCache(ItemFilter filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(ItemStack item) {
|
||||
lock.lock();
|
||||
|
||||
try {
|
||||
Boolean match = states.get(item);
|
||||
|
||||
if (match == null) {
|
||||
states.put(item, match = this.filter.matches(item));
|
||||
}
|
||||
|
||||
return match;
|
||||
} catch (Throwable th) {
|
||||
th.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final LinkedHashMap<String, ItemFilter> filtersCache = new LinkedHashMap<>() {
|
||||
|
||||
private static final long serialVersionUID = 1042213947848622164L;
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<String, ItemFilter> eldest) {
|
||||
return size() > 20;
|
||||
}
|
||||
};
|
||||
|
||||
protected final List<ISearchParserProvider> searchProviders;
|
||||
protected final ProvidersCache providersCache = new ProvidersCache();
|
||||
protected final TCharCharMap prefixRedefinitions = new TCharCharHashMap();
|
||||
@@ -134,17 +177,26 @@ public class SearchTokenParser {
|
||||
}
|
||||
|
||||
public ItemFilter getFilter(String filterText) {
|
||||
final String[] parts = EnumChatFormatting.getTextWithoutFormattingCodes(filterText).toLowerCase().split("\\|");
|
||||
final List<ItemFilter> searchTokens = Arrays.stream(parts).map(this::parseSearchText).filter(s -> s != null)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
if (searchTokens.isEmpty()) {
|
||||
return new EverythingItemFilter();
|
||||
} else if (searchTokens.size() == 1) {
|
||||
return new IsRegisteredItemFilter(searchTokens.get(0));
|
||||
} else {
|
||||
return new IsRegisteredItemFilter(new AnyMultiItemFilter(searchTokens));
|
||||
if (!filtersCache.containsKey(filterText)) {
|
||||
final String[] parts = EnumChatFormatting.getTextWithoutFormattingCodes(filterText).toLowerCase()
|
||||
.split("\\|");
|
||||
final List<ItemFilter> searchTokens = Arrays.stream(parts).map(this::parseSearchText).filter(s -> s != null)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
if (searchTokens.isEmpty()) {
|
||||
filtersCache.put(filterText, new EverythingItemFilter());
|
||||
} else if (searchTokens.size() == 1) {
|
||||
filtersCache.put(filterText, new IsRegisteredItemFilter(new ItemFilterCache(searchTokens.get(0))));
|
||||
} else {
|
||||
filtersCache.put(
|
||||
filterText,
|
||||
new IsRegisteredItemFilter(new ItemFilterCache(new AnyMultiItemFilter(searchTokens))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return filtersCache.get(filterText);
|
||||
}
|
||||
|
||||
public Pattern getSplitPattern() {
|
||||
|
||||
Reference in new issue
Block a user