Skip to content

Conversation

@Shadows-of-Fire
Copy link
Contributor

This PR adds logic to RecipeLink which engages when the RecipeLink is holding a shaped recipe (of type minecraft:crafting_shaped) to speed up the matching process against other shaped recipes.

Recipe Links are compared O(N^2) times against other recipe links of the same type - in ATM10, there are ~70k recipes, with the vast majority being shaped, so then number of comparisons is... probably in the billions, at least (60k^2 is 3.6B).

So, to preempt the common case, when a RecipeLink for a shaped recipe is encountered, we parse the output item from the underlying JsonObject, and preemptively compare it using reference equality.

This brings AU's startup time in ATM10 from 20s to 1.6s (12.5x faster // 92% uplift). You could probably optimize this further by pushing the HolderLookup.Provider down into the RecipeLink ctor and being able to parse the entire ItemStack, but it seems like this gets the supermajority of the cases so there might not be much point.

@rlnt
Copy link
Member

rlnt commented Apr 27, 2025

I adjusted a few things. The change is very good and a nice trick to avoid JSON comparison. However, you messed up the inner key, which yeeted the recipes instead of comparing them. The inner key in ItemStacks is id, not item.

I also added handling for shapeless recipes, which further improved performance. Here are the benchmarks.

Pre Change

  • World Load: 17.80 s
  • Reload: 17.89 s

Post Change (Shaped)

  • World Load: 3.04 s
  • Reload: 2.27 s

Post Change (Shaped + Shapeless)

  • World Load: 1.375 s
  • Reload 1.4 s

Thanks a lot for this contribution!

@rlnt rlnt merged commit 739f6ce into AlmostReliable:1.21.1 Apr 27, 2025
1 check passed
@embeddedt
Copy link
Contributor

This is a fairly huge speedup, so would a backport of this to 1.20.1 be accepted, and if so, do I have permission to treat this patch as LGPL3, since 1.20.1 is still under the old license?

rlnt added a commit that referenced this pull request Apr 27, 2025
Co-authored-by: Shadows_of_Fire <3682588+Shadows-of-Fire@users.noreply.github.com>
Co-authored-by: rlnt <relentless@rlnt.dev>
@Shadows-of-Fire
Copy link
Contributor Author

Looks like @rlnt already has the cherry pick in. Not worried about the license change myself.

@rlnt
Copy link
Member

rlnt commented Apr 27, 2025

Need to fix a thing first, though. The change completely messed up duplicate comparison since the PR fetches the output item of the original recipe, not the unified recipe. But yes, I will publish a 1.20.1 too along with some other commits I moved over.

@embeddedt
Copy link
Contributor

Thanks! Appreciate it.

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.

3 participants