Explorar o código

Improve memory usage

Signed-off-by: shedaniel <daniel@shedaniel.me>
shedaniel %!s(int64=4) %!d(string=hai) anos
pai
achega
2307ef0bff

+ 11 - 6
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/AbstractEntryStack.java

@@ -23,7 +23,7 @@
 
 package me.shedaniel.rei.impl;
 
-import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
+import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
 import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
 import me.shedaniel.rei.api.EntryStack;
 import net.minecraft.client.gui.GuiComponent;
@@ -33,33 +33,38 @@ import java.util.Map;
 
 @ApiStatus.Internal
 public abstract class AbstractEntryStack extends GuiComponent implements EntryStack {
-    private Reference2ObjectMap<Settings<?>, Object> settings = new Reference2ObjectOpenHashMap<>();
+    private static final Map<Settings<?>, Object> EMPTY_SETTINGS = Reference2ObjectMaps.emptyMap();
+    private Map<Settings<?>, Object> settings = null;
     
     @Override
     public <T> EntryStack setting(Settings<T> settings, T value) {
+        if (this.settings == null)
+            this.settings = new Reference2ObjectOpenHashMap<>(4);
         this.settings.put(settings, value);
         return this;
     }
     
     @Override
     public <T> EntryStack removeSetting(Settings<T> settings) {
-        this.settings.remove(settings);
+        if (this.settings != null && this.settings.remove(settings) != null && this.settings.isEmpty()) {
+            this.settings = null;
+        }
         return this;
     }
     
     @Override
     public EntryStack clearSettings() {
-        this.settings.clear();
+        this.settings = null;
         return this;
     }
     
     protected Map<Settings<?>, Object> getSettings() {
-        return settings;
+        return this.settings == null ? EMPTY_SETTINGS : this.settings;
     }
     
     @Override
     public <T> T get(Settings<T> settings) {
-        Object o = this.settings.get(settings);
+        Object o = this.settings == null ? null : this.settings.get(settings);
         if (o == null)
             return settings.getDefaultValue();
         return (T) o;

+ 8 - 4
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/EntryRegistryImpl.java

@@ -36,6 +36,7 @@ import me.shedaniel.rei.utils.CollectionUtils;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.core.NonNullList;
+import net.minecraft.core.Registry;
 import net.minecraft.world.item.Item;
 import net.minecraft.world.item.ItemStack;
 import org.jetbrains.annotations.ApiStatus;
@@ -53,7 +54,8 @@ public class EntryRegistryImpl implements EntryRegistry {
     
     private final List<EntryStack> preFilteredList = Lists.newCopyOnWriteArrayList();
     private final List<EntryStack> entries = Lists.newCopyOnWriteArrayList();
-    private final List<AmountIgnoredEntryStackWrapper> reloadingRegistry = Lists.newArrayList();
+    @Nullable
+    private List<AmountIgnoredEntryStackWrapper> reloadingRegistry;
     private boolean reloading;
     
     private static EntryStack findFirstOrNullEqualsEntryIgnoreAmount(Collection<EntryStack> list, EntryStack obj) {
@@ -70,7 +72,7 @@ public class EntryRegistryImpl implements EntryRegistry {
         reloadingRegistry.removeIf(AmountIgnoredEntryStackWrapper::isEmpty);
         entries.clear();
         entries.addAll(CollectionUtils.map(reloadingRegistry, AmountIgnoredEntryStackWrapper::unwrap));
-        reloadingRegistry.clear();
+        reloadingRegistry = null;
     }
     
     @Override
@@ -121,9 +123,11 @@ public class EntryRegistryImpl implements EntryRegistry {
         return (Predicate<T>) target.negate();
     }
     
-    public void reset() {
+    public void resetToReloadStart() {
         entries.clear();
-        reloadingRegistry.clear();
+        if (reloadingRegistry != null)
+            reloadingRegistry.clear();
+        reloadingRegistry = Lists.newArrayListWithCapacity(Registry.ITEM.keySet().size() + 100);
         preFilteredList.clear();
         reloading = true;
     }

+ 1 - 1
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java

@@ -374,7 +374,7 @@ public class RecipeHelperImpl implements RecipeHelper {
         plugins.sort(Comparator.comparingInt(REIPluginEntry::getPriority).reversed());
         RoughlyEnoughItemsCore.LOGGER.info("Reloading REI, registered %d plugins: %s", plugins.size(), plugins.stream().map(REIPluginEntry::getPluginIdentifier).map(ResourceLocation::toString).collect(Collectors.joining(", ")));
         Collections.reverse(plugins);
-        entryRegistry.reset();
+        entryRegistry.resetToReloadStart();
         List<REIPluginV0> reiPluginV0s = new ArrayList<>();
         endSection(sectionData);
         for (REIPluginEntry plugin : plugins) {