aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2024-04-18 14:31:36 +0900
committershedaniel <daniel@shedaniel.me>2024-04-25 22:33:28 +0900
commit7e6221c72d4a1383f682924b06bec397642ca9d5 (patch)
tree983615b49069edfed4be08a267a5d354b7101a1f
parenteb6ef8b13fe5842f51a0cd7fa8a646d0fb63ed72 (diff)
downloadRoughlyEnoughItems-7e6221c72d4a1383f682924b06bec397642ca9d5.tar.gz
RoughlyEnoughItems-7e6221c72d4a1383f682924b06bec397642ca9d5.tar.bz2
RoughlyEnoughItems-7e6221c72d4a1383f682924b06bec397642ca9d5.zip
Fix #1611
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java2
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCache.java88
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCacheImpl.java172
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java12
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java55
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolderImpl.java144
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java8
7 files changed, 320 insertions, 161 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java
index 2c6b31874..58e745e34 100644
--- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java
@@ -329,7 +329,7 @@ public class EntryWidget extends Slot implements DraggableStackProviderWidget {
Map<CategoryIdentifier<?>, Boolean> filteringQuickCraftCategories = ConfigObject.getInstance().getFilteringQuickCraftCategories();
boolean shouldFilterDisplays = ConfigObject.getInstance().shouldFilterDisplays();
- for (Display display : displaysHolder.getAllDisplaysByOutputs(getEntries())) {
+ for (Display display : displaysHolder.cache().getAllDisplaysByOutputs(getEntries())) {
CategoryIdentifier<?> categoryIdentifier = display.getCategoryIdentifier();
Optional<? extends CategoryRegistry.CategoryConfiguration<?>> configuration;
if ((configuration = categoryRegistry.tryGet(categoryIdentifier)).isEmpty()
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCache.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCache.java
new file mode 100644
index 000000000..b65aba97c
--- /dev/null
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCache.java
@@ -0,0 +1,88 @@
+/*
+ * This file is licensed under the MIT License, part of Roughly Enough Items.
+ * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.shedaniel.rei.impl.client.registry.display;
+
+import com.google.common.collect.Iterables;
+import me.shedaniel.rei.api.common.display.Display;
+import me.shedaniel.rei.api.common.entry.EntryStack;
+import me.shedaniel.rei.api.common.util.CollectionUtils;
+import me.shedaniel.rei.impl.client.view.ViewsImpl;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+public interface DisplayCache {
+ boolean doesCache();
+
+ boolean isCached(Display display);
+
+ void add(Display display);
+
+ boolean remove(Display display);
+
+ void endReload();
+
+ Set<Display> getDisplaysNotCached();
+
+ Set<Display> getDisplaysByInput(EntryStack<?> stack);
+
+ Set<Display> getDisplaysByOutput(EntryStack<?> stack);
+
+ default Iterable<Display> getAllDisplaysByInputs(List<EntryStack<?>> stacks) {
+ if (stacks.isEmpty()) return List.of();
+ Iterable<Display> inputCached = null;
+ if (doesCache()) {
+ for (EntryStack<?> stack : stacks) {
+ Set<Display> set = getDisplaysByInput(stack);
+ inputCached = inputCached == null ? set : Iterables.concat(inputCached, set);
+ }
+ if (stacks.size() > 1) inputCached = CollectionUtils.distinctReferenceOf(inputCached);
+ }
+ Collection<Display> notCached = this.getDisplaysNotCached();
+ if (notCached.isEmpty()) return inputCached == null ? List.of() : inputCached;
+ Iterable<Display> filteredNotCached = Iterables.filter(notCached, display ->
+ ViewsImpl.isUsagesFor(null, stacks, display));
+ if (inputCached == null) return filteredNotCached;
+ return Iterables.concat(inputCached, filteredNotCached);
+ }
+
+ default Iterable<Display> getAllDisplaysByOutputs(List<EntryStack<?>> stacks) {
+ if (stacks.isEmpty()) return List.of();
+ Iterable<Display> outputCached = null;
+ if (doesCache()) {
+ for (EntryStack<?> stack : stacks) {
+ Set<Display> set = getDisplaysByOutput(stack);
+ outputCached = outputCached == null ? set : Iterables.concat(outputCached, set);
+ }
+ if (stacks.size() > 1) outputCached = CollectionUtils.distinctReferenceOf(outputCached);
+ }
+ Collection<Display> notCached = this.getDisplaysNotCached();
+ if (notCached.isEmpty()) return outputCached == null ? List.of() : outputCached;
+ Iterable<Display> filteredNotCached = Iterables.filter(notCached, display ->
+ ViewsImpl.isRecipesFor(null, stacks, display));
+ if (outputCached == null) return filteredNotCached;
+ return Iterables.concat(outputCached, filteredNotCached);
+ }
+}
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCacheImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCacheImpl.java
new file mode 100644
index 000000000..80a73f82e
--- /dev/null
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayCacheImpl.java
@@ -0,0 +1,172 @@
+/*
+ * This file is licensed under the MIT License, part of Roughly Enough Items.
+ * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.shedaniel.rei.impl.client.registry.display;
+
+import com.google.common.base.Stopwatch;
+import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
+import it.unimi.dsi.fastutil.Hash;
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
+import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
+import me.shedaniel.rei.api.client.config.ConfigObject;
+import me.shedaniel.rei.api.common.display.Display;
+import me.shedaniel.rei.api.common.entry.EntryIngredient;
+import me.shedaniel.rei.api.common.entry.EntryStack;
+import me.shedaniel.rei.api.common.util.EntryStacks;
+import me.shedaniel.rei.impl.common.InternalLogger;
+
+import java.util.Collections;
+import java.util.Set;
+
+public class DisplayCacheImpl implements DisplayCache {
+ private final boolean cache;
+ private Set<Display> displaysCached = new ReferenceOpenHashSet<>();
+ private Set<Display> displaysNotCached = Collections.synchronizedSet(new ReferenceOpenHashSet<>());
+ private SetMultimap<EntryStack<?>, Display> displaysByInput;
+ private SetMultimap<EntryStack<?>, Display> displaysByOutput;
+ private boolean preprocessed = false;
+
+ public DisplayCacheImpl(boolean init) {
+ this.cache = init && ConfigObject.getInstance().doesCacheDisplayLookup();
+ this.displaysByInput = createSetMultimap();
+ this.displaysByOutput = createSetMultimap();
+ }
+
+ @Override
+ public boolean doesCache() {
+ return this.cache;
+ }
+
+ @Override
+ public boolean isCached(Display display) {
+ return this.cache && this.displaysCached.contains(display);
+ }
+
+ @Override
+ public void add(Display display) {
+ if (this.cache) {
+ if (!preprocessed) {
+ this.displaysNotCached.add(display);
+ } else {
+ this.process(display);
+ this.displaysCached.add(display);
+ }
+ } else {
+ this.displaysNotCached.add(display);
+ }
+ }
+
+ @Override
+ public boolean remove(Display display) {
+ if (this.cache) {
+ if (!preprocessed) {
+ return this.displaysNotCached.remove(display);
+ } else {
+ boolean removed = this.displaysCached.remove(display);
+ if (removed) {
+ for (EntryIngredient input : display.getInputEntries()) {
+ for (EntryStack<?> stack : input) {
+ this.displaysByInput.remove(stack, display);
+ }
+ }
+ for (EntryIngredient output : display.getOutputEntries()) {
+ for (EntryStack<?> stack : output) {
+ this.displaysByOutput.remove(stack, display);
+ }
+ }
+ }
+ return removed;
+ }
+ } else {
+ return this.displaysNotCached.remove(display);
+ }
+ }
+
+ @Override
+ public void endReload() {
+ if (this.cache) {
+ if (preprocessed) {
+ InternalLogger.getInstance().error("DisplayCache#endReload called after preprocessed!");
+ }
+
+ InternalLogger.getInstance().debug("Processing %d displays for optimal lookup performance...", this.displaysNotCached.size());
+ Stopwatch stopwatch = Stopwatch.createStarted();
+ this.displaysCached = new ReferenceOpenHashSet<>(this.displaysNotCached.size());
+ this.displaysByInput = createSetMultimap();
+ this.displaysByOutput = createSetMultimap();
+ for (Display display : this.displaysNotCached) {
+ this.process(display);
+ }
+ this.displaysCached.addAll(this.displaysNotCached);
+ this.displaysNotCached = Set.of();
+ this.preprocessed = true;
+ InternalLogger.getInstance().debug("Processed displays for optimal lookup performance in %s.", stopwatch.stop());
+ }
+ }
+
+ @Override
+ public Set<Display> getDisplaysNotCached() {
+ return this.displaysNotCached;
+ }
+
+ @Override
+ public Set<Display> getDisplaysByInput(EntryStack<?> stack) {
+ return this.displaysByInput.get(stack);
+ }
+
+ @Override
+ public Set<Display> getDisplaysByOutput(EntryStack<?> stack) {
+ return this.displaysByOutput.get(stack);
+ }
+
+ private void process(Display display) {
+ for (EntryIngredient input : display.getInputEntries()) {
+ for (EntryStack<?> stack : input) {
+ this.displaysByInput.put(stack, display);
+ }
+ }
+ for (EntryIngredient output : display.getOutputEntries()) {
+ for (EntryStack<?> stack : output) {
+ this.displaysByOutput.put(stack, display);
+ }
+ }
+ }
+
+ private static SetMultimap<EntryStack<?>, Display> createSetMultimap() {
+ return Multimaps.newSetMultimap(
+ new Object2ObjectOpenCustomHashMap<>(5000, new Hash.Strategy<>() {
+ @Override
+ public int hashCode(EntryStack<?> stack) {
+ return Long.hashCode(EntryStacks.hashFuzzy(stack));
+ }
+
+ @Override
+ public boolean equals(EntryStack<?> o1, EntryStack<?> o2) {
+ return EntryStacks.equalsFuzzy(o1, o2);
+ }
+ }),
+ ReferenceOpenHashSet::new
+ );
+ }
+}
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java
index 277ba64d7..d0d36130c 100644
--- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java
@@ -86,7 +86,7 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi
@Override
public Map<CategoryIdentifier<?>, List<Display>> getAll() {
- return this.displaysHolder.get();
+ return this.displaysHolder.getUnmodifiable();
}
@Override
@@ -201,8 +201,16 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi
}
}
+ List<Display> failedDisplays = new ArrayList<>();
for (List<Display> displays : getAll().values()) {
- displays.removeIf(display -> !DisplayValidator.validate(display));
+ for (Display display : displays) {
+ if (!DisplayValidator.validate(display)) {
+ failedDisplays.add(display);
+ }
+ }
+ }
+ for (Display display : failedDisplays) {
+ this.displaysHolder.remove(display);
}
this.displaysHolder.endReload();
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java
index 2480053d1..37ff40d50 100644
--- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java
@@ -23,27 +23,24 @@
package me.shedaniel.rei.impl.client.registry.display;
-import com.google.common.collect.Iterables;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.Display;
-import me.shedaniel.rei.api.common.entry.EntryStack;
-import me.shedaniel.rei.api.common.util.CollectionUtils;
-import me.shedaniel.rei.impl.client.view.ViewsImpl;
import org.jetbrains.annotations.Nullable;
-import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface DisplaysHolder {
- boolean doesCache();
+ DisplayCache cache();
void add(Display display, @Nullable Object origin);
+ boolean remove(Display display);
+
int size();
- Map<CategoryIdentifier<?>, List<Display>> get();
+ Map<CategoryIdentifier<?>, List<Display>> getUnmodifiable();
@Nullable
Object getDisplayOrigin(Display display);
@@ -51,48 +48,4 @@ public interface DisplaysHolder {
void endReload();
Set<Display> getDisplaysByKey(DisplayKey key);
-
- boolean isCached(Display display);
-
- Set<Display> getDisplaysNotCached();
-
- Set<Display> getDisplaysByInput(EntryStack<?> stack);
-
- Set<Display> getDisplaysByOutput(EntryStack<?> stack);
-
- default Iterable<Display> getAllDisplaysByInputs(List<EntryStack<?>> stacks) {
- if (stacks.isEmpty()) return List.of();
- Iterable<Display> inputCached = null;
- if (doesCache()) {
- for (EntryStack<?> stack : stacks) {
- Set<Display> set = getDisplaysByInput(stack);
- inputCached = inputCached == null ? set : Iterables.concat(inputCached, set);
- }
- if (stacks.size() > 1) inputCached = CollectionUtils.distinctReferenceOf(inputCached);
- }
- Collection<Display> notCached = this.getDisplaysNotCached();
- if (notCached.isEmpty()) return inputCached == null ? List.of() : inputCached;
- Iterable<Display> filteredNotCached = Iterables.filter(notCached, display ->
- ViewsImpl.isUsagesFor(null, stacks, display));
- if (inputCached == null) return filteredNotCached;
- return Iterables.concat(inputCached, filteredNotCached);
- }
-
- default Iterable<Display> getAllDisplaysByOutputs(List<EntryStack<?>> stacks) {
- if (stacks.isEmpty()) return List.of();
- Iterable<Display> outputCached = null;
- if (doesCache()) {
- for (EntryStack<?> stack : stacks) {
- Set<Display> set = getDisplaysByOutput(stack);
- outputCached = outputCached == null ? set : Iterables.concat(outputCached, set);
- }
- if (stacks.size() > 1) outputCached = CollectionUtils.distinctReferenceOf(outputCached);
- }
- Collection<Display> notCached = this.getDisplaysNotCached();
- if (notCached.isEmpty()) return outputCached == null ? List.of() : outputCached;
- Iterable<Display> filteredNotCached = Iterables.filter(notCached, display ->
- ViewsImpl.isRecipesFor(null, stacks, display));
- if (outputCached == null) return filteredNotCached;
- return Iterables.concat(outputCached, filteredNotCached);
- }
}
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolderImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolderImpl.java
index bf09618c7..09a4ff1a1 100644
--- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolderImpl.java
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolderImpl.java
@@ -23,21 +23,13 @@
package me.shedaniel.rei.impl.client.registry.display;
-import com.google.common.base.Stopwatch;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
-import it.unimi.dsi.fastutil.Hash;
-import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
-import me.shedaniel.rei.api.client.config.ConfigObject;
import me.shedaniel.rei.api.client.registry.category.CategoryRegistry;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.Display;
-import me.shedaniel.rei.api.common.entry.EntryIngredient;
-import me.shedaniel.rei.api.common.entry.EntryStack;
-import me.shedaniel.rei.api.common.util.EntryStacks;
import me.shedaniel.rei.impl.client.gui.widget.favorites.history.DisplayHistoryManager;
-import me.shedaniel.rei.impl.common.InternalLogger;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.Nullable;
@@ -46,33 +38,26 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class DisplaysHolderImpl implements DisplaysHolder {
- private final boolean cache;
+ private final DisplayCache cache;
private final SetMultimap<DisplayKey, Display> displaysByKey = Multimaps.newSetMultimap(new IdentityHashMap<>(), ReferenceOpenHashSet::new);
private final Map<CategoryIdentifier<?>, DisplaysList> displays = new ConcurrentHashMap<>();
- private final Map<CategoryIdentifier<?>, List<Display>> unmodifiableDisplays;
- private final WeakHashMap<Display, Object> displaysBase = new WeakHashMap<>();
- private Set<Display> displaysCached = new ReferenceOpenHashSet<>();
- private Set<Display> displaysNotCached = Collections.synchronizedSet(new ReferenceOpenHashSet<>());
- private boolean preprocessed = false;
- private SetMultimap<EntryStack<?>, Display> displaysByInput;
- private SetMultimap<EntryStack<?>, Display> displaysByOutput;
+ private final Map<CategoryIdentifier<?>, List<Display>> unmodifiableDisplays = new RemappingMap<>(
+ Collections.unmodifiableMap(displays), list -> {
+ if (list == null) {
+ return null;
+ } else {
+ return ((DisplaysList) list).synchronizedList;
+ }
+ }, key -> CategoryRegistry.getInstance().tryGet(key).isPresent());
+ private final WeakHashMap<Display, Object> originsMap = new WeakHashMap<>();
private final MutableInt displayCount = new MutableInt(0);
public DisplaysHolderImpl(boolean init) {
- this.cache = init && ConfigObject.getInstance().doesCacheDisplayLookup();
- this.unmodifiableDisplays = new RemappingMap<>(Collections.unmodifiableMap(displays), list -> {
- if (list == null) {
- return null;
- } else {
- return ((DisplaysList) list).synchronizedList;
- }
- }, key -> CategoryRegistry.getInstance().tryGet(key).isPresent());
- this.displaysByInput = createSetMultimap();
- this.displaysByOutput = createSetMultimap();
+ this.cache = new DisplayCacheImpl(init);
}
@Override
- public boolean doesCache() {
+ public DisplayCache cache() {
return this.cache;
}
@@ -86,93 +71,63 @@ public class DisplaysHolderImpl implements DisplaysHolder {
}
this.displayCount.increment();
if (origin != null) {
- synchronized (this.displaysBase) {
- this.displaysBase.put(display, origin);
+ synchronized (this.originsMap) {
+ this.originsMap.put(display, origin);
}
}
- if (this.cache) {
- if (!preprocessed) {
- this.displaysNotCached.add(display);
- } else {
- this.process(display);
- this.displaysCached.add(display);
- }
- } else {
- this.displaysNotCached.add(display);
- }
+ this.cache.add(display);
}
@Override
- public int size() {
- return this.displayCount.intValue();
- }
-
- @Override
- public Map<CategoryIdentifier<?>, List<Display>> get() {
- return this.unmodifiableDisplays;
- }
-
- @Override
- public void endReload() {
- if (this.cache) {
- InternalLogger.getInstance().debug("Processing %d displays for optimal lookup performance...", this.size());
- Stopwatch stopwatch = Stopwatch.createStarted();
- this.displaysCached = new ReferenceOpenHashSet<>(this.size());
- this.displaysByInput = createSetMultimap();
- this.displaysByOutput = createSetMultimap();
- for (Display display : this.displaysNotCached) {
- this.process(display);
+ public boolean remove(Display display) {
+ if (this.displays.get(display.getCategoryIdentifier()).remove(display)) {
+ removeFallout(display);
+ if (this.displays.get(display.getCategoryIdentifier()).isEmpty()) {
+ this.displays.remove(display.getCategoryIdentifier());
}
- this.displaysCached.addAll(this.displaysNotCached);
- this.displaysNotCached = Set.of();
- this.preprocessed = true;
- InternalLogger.getInstance().debug("Processed displays for optimal lookup performance in %s.", stopwatch.stop());
+ return true;
}
+
+ return false;
}
- private void process(Display display) {
- for (EntryIngredient input : display.getInputEntries()) {
- for (EntryStack<?> stack : input) {
- this.displaysByInput.put(stack, display);
- }
+ private void removeFallout(Display display) {
+ Optional<ResourceLocation> location = display.getDisplayLocation();
+ if (location.isPresent()) {
+ this.displaysByKey.remove(DisplayKey.create(display.getCategoryIdentifier(), location.get()), display);
}
- for (EntryIngredient output : display.getOutputEntries()) {
- for (EntryStack<?> stack : output) {
- this.displaysByOutput.put(stack, display);
- }
+ this.displayCount.decrement();
+ synchronized (this.originsMap) {
+ this.originsMap.remove(display);
}
+ this.cache.remove(display);
}
@Override
- public Set<Display> getDisplaysByKey(DisplayKey key) {
- return this.displaysByKey.get(key);
- }
-
- @Override
- public boolean isCached(Display display) {
- return this.cache && this.displaysCached.contains(display);
+ public int size() {
+ return this.displayCount.intValue();
}
@Override
- public Set<Display> getDisplaysNotCached() {
- return this.displaysNotCached;
+ public Map<CategoryIdentifier<?>, List<Display>> getUnmodifiable() {
+ return this.unmodifiableDisplays;
}
@Override
- public Set<Display> getDisplaysByInput(EntryStack<?> stack) {
- return this.displaysByInput.get(stack);
+ public void endReload() {
+ this.cache.endReload();
}
@Override
- public Set<Display> getDisplaysByOutput(EntryStack<?> stack) {
- return this.displaysByOutput.get(stack);
+ public Set<Display> getDisplaysByKey(DisplayKey key) {
+ return this.displaysByKey.get(key);
}
@Override
@Nullable
public Object getDisplayOrigin(Display display) {
- synchronized (this.displaysBase) {
- Object origin = this.displaysBase.get(display);
+ synchronized (this.originsMap) {
+ Object origin = this.originsMap.get(display);
if (origin != null) {
return origin;
@@ -190,21 +145,4 @@ public class DisplaysHolderImpl implements DisplaysHolder {
this.synchronizedList = Collections.synchronizedList(unmodifiableList);
}
}
-
- private SetMultimap<EntryStack<?>, Display> createSetMultimap() {
- return Multimaps.newSetMultimap(
- new Object2ObjectOpenCustomHashMap<>(Math.max(10000, this.size() * 5 / 2), new Hash.Strategy<>() {
- @Override
- public int hashCode(EntryStack<?> stack) {
- return Long.hashCode(EntryStacks.hashFuzzy(stack));
- }
-
- @Override
- public boolean equals(EntryStack<?> o1, EntryStack<?> o2) {
- return EntryStacks.equalsFuzzy(o1, o2);
- }
- }),
- ReferenceOpenHashSet::new
- );
- }
}
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java
index 34bff7be2..79d27d9fd 100644
--- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java
+++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java
@@ -286,9 +286,9 @@ public class ViewsImpl implements Views {
}
public static boolean isRecipesFor(@Nullable DisplaysHolder displaysHolder, List<EntryStack<?>> stacks, Display display) {
- if (displaysHolder != null && displaysHolder.isCached(display)) {
+ if (displaysHolder != null && displaysHolder.cache().isCached(display)) {
for (EntryStack<?> recipesFor : stacks) {
- return displaysHolder.getDisplaysByOutput(recipesFor).contains(display);
+ return displaysHolder.cache().getDisplaysByOutput(recipesFor).contains(display);
}
}
@@ -296,9 +296,9 @@ public class ViewsImpl implements Views {
}
public static boolean isUsagesFor(@Nullable DisplaysHolder displaysHolder, List<EntryStack<?>> stacks, Display display) {
- if (displaysHolder != null && displaysHolder.isCached(display)) {
+ if (displaysHolder != null && displaysHolder.cache().isCached(display)) {
for (EntryStack<?> recipesFor : stacks) {
- return displaysHolder.getDisplaysByInput(recipesFor).contains(display);
+ return displaysHolder.cache().getDisplaysByInput(recipesFor).contains(display);
}
}