Skip to content

Commit

Permalink
WIP: Optimize client performance of large networks with many diffs, #139
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 26, 2024
1 parent 8e08203 commit 30154ce
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,7 +98,7 @@ public class TerminalStorageTabIngredientComponentClient<T, M>
protected final ContainerTerminalStorageBase container;
private final List<ITerminalButton<?, ?, ?>> buttons;

private final Int2ObjectMap<List<InstanceWithMetadata<T>>> ingredientsViews;
private final Int2ObjectMap<IIngredientCollapsedCollectionMutable<T, M>> ingredientsUnsortedViews;
private final Int2ObjectMap<List<InstanceWithMetadata<T>>> filteredIngredientsViews;
private final Int2ObjectMap<Collection<HandlerWrappedTerminalCraftingOption<T>>> craftingOptions;

Expand Down Expand Up @@ -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<>();

Expand Down Expand Up @@ -238,11 +238,11 @@ public void setInstanceFilter(int channel, String filter) {
container.getGuiState().setSearch(getTabSettingsName().toString(), channel, filter.toLowerCase(Locale.ENGLISH));
}

public List<InstanceWithMetadata<T>> getRawUnfilteredIngredientsView(int channel) {
List<InstanceWithMetadata<T>> ingredientsView = ingredientsViews.get(channel);
public IIngredientCollapsedCollectionMutable<T, M> getRawUnfilteredIngredientsView(int channel) {
IIngredientCollapsedCollectionMutable<T, M> ingredientsView = ingredientsUnsortedViews.get(channel);
if (ingredientsView == null) {
ingredientsView = Lists.newArrayList();
ingredientsViews.put(channel, ingredientsView);
ingredientsView = IngredientCollectionHelpers.createCollapsedCollection(getIngredientComponent());
ingredientsUnsortedViews.put(channel, ingredientsView);
}
return ingredientsView;
}
Expand All @@ -252,21 +252,24 @@ public Collection<HandlerWrappedTerminalCraftingOption<T>> getCraftingOptions(in
return craftingOptions.get(channel);
}

public List<InstanceWithMetadata<T>> getUnfilteredIngredientsView(int channel) {
public List<InstanceWithMetadata<T>> getUnfilteredIngredientsView(int channel) { // TODO: rename, and don't use this one anymore in ITerms-Compat
// Convert raw ingredients view to list
List<InstanceWithMetadata<T>> enrichedIngredients = Lists.newArrayList();
for (T persistedIngredient : getRawUnfilteredIngredientsView(channel)) {
enrichedIngredients.add(new InstanceWithMetadata<>(persistedIngredient, null));
}

// Add all crafting option outputs
Collection<HandlerWrappedTerminalCraftingOption<T>> craftingOptions = getCraftingOptions(channel);
if (craftingOptions == null) {
return getRawUnfilteredIngredientsView(channel);
} else {
List<InstanceWithMetadata<T>> enrichedIngredients = Lists.newArrayList();
enrichedIngredients.addAll(getRawUnfilteredIngredientsView(channel));
// Add all crafting option outputs
if (craftingOptions != null) {
for (HandlerWrappedTerminalCraftingOption<T> craftingOption : craftingOptions) {
for (T output : getUniqueCraftingOptionOutputs(craftingOption.getCraftingOption())) {
enrichedIngredients.add(new InstanceWithMetadata<>(output, craftingOption));
}
}
return enrichedIngredients;
}

return enrichedIngredients;
}

protected Collection<T> getUniqueCraftingOptionOutputs(ITerminalCraftingOption<T> craftingOption) {
Expand Down Expand Up @@ -399,21 +402,12 @@ public synchronized void onChange(int channel, IIngredientComponentStorageObserv
totalQuantities.put(channel, newQuantity);

// Apply diff
List<InstanceWithMetadata<T>> rawPersistedIngredients = getRawUnfilteredIngredientsView(channel);
IIngredientCollapsedCollectionMutable<T, M> persistedIngredients = new IngredientCollectionPrototypeMap<>(ingredientComponent);
rawPersistedIngredients
.stream()
.map(InstanceWithMetadata::getInstance)
.forEach(persistedIngredients::add);
IIngredientCollapsedCollectionMutable<T, M> rawPersistedIngredients = getRawUnfilteredIngredientsView(channel);
IngredientCollectionDiff<T, M> 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);
Expand Down

0 comments on commit 30154ce

Please sign in to comment.