Optimize item count lookups in crafting GUI#320
Open
Grayjou wants to merge 1 commit intoblushiemagic:1.4.4from
Open
Optimize item count lookups in crafting GUI#320Grayjou wants to merge 1 commit intoblushiemagic:1.4.4from
Grayjou wants to merge 1 commit intoblushiemagic:1.4.4from
Conversation
Replace nested iterations over storageItems with O(1) dictionary lookups (itemCounts) when computing available ingredient stacks in CraftingGUI.ItemSlotMethods.cs. This reduces severe lag when players have many stacks of common items by summing group totals via itemCounts, using a directItemCount for direct-type checks, and preserving the early-exit logic for recipe groups and existing context decisions (IngredientNotCraftable / IngredientPartiallyInStock). Comments added to explain the optimization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡ Optimize Item Count Lookups in Crafting GUI
Summary
Replaces inefficient nested iteration over
storageItemswith O(1) dictionary lookups using the existingitemCountsdictionary when determining ingredient availability in the crafting GUI.Problem
The previous implementation used
FirstOrDefaultand nestedforeachloops to check item counts againststorageItems, resulting in O(n × m × k × p) complexity. This caused significant lag/stuttering in the crafting GUI when players had large numbers of item stacks in storage (e.g., stone, mud, dirt, or other bulk materials), as every ingredient slot check would iterate through all stored items repeatedly.Changes
storageItems.FirstOrDefault(stored => stored.type == ...)with a directitemCounts.TryGetValue(...)lookup for the direct item countforeach (Item storedItem in storageItems)loop inside the recipe group check with a singleitemCounts.TryGetValue(type, ...)dictionary lookupItem iandItem storageItemvariablesPerformance Impact
Where
n= number of stored item stacks, which is now eliminated from the complexity.Testing