Skip to content
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

Optimize MultiPart and ParallelHatch performance #2684

Merged
merged 3 commits into from
Jan 8, 2025

Conversation

krossgg
Copy link
Contributor

@krossgg krossgg commented Dec 31, 2024

What

This PR aims to optimize some parts of the Multiblock Lifecycle.

  1. Whenever an IMultiPart needed to query the controllers it was attached to, it would iterate through all of its known controller positions and return a new List every time.
  2. When the current parallel setting of a multiblock is needed, its parts list would be searched for the parallel hatch every time, which is especially noticeable when using many GCYM machines.

Implementation Details

  1. As it is highly unlikely for an IMultiController to be moved from its current position without triggering a structure update and thus removing itself from the controller set, we can also keep track of a Set of IMultiControllers. I have used a ReferenceLinkedOpenHashSet from FastUtil, as we only really care about references and using a LinkedSet provides us with consistent iterations, as well as quick access to the first controller in the set.
    This Set does need to be populated manually on the client-side due to an inability to sync, so that is the reason why maintaining the controllerPositions set is useful.
  2. It is very unlikely that the parallel hatch of a multiblock will change while it is formed, and thus can be cached. This has been implemented in the MultiblockControllerMachine class as a simple field. I have chosen to make the method return an Optional to better fit in with our current usages, but it can be switched to a Nullable if desired.

Additionally, some small tweaks have been added:

  • RotorHolderPartMachines now cannot be shared and its methods have been updated to reflect that
  • !getControllers.isEmpty() calls have been changed to isFormed()
  • onUnload has been cleaned up a bit.

Outcome

Performance.

600s runs in fairly stressful situations
Shown is average MSPT spent executing the method
MultiPart example in RotorHolderPartMachine

Before After
image image

Parallel Hatch searching example in GTRecipeModifiers#hatchParallel
(ignore the Optional.map, it's the other bits that are relevant)

Before After
image image

@krossgg krossgg added the type: refactor Suggestion to refactor a section of code label Dec 31, 2024
@krossgg krossgg requested a review from a team as a code owner December 31, 2024 09:46
Copy link
Member

@screret screret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the comment if you feel like it :)

return result;
}

@Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add the @UnmodifiableView annotation here

@jtuc jtuc mentioned this pull request Jan 4, 2025
2 tasks
@krossgg krossgg merged commit 2472c9c into 1.20.1 Jan 8, 2025
2 checks passed
@krossgg krossgg deleted the kr/multi-optimization branch January 8, 2025 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: refactor Suggestion to refactor a section of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants