Fix client lags when crafting from large stacks in stock keeper #191
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.
When using a stock keeper to issue a crafting order, the algorithm to calculate crafting ingredients tends to lag the client when working with large amount of items. After debugging it, it turns out that
RecipeRequestHelper.resolveIngredientAmountshad a bug causing it to split the source stacks into individual stacks of 1 item each, while the same time having anO(n^2)complexity over the list of resolved ingredients of given type. During my testing attempting to craft while having 20K planks in the system causes the client to freeze for about 10 seconds. This happens when adding the crafting recipe for the first time, or when scrolling on the requested item in the stock keeper GUI.Here's a proof of buggy behavior from the debugger session:

The fix changes how the item stacks are consolidated by comparing only the generic stack keys (which are also stacks), which prevents the list from growing and properly utilizes the stack amount value in resolved list. Now adding a crafting recipe or scrolling over one does not have any noticeable impact on client performance.