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

Hatchery: Faster sorting algorithm #373

Merged
merged 3 commits into from
Jul 12, 2024

Conversation

Xwaler
Copy link
Contributor

@Xwaler Xwaler commented Jul 8, 2024

Currently each loop of the Hatchery Automation sorts the whole pokémon list only to use at most the first 4 to fill the hatchery queue, this results in a significant overhead and some visible stuttering with large parties. On my 1100% save the automation used 99ms every second.

This PR proposes to use partial sorting to only compute the sorted top 4 pokémons, avoiding spending a lot of time sorting the 1300+ other.

Only caveat of this is that the pokerus spreading priority currently requires the whole list to be sorted to pick relevant uninfected/contagious pokémons depending on the current hatchery state. For instance the first contagious pokémon with the needed types to spread might be at the very end of the sorted list for all I know.
Therefore, when this priority is enabled (and some pokémons are uninfected), the previous complete sort is done instead to preserve functionnality at the cost of performance. This seems like a reasonable compromise considering it's very quick to be done spreading the pokerus.

The results show that with the attribute sort set to Breeding Efficiency (the most expensive), the loop goes from 99ms to 19ms. Stutters are also gone.

Before
After

@Farigh
Copy link
Owner

Farigh commented Jul 12, 2024

Hi,

Sorry for the late review, I couldn't find time sooner 😞

That's an awesome improvement, I did not think about this optimalisation when I tried to optimise a bit the scripts to run on my Py4. Good job !
If you figure-out a way to include the pokérus filter at some point, don't hesitate to submit a new PR 😉

Side note : I fixed a few style issues, and added a bit more comments, please try to fit the project's style and documentation for futur work.

Thanks again 🚀

@Farigh Farigh merged commit 6f9f192 into Farigh:master Jul 12, 2024
1 check passed
@Farigh
Copy link
Owner

Farigh commented Jul 12, 2024

I also renamed the partialSort Utils method, since it did not do a partial sort per say, which would still return a list with all the elements (and have only the first part being sorted).
I chose the getSortedSubRange name, as it seems more appropriate.

Additionally, I optimised a bit the bisect method by returning directly if the exact match was found.

Finally, I fixed a risk of overflow. It could not happen in the hatchery case (since the user would definitely have at least 4 pokémons at this point), but since Utils methods are meant to be reused in any other case of the project, I prefer to be exhaustive.

@Xwaler
Copy link
Contributor Author

Xwaler commented Jul 12, 2024

Thank you for the improvements !

As for the style issues, my apologies I completly forgot to refactor to the (former) partialSort function, kind of written it to benchmark and then left it there haha. I'll do my best to better fit the project's guidelines from now on 😉

@Farigh Farigh added the enhancement An improvement of an existing feature label Jul 12, 2024
@Farigh Farigh added this to the v0.6 milestone Jul 12, 2024
@Xwaler Xwaler deleted the hatchery-optimization branch July 26, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants