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

Core: Rewrite start inventory from pool code #3778

Merged
merged 12 commits into from
Nov 29, 2024

Conversation

NewSoupVi
Copy link
Member

@NewSoupVi NewSoupVi commented Aug 11, 2024

I think the old code is pretty ugly and unpythonic

Here's my proposal for a better version

Even had some room to spare for comments, since I made it a couple lines shorter

@github-actions github-actions bot added affects: core Issues/PRs that touch core and may need additional validation. waiting-on: peer-review Issue/PR has not been reviewed by enough people yet. labels Aug 11, 2024
@NewSoupVi NewSoupVi added the is: refactor/cleanup Improvements to code/output readability or organizization. label Aug 11, 2024
Main.py Outdated
multiworld.itempool[:] = new_items + old_items
new_itempool.append(item)

unfound_items_per_player = {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this section could all be inside an if overall_target: and then at the end have:

else:
    for player, target in target_per_player.items():
        new_itempool += [multiworld.worlds[player].create_filler() for _ in range(target_per_player[player])]

Copy link
Member Author

Choose a reason for hiding this comment

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

I do think this makes the code slightly more complicated, at probably little performance gain (The full iteration of the multiworld itempool is gonna be so much slower than anything that happens after it)

But I see your point. I'll think about it. I don't like the idea of just wrapping it in an extra indent, but maybe there is a different cute solution here.

Copy link
Member Author

@NewSoupVi NewSoupVi Aug 12, 2024

Choose a reason for hiding this comment

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

I timed it roughly, this change barely has any impact. The bottleneck is the exact code you're isolating:

    for player, target in target_per_player.items():
        new_itempool += [multiworld.worlds[player].create_filler() for _ in range(target_per_player[player])]

These lines take the most amount of time (after multiworld itempool iteration, which is the slowest part of the code).
The additional calculations you're avoiding with this change, barely make a dent.
I think in this case, I'd prefer to keep the code shorter / simpler, but open to be convinced still

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I went and timed it myself with 10K players and it saved 0.0054 seconds on average over 20 trials (the range was 0.005-0.006) so... yeah, not worth

Copy link
Member Author

Choose a reason for hiding this comment

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

I made a different improvement that should make this better in the worst cases

Copy link
Member

@Exempt-Medic Exempt-Medic left a comment

Choose a reason for hiding this comment

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

One comment, otherwise the changes LGTM. Tested various configurations of start_inventory_from_pool and all behavior was identical to how it was before.

Copy link
Member

@Exempt-Medic Exempt-Medic left a comment

Choose a reason for hiding this comment

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

New changes look good, did some more testing (including two cases I forgot about which would have failed previously...) and everything seems to work well.

@NewSoupVi
Copy link
Member Author

NewSoupVi commented Aug 12, 2024

I have now opted to remove overall_target.

I understand why it exists, not doing it is about 20% slower in the "average worst case" (lol), which could be considered worth it for a full iteration of the multiworld itempool.

However, it

  1. Makes the code more confusing and complicated
  2. Full multiworld itempool iterations, and things that are way worse than it, are done elsewhere
  3. Now that we have this "warn instead of crash" behavior, I imagine in big multiworlds, statistically, one player will make use of it, and as soon as one item can't be found, we're in the bad code path anyway

I can be convinced to bring it back, though.

Copy link
Contributor

@qwint qwint left a comment

Choose a reason for hiding this comment

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

code looks correct and cuts some corners that should improve performance but didn't actually run the code yet to see if i can catch any issues (medic seems to have ran through most of them tho lol)

@Exempt-Medic Exempt-Medic added waiting-on: core-review Issue/PR has been peer-reviewed and is ready to be merged or needs input from a core maintainer. and removed waiting-on: peer-review Issue/PR has not been reviewed by enough people yet. labels Aug 12, 2024
@NewSoupVi NewSoupVi merged commit c97e486 into ArchipelagoMW:main Nov 29, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects: core Issues/PRs that touch core and may need additional validation. is: refactor/cleanup Improvements to code/output readability or organizization. waiting-on: core-review Issue/PR has been peer-reviewed and is ready to be merged or needs input from a core maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants