Переглянути джерело

Performance improvements to search filtering and fix filtering not even working.

Signed-off-by: shedaniel <daniel@shedaniel.me>
shedaniel 5 роки тому
батько
коміт
267d7119eb

+ 2 - 2
gradle.properties

@@ -1,11 +1,11 @@
 org.gradle.jvmargs=-Xmx3G
-mod_version=4.9.1
+mod_version=4.9.2
 supported_version=1.16.x
 minecraft_version=1.16.1
 yarn_version=1.16.1+build.4+legacy.20w09a+build.8
 fabricloader_version=0.8.8+build.202
 cloth_client_events_v0_version=1.0.4
-cloth_config_version=4.5.6
+cloth_config_version=4.6.0
 modmenu_version=1.12.2+build.16
 fabric_api=0.13.1+build.370-1.16
 autoconfig1u=3.2.0-unstable

+ 1 - 0
src/main/java/me/shedaniel/rei/gui/config/entry/FilteringEntry.java

@@ -88,6 +88,7 @@ public class FilteringEntry extends AbstractConfigListEntry<List<EntryStack>> {
     
     @Override
     public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
+        super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
         Window window = MinecraftClient.getInstance().getWindow();
         this.buttonWidget.active = this.isEditable();
         this.buttonWidget.y = y;

+ 1 - 0
src/main/java/me/shedaniel/rei/gui/config/entry/NoFilteringEntry.java

@@ -74,6 +74,7 @@ public class NoFilteringEntry extends AbstractConfigListEntry<List<EntryStack>>
     
     @Override
     public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
+        super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
         Window window = MinecraftClient.getInstance().getWindow();
         this.buttonWidget.active = false;
         this.buttonWidget.y = y;

+ 1 - 0
src/main/java/me/shedaniel/rei/gui/config/entry/ReloadPluginsEntry.java

@@ -79,6 +79,7 @@ public class ReloadPluginsEntry extends AbstractConfigListEntry<Unit> {
     
     @Override
     public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
+        super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
         Window window = MinecraftClient.getInstance().getWindow();
         this.buttonWidget.active = this.isEditable();
         this.buttonWidget.y = y;

+ 1 - 1
src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java

@@ -529,7 +529,7 @@ public class EntryListWidget extends WidgetWithBounds {
                 } else {
                     for (EntryStack stack : stacks) {
                         if (canLastSearchTermsBeAppliedTo(stack)) {
-                            if (workingItems != null && workingItems.contains(stack))
+                            if (workingItems != null && !workingItems.contains(stack.hashIgnoreAmount()))
                                 continue;
                             list.add(stack.copy().setting(EntryStack.Settings.RENDER_COUNTS, EntryStack.Settings.FALSE).setting(EntryStack.Settings.Item.RENDER_ENCHANTMENT_GLINT, RENDER_ENCHANTMENT_GLINT));
                         }

+ 3 - 9
src/main/java/me/shedaniel/rei/impl/AmountIgnoredEntryStackWrapper.java

@@ -34,26 +34,20 @@ import java.util.Objects;
 @Environment(EnvType.CLIENT)
 public class AmountIgnoredEntryStackWrapper {
     private final EntryStack stack;
-    private int hash = -1390123012;
+    private int hash;
     
     public AmountIgnoredEntryStackWrapper(EntryStack stack) {
         this.stack = Objects.requireNonNull(stack);
+        this.hash = stack.hashIgnoreAmount();
     }
     
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-        
-        AmountIgnoredEntryStackWrapper that = (AmountIgnoredEntryStackWrapper) o;
-        return hashCode() == that.hashCode();
+        return o instanceof AmountIgnoredEntryStackWrapper && hashCode() == o.hashCode();
     }
     
     @Override
     public int hashCode() {
-        if (hash == -1390123012) {
-            hash = stack.hashIgnoreAmount();
-        }
         return hash;
     }
     

+ 2 - 5
src/main/java/me/shedaniel/rei/impl/ItemEntryStack.java

@@ -253,11 +253,9 @@ public class ItemEntryStack extends AbstractEntryStack implements OptimalEntrySt
     
     @Override
     public int hashIgnoreAmount() {
-        int result = 1;
-        result = 31 * result + getType().hashCode();
-        result = 31 * result + itemStack.getItem().hashCode();
+        int result = hashIgnoreAmountAndTags();
         if (itemStack.hasTag()) {
-            result = 31 * result + itemStack.getTag().asString().hashCode();
+            result = 31 * result + itemStack.getTag().toString().hashCode();
         } else {
             result = 31 * result;
         }
@@ -267,7 +265,6 @@ public class ItemEntryStack extends AbstractEntryStack implements OptimalEntrySt
     @Override
     public int hashIgnoreAmountAndTags() {
         int result = 1;
-        result = 31 * result + getType().hashCode();
         result = 31 * result + itemStack.getItem().hashCode();
         return result;
     }

+ 9 - 4
src/main/java/me/shedaniel/rei/impl/filtering/FilteringContextImpl.java

@@ -47,8 +47,13 @@ public class FilteringContextImpl implements FilteringContext {
     private final Map<FilteringContextType, Collection<EntryStack>> cachedStacks;
     
     public FilteringContextImpl(List<EntryStack> allStacks) {
-        this(Maps.newHashMap());
-        getUnsetStacks().addAll(allStacks);
+        this.stacks = Maps.newHashMap();
+        this.cachedStacks = Maps.newHashMap();
+        for (FilteringContextType type : FilteringContextType.values()) {
+            this.stacks.computeIfAbsent(type, t -> Sets.newHashSet());
+        }
+        this.stacks.get(FilteringContextType.DEFAULT).addAll(CollectionUtils.map(allStacks, AmountIgnoredEntryStackWrapper::new));
+        fillCache();
     }
     
     public FilteringContextImpl(Map<FilteringContextType, Set<AmountIgnoredEntryStackWrapper>> stacks) {
@@ -73,8 +78,8 @@ public class FilteringContextImpl implements FilteringContext {
     }
     
     public void handleResult(FilteringResult result) {
-        Collection<AmountIgnoredEntryStackWrapper> hiddenStacks = CollectionUtils.map(result.getHiddenStacks(), AmountIgnoredEntryStackWrapper::new);
-        Collection<AmountIgnoredEntryStackWrapper> shownStacks = CollectionUtils.map(result.getShownStacks(), AmountIgnoredEntryStackWrapper::new);
+        Collection<AmountIgnoredEntryStackWrapper> hiddenStacks = result.getHiddenStacks();
+        Collection<AmountIgnoredEntryStackWrapper> shownStacks = result.getShownStacks();
         
         List<CompletableFuture<Void>> completableFutures = Lists.newArrayList();
         completableFutures.add(CompletableFuture.runAsync(() -> {

+ 32 - 6
src/main/java/me/shedaniel/rei/impl/filtering/FilteringResult.java

@@ -25,13 +25,17 @@ package me.shedaniel.rei.impl.filtering;
 
 import com.google.common.collect.Lists;
 import me.shedaniel.rei.api.EntryStack;
+import me.shedaniel.rei.impl.AmountIgnoredEntryStackWrapper;
+import me.shedaniel.rei.utils.CollectionUtils;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
 import org.jetbrains.annotations.ApiStatus;
 
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
+@Environment(EnvType.CLIENT)
 @ApiStatus.Internal
 @ApiStatus.Experimental
 public interface FilteringResult {
@@ -43,24 +47,46 @@ public interface FilteringResult {
         return new FilteringResultImpl(hiddenStacks, shownStacks);
     }
     
-    Set<EntryStack> getHiddenStacks();
+    Set<AmountIgnoredEntryStackWrapper> getHiddenStacks();
     
-    Set<EntryStack> getShownStacks();
+    Set<AmountIgnoredEntryStackWrapper> getShownStacks();
     
     default FilteringResult hide(EntryStack stack) {
-        return hide(Collections.singletonList(stack));
+        getHiddenStacks().add(new AmountIgnoredEntryStackWrapper(stack));
+        return this;
     }
     
     default FilteringResult hide(Collection<EntryStack> stacks) {
-        getHiddenStacks().addAll(stacks);
+        getHiddenStacks().addAll(CollectionUtils.map(stacks, AmountIgnoredEntryStackWrapper::new));
         return this;
     }
     
     default FilteringResult show(EntryStack stack) {
-        return show(Collections.singletonList(stack));
+        getShownStacks().add(new AmountIgnoredEntryStackWrapper(stack));
+        return this;
     }
     
     default FilteringResult show(Collection<EntryStack> stacks) {
+        getShownStacks().addAll(CollectionUtils.map(stacks, AmountIgnoredEntryStackWrapper::new));
+        return this;
+    }
+    
+    default FilteringResult hideW(AmountIgnoredEntryStackWrapper stack) {
+        getHiddenStacks().add(stack);
+        return this;
+    }
+    
+    default FilteringResult hideW(Collection<AmountIgnoredEntryStackWrapper> stacks) {
+        getHiddenStacks().addAll(stacks);
+        return this;
+    }
+    
+    default FilteringResult showW(AmountIgnoredEntryStackWrapper stack) {
+        getShownStacks().add(stack);
+        return this;
+    }
+    
+    default FilteringResult showW(Collection<AmountIgnoredEntryStackWrapper> stacks) {
         getShownStacks().addAll(stacks);
         return this;
     }

+ 12 - 9
src/main/java/me/shedaniel/rei/impl/filtering/FilteringResultImpl.java

@@ -23,30 +23,33 @@
 
 package me.shedaniel.rei.impl.filtering;
 
+import com.google.common.collect.Sets;
 import me.shedaniel.rei.api.EntryStack;
+import me.shedaniel.rei.impl.AmountIgnoredEntryStackWrapper;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
 
-import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
-import java.util.TreeSet;
 
+@Environment(EnvType.CLIENT)
 public class FilteringResultImpl implements FilteringResult {
-    private final Set<EntryStack> hiddenStacks, shownStacks;
+    private final Set<AmountIgnoredEntryStackWrapper> hiddenStacks, shownStacks;
     
     public FilteringResultImpl(List<EntryStack> hiddenStacks, List<EntryStack> shownStacks) {
-        this.hiddenStacks = new TreeSet<>(Comparator.comparing(EntryStack::hashIgnoreAmount));
-        this.shownStacks = new TreeSet<>(Comparator.comparing(EntryStack::hashIgnoreAmount));
-        this.hiddenStacks.addAll(hiddenStacks);
-        this.shownStacks.addAll(shownStacks);
+        this.hiddenStacks = Sets.newHashSetWithExpectedSize(hiddenStacks.size());
+        this.shownStacks = Sets.newHashSetWithExpectedSize(shownStacks.size());
+        hide(hiddenStacks);
+        show(shownStacks);
     }
     
     @Override
-    public Set<EntryStack> getHiddenStacks() {
+    public Set<AmountIgnoredEntryStackWrapper> getHiddenStacks() {
         return hiddenStacks;
     }
     
     @Override
-    public Set<EntryStack> getShownStacks() {
+    public Set<AmountIgnoredEntryStackWrapper> getShownStacks() {
         return shownStacks;
     }
 }

+ 3 - 3
src/main/java/me/shedaniel/rei/utils/CollectionUtils.java

@@ -142,7 +142,7 @@ public class CollectionUtils {
     }
     
     public static <T, R> List<R> map(List<T> list, Function<T, R> function) {
-        List<R> l = new ArrayList<>(list.size());
+        List<R> l = new ArrayList<>(list.size() + 1);
         for (T t : list) {
             l.add(function.apply(t));
         }
@@ -150,7 +150,7 @@ public class CollectionUtils {
     }
     
     public static <T, R> List<R> map(Collection<T> list, Function<T, R> function) {
-        List<R> l = new ArrayList<>(list.size());
+        List<R> l = new ArrayList<>(list.size() + 1);
         for (T t : list) {
             l.add(function.apply(t));
         }
@@ -158,7 +158,7 @@ public class CollectionUtils {
     }
     
     public static <T, R> List<R> map(T[] list, Function<T, R> function) {
-        List<R> l = new ArrayList<>(list.length);
+        List<R> l = new ArrayList<>(list.length + 1);
         for (T t : list) {
             l.add(function.apply(t));
         }