-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Several performance improvements #35
Merged
Merged
Conversation
This file contains 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
Hey, thanks for the PR. I will take a deeper look later. |
rlnt
requested changes
Mar 9, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice performance improvement, thanks for the PR! Two minor things I'd like to change here.
- The duplication config is loaded inside the runtime which we keep alive for other mods to use. The cache needs to be cleared after the unification process.
- Rename
recipeTypeIgnoreCache
toignoredRecipeTypesCache
(I will probably rename a lot of stuff in a new major version because of all the inconsistencies in general).
rlnt
approved these changes
Mar 9, 2023
rlnt
added a commit
that referenced
this pull request
Mar 10, 2023
Co-authored-by: LLytho <main@lytho.dev> Co-authored-by: Relentless <relentless@rlnt.dev>
rlnt
added a commit
that referenced
this pull request
Mar 10, 2023
Co-authored-by: LLytho <main@lytho.dev> Co-authored-by: Relentless <relentless@rlnt.dev>
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.
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.
DuplicationConfig.shouldIgnoreRecipe
has been optimized to avoid matching regexes as much as possible. In particular, whether or not a given recipe type ID is ignored is now cached. This saves a regex match for almost all recipes, as many of them will share the same type.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 callingJsonCompare.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%.Instead the logic was replaced with a simpler for loop that loops over each entry of one of the JSON objects, skips any keys that should be ignored, and attempts to retrieve the same key from the other object. In particular, this eliminates the need to call
get
on one of the objects entirely, as we can simply iterate over its entries and be given both a key and value at the same time.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:
Profiler screenshot after this PR:
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.