From 30154cef1a3991363c7094a451e5c11494cd4c9c Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Tue, 26 Nov 2024 10:13:05 +0100 Subject: [PATCH] WIP: Optimize client performance of large networks with many diffs, #139 --- ...alStorageTabIngredientComponentClient.java | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java b/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java index eb94b4e44..868692f01 100644 --- a/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java +++ b/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java @@ -34,7 +34,7 @@ import org.cyclops.cyclopscore.helper.StringHelpers; import org.cyclops.cyclopscore.ingredient.collection.IIngredientCollapsedCollectionMutable; import org.cyclops.cyclopscore.ingredient.collection.IngredientArrayList; -import org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionPrototypeMap; +import org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionHelpers; import org.cyclops.cyclopscore.ingredient.collection.diff.IngredientCollectionDiff; import org.cyclops.cyclopscore.ingredient.collection.diff.IngredientCollectionDiffHelpers; import org.cyclops.integrateddynamics.api.ingredient.IIngredientComponentStorageObservable; @@ -98,7 +98,7 @@ public class TerminalStorageTabIngredientComponentClient protected final ContainerTerminalStorageBase container; private final List> buttons; - private final Int2ObjectMap>> ingredientsViews; + private final Int2ObjectMap> ingredientsUnsortedViews; private final Int2ObjectMap>> filteredIngredientsViews; private final Int2ObjectMap>> craftingOptions; @@ -142,7 +142,7 @@ public TerminalStorageTabIngredientComponentClient(ContainerTerminalStorageBase NeoForge.EVENT_BUS.post(event); this.buttons = event.getButtons(); - this.ingredientsViews = new Int2ObjectOpenHashMap<>(); + this.ingredientsUnsortedViews = new Int2ObjectOpenHashMap<>(); this.filteredIngredientsViews = new Int2ObjectOpenHashMap<>(); this.craftingOptions = new Int2ObjectOpenHashMap<>(); @@ -238,11 +238,11 @@ public void setInstanceFilter(int channel, String filter) { container.getGuiState().setSearch(getTabSettingsName().toString(), channel, filter.toLowerCase(Locale.ENGLISH)); } - public List> getRawUnfilteredIngredientsView(int channel) { - List> ingredientsView = ingredientsViews.get(channel); + public IIngredientCollapsedCollectionMutable getRawUnfilteredIngredientsView(int channel) { + IIngredientCollapsedCollectionMutable ingredientsView = ingredientsUnsortedViews.get(channel); if (ingredientsView == null) { - ingredientsView = Lists.newArrayList(); - ingredientsViews.put(channel, ingredientsView); + ingredientsView = IngredientCollectionHelpers.createCollapsedCollection(getIngredientComponent()); + ingredientsUnsortedViews.put(channel, ingredientsView); } return ingredientsView; } @@ -252,21 +252,24 @@ public Collection> getCraftingOptions(in return craftingOptions.get(channel); } - public List> getUnfilteredIngredientsView(int channel) { + public List> getUnfilteredIngredientsView(int channel) { // TODO: rename, and don't use this one anymore in ITerms-Compat + // Convert raw ingredients view to list + List> enrichedIngredients = Lists.newArrayList(); + for (T persistedIngredient : getRawUnfilteredIngredientsView(channel)) { + enrichedIngredients.add(new InstanceWithMetadata<>(persistedIngredient, null)); + } + + // Add all crafting option outputs Collection> craftingOptions = getCraftingOptions(channel); - if (craftingOptions == null) { - return getRawUnfilteredIngredientsView(channel); - } else { - List> enrichedIngredients = Lists.newArrayList(); - enrichedIngredients.addAll(getRawUnfilteredIngredientsView(channel)); - // Add all crafting option outputs + if (craftingOptions != null) { for (HandlerWrappedTerminalCraftingOption craftingOption : craftingOptions) { for (T output : getUniqueCraftingOptionOutputs(craftingOption.getCraftingOption())) { enrichedIngredients.add(new InstanceWithMetadata<>(output, craftingOption)); } } - return enrichedIngredients; } + + return enrichedIngredients; } protected Collection getUniqueCraftingOptionOutputs(ITerminalCraftingOption craftingOption) { @@ -399,21 +402,12 @@ public synchronized void onChange(int channel, IIngredientComponentStorageObserv totalQuantities.put(channel, newQuantity); // Apply diff - List> rawPersistedIngredients = getRawUnfilteredIngredientsView(channel); - IIngredientCollapsedCollectionMutable persistedIngredients = new IngredientCollectionPrototypeMap<>(ingredientComponent); - rawPersistedIngredients - .stream() - .map(InstanceWithMetadata::getInstance) - .forEach(persistedIngredients::add); + IIngredientCollapsedCollectionMutable rawPersistedIngredients = getRawUnfilteredIngredientsView(channel); IngredientCollectionDiff diff = new IngredientCollectionDiff<>( changeType == IIngredientComponentStorageObservable.Change.ADDITION ? ingredients : null, changeType == IIngredientComponentStorageObservable.Change.DELETION ? ingredients : null, false); - IngredientCollectionDiffHelpers.applyDiff(ingredientComponent, diff, persistedIngredients); - rawPersistedIngredients.clear(); - for (T persistedIngredient : persistedIngredients) { - rawPersistedIngredients.add(new InstanceWithMetadata<>(persistedIngredient, null)); - } + IngredientCollectionDiffHelpers.applyDiff(ingredientComponent, diff, rawPersistedIngredients); // Persist changes resetFilteredIngredientsViews(channel);