소스 검색

Performance improvements to search filtering thanks to multithreading.
Fix the drag state issue when deleting items.

Signed-off-by: shedaniel <daniel@shedaniel.me>

shedaniel 5 년 전
부모
커밋
17d03edde6

+ 1 - 1
gradle.properties

@@ -1,5 +1,5 @@
 org.gradle.jvmargs=-Xmx3G
-mod_version=4.9.2
+mod_version=4.9.3
 supported_version=1.16.x
 minecraft_version=1.16.1
 yarn_version=1.16.1+build.4+legacy.20w09a+build.8

+ 1 - 4
src/main/java/me/shedaniel/rei/api/EntryRegistry.java

@@ -35,7 +35,6 @@ import org.jetbrains.annotations.Nullable;
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -91,9 +90,7 @@ public interface EntryRegistry {
      * @param afterEntry the stack to put after
      * @param stack      the stack to register
      */
-    default void registerEntryAfter(@Nullable EntryStack afterEntry, @NotNull EntryStack stack) {
-        registerEntriesAfter(afterEntry, Collections.singletonList(stack));
-    }
+    void registerEntryAfter(@Nullable EntryStack afterEntry, @NotNull EntryStack stack);
     
     /**
      * Registers an new stack to the entry list

+ 4 - 0
src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java

@@ -42,6 +42,7 @@ import net.fabricmc.loader.api.ModContainer;
 import net.fabricmc.loader.api.metadata.ModMetadata;
 import net.minecraft.client.MinecraftClient;
 import net.minecraft.client.gui.screen.Screen;
+import net.minecraft.client.gui.screen.ingame.ContainerScreen;
 import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
 import net.minecraft.client.util.NarratorManager;
 import net.minecraft.entity.player.PlayerInventory;
@@ -150,6 +151,9 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer {
             return;
         }
         ClientSidePacketRegistry.INSTANCE.sendToServer(RoughlyEnoughItemsNetwork.DELETE_ITEMS_PACKET, new PacketByteBuf(Unpooled.buffer()));
+        if (MinecraftClient.getInstance().currentScreen instanceof ContainerScreen) {
+            ((ContainerScreen<?>) MinecraftClient.getInstance().currentScreen).isCursorDragging = false;
+        }
     }
     
     @Override

+ 22 - 9
src/main/java/me/shedaniel/rei/impl/EntryRegistryImpl.java

@@ -91,12 +91,10 @@ public class EntryRegistryImpl implements EntryRegistry {
             context.handleResult(rules.get(i).processFilteredStacks(context));
         }
         
-        Set<AmountIgnoredEntryStackWrapper> set = Sets.newLinkedHashSet();
-        set.addAll(CollectionUtils.map(entries, AmountIgnoredEntryStackWrapper::new));
-        Collection<EntryStack> hiddenStacks = context.getHiddenStacks();
-        set.removeAll(CollectionUtils.map(hiddenStacks, AmountIgnoredEntryStackWrapper::new));
+        Set<AmountIgnoredEntryStackWrapper> set = CollectionUtils.mapParallel(entries, AmountIgnoredEntryStackWrapper::new, Sets::newLinkedHashSet);
+        set.removeAll(CollectionUtils.mapParallel(context.getHiddenStacks(), AmountIgnoredEntryStackWrapper::new, Sets::newHashSet));
         preFilteredList.clear();
-        preFilteredList.addAll(CollectionUtils.map(set, AmountIgnoredEntryStackWrapper::unwrap));
+        preFilteredList.addAll(CollectionUtils.mapParallel(set, AmountIgnoredEntryStackWrapper::unwrap));
         
         long time = System.currentTimeMillis() - started;
         RoughlyEnoughItemsCore.LOGGER.info("Refiltered %d entries with %d rules in %dms.", entries.size() - preFilteredList.size(), rules.size(), time);
@@ -129,15 +127,30 @@ public class EntryRegistryImpl implements EntryRegistry {
     }
     
     @Override
-    public void registerEntriesAfter(@Nullable EntryStack afterStack, @NotNull Collection<@NotNull ? extends EntryStack> stacks) {
+    public void registerEntryAfter(@Nullable EntryStack afterEntry, @NotNull EntryStack stack) {
         if (reloading) {
-            int index = afterStack != null ? reloadingRegistry.lastIndexOf(new AmountIgnoredEntryStackWrapper(afterStack)) : -1;
+            int index = afterEntry != null ? reloadingRegistry.lastIndexOf(new AmountIgnoredEntryStackWrapper(afterEntry)) : -1;
+            if (index >= 0) {
+                reloadingRegistry.add(index, new AmountIgnoredEntryStackWrapper(stack));
+            } else reloadingRegistry.add(new AmountIgnoredEntryStackWrapper(stack));
+        } else {
+            if (afterEntry != null) {
+                int index = entries.lastIndexOf(afterEntry);
+                entries.add(index, stack);
+            } else entries.add(stack);
+        }
+    }
+    
+    @Override
+    public void registerEntriesAfter(@Nullable EntryStack afterEntry, @NotNull Collection<@NotNull ? extends EntryStack> stacks) {
+        if (reloading) {
+            int index = afterEntry != null ? reloadingRegistry.lastIndexOf(new AmountIgnoredEntryStackWrapper(afterEntry)) : -1;
             if (index >= 0) {
                 reloadingRegistry.addAll(index, CollectionUtils.map(stacks, AmountIgnoredEntryStackWrapper::new));
             } else reloadingRegistry.addAll(CollectionUtils.map(stacks, AmountIgnoredEntryStackWrapper::new));
         } else {
-            if (afterStack != null) {
-                int index = entries.lastIndexOf(afterStack);
+            if (afterEntry != null) {
+                int index = entries.lastIndexOf(afterEntry);
                 entries.addAll(index, stacks);
             } else entries.addAll(stacks);
         }

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

@@ -52,7 +52,7 @@ public class FilteringContextImpl implements FilteringContext {
         for (FilteringContextType type : FilteringContextType.values()) {
             this.stacks.computeIfAbsent(type, t -> Sets.newHashSet());
         }
-        this.stacks.get(FilteringContextType.DEFAULT).addAll(CollectionUtils.map(allStacks, AmountIgnoredEntryStackWrapper::new));
+        this.stacks.get(FilteringContextType.DEFAULT).addAll(CollectionUtils.mapParallel(allStacks, AmountIgnoredEntryStackWrapper::new));
         fillCache();
     }
     

+ 5 - 11
src/main/java/me/shedaniel/rei/impl/filtering/rules/ManualFilteringRule.java

@@ -23,8 +23,8 @@
 
 package me.shedaniel.rei.impl.filtering.rules;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
+import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
+import it.unimi.dsi.fastutil.ints.IntSet;
 import me.shedaniel.rei.api.ConfigObject;
 import me.shedaniel.rei.api.EntryStack;
 import me.shedaniel.rei.impl.filtering.AbstractFilteringRule;
@@ -37,8 +37,7 @@ import net.minecraft.text.TranslatableText;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collection;
-import java.util.List;
-import java.util.Set;
+import java.util.stream.Collectors;
 
 public class ManualFilteringRule extends AbstractFilteringRule<ManualFilteringRule> {
     @Override
@@ -60,13 +59,8 @@ public class ManualFilteringRule extends AbstractFilteringRule<ManualFilteringRu
     }
     
     private void processList(Collection<EntryStack> stacks, FilteringResult result) {
-        Set<Integer> filteredStacks = Sets.newHashSet(CollectionUtils.map(ConfigObject.getInstance().getFilteredStacks(), EntryStack::hashIgnoreAmount));
-        List<EntryStack> filtered = Lists.newArrayList();
-        for (EntryStack stack : stacks) {
-            if (filteredStacks.contains(stack.hashIgnoreAmount()))
-                filtered.add(stack);
-        }
-        result.hide(filtered);
+        IntSet filteredStacks = CollectionUtils.mapParallel(ConfigObject.getInstance().getFilteredStacks(), EntryStack::hashIgnoreAmount, IntOpenHashSet::new);
+        result.hide(stacks.parallelStream().filter(stack -> filteredStacks.contains(stack.hashIgnoreAmount())).collect(Collectors.toList()));
     }
     
     @Override

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

@@ -32,6 +32,8 @@ import net.fabricmc.api.Environment;
 import java.util.*;
 import java.util.function.Function;
 import java.util.function.Predicate;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
 
 public class CollectionUtils {
     public static <A, B> List<B> getOrPutEmptyList(Map<A, List<B>> map, A key) {
@@ -157,6 +159,14 @@ public class CollectionUtils {
         return l;
     }
     
+    public static <T, R> List<R> mapParallel(Collection<T> list, Function<T, R> function) {
+        return list.parallelStream().map(function).collect(Collectors.toList());
+    }
+    
+    public static <T, R, C extends Collection<R>> C mapParallel(Collection<T> list, Function<T, R> function, Supplier<C> supplier) {
+        return list.parallelStream().map(function).collect(Collectors.toCollection(supplier));
+    }
+    
     public static <T, R> List<R> map(T[] list, Function<T, R> function) {
         List<R> l = new ArrayList<>(list.length + 1);
         for (T t : list) {