AlmostReliable / almostunified

A Minecraft mod to unify resources.
https://www.curseforge.com/minecraft/mc-mods/almost-unified
GNU Lesser General Public License v3.0
50 stars 9 forks source link

Several performance improvements #35

Closed embeddedt closed 1 year ago

embeddedt commented 1 year ago

Proposed Changes

This PR implements a number of optimizations designed to reduce the time spent running recipe transformations. This was done by carefully profiling a world load during Enigmatica 8 and then targeting the obvious bottlenecks. I have summarized the changes I made below.

As a result of the above optimizations, this method is now about 4 times faster (which can be seen in the profiler results). I believe 2 seconds is enough of a difference that it's not just caused by Java's random speed variation between runs.

Most of the remaining time is spent within handleDuplicate, which essentially boils down to calling JsonCompare.matches. Unfortunately, checking two JSON objects for equality is a rather inefficient process in general, as key retrieval is O(log(n)) in tree maps. Nonetheless I was able to make a couple changes that reduced running time by about 40%.

Unfortunately this method is still bottlenecked by the JsonObject.get call. Further improvements are probably not possible without a cleverer solution that reduces the number of recipes being compared.

Lastly, I added a timer to RecipeTransformer.transformRecipes in order to see how long the process takes even when the profiler is not running.

I would appreciate if you could give these changes a try to verify that they don't only increase performance on ancient hardware. Let me know if you have any questions/feedback.

Additional Context

Profiler screenshot from latest 1.18 version:

before_au

Profiler screenshot after this PR:

after_au

I believe the time increase in unifyRecipes was simply an artifact of garbage collection/some other stall (I have very few CPU cores), and in any case the total runtime is clearly shorter.

LLytho commented 1 year ago

Hey,

thanks for the PR. I will take a deeper look later.