Skip to content

Optimize item count lookups in crafting GUI#320

Open
Grayjou wants to merge 1 commit intoblushiemagic:1.4.4from
Grayjou:1.4.4
Open

Optimize item count lookups in crafting GUI#320
Grayjou wants to merge 1 commit intoblushiemagic:1.4.4from
Grayjou:1.4.4

Conversation

@Grayjou
Copy link

@Grayjou Grayjou commented Mar 3, 2026

⚡ Optimize Item Count Lookups in Crafting GUI

Summary

Replaces inefficient nested iteration over storageItems with O(1) dictionary lookups using the existing itemCounts dictionary when determining ingredient availability in the crafting GUI.


Problem

The previous implementation used FirstOrDefault and nested foreach loops to check item counts against storageItems, 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

  • Replaced storageItems.FirstOrDefault(stored => stored.type == ...) with a direct itemCounts.TryGetValue(...) lookup for the direct item count
  • Replaced the inner foreach (Item storedItem in storageItems) loop inside the recipe group check with a single itemCounts.TryGetValue(type, ...) dictionary lookup
  • Removed the now-unnecessary locally captured Item i and Item storageItem variables

Performance Impact

Before After
O(n × m × k × p) per ingredient slot O(m × k) per ingredient slot

Where n = number of stored item stacks, which is now eliminated from the complexity.


Testing

  • Verified ingredient slot highlighting (not in stock / partially in stock / in stock) remains correct
  • No behavioral changes — purely a performance optimization

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant