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

MultiServer: speed up location commands #1926

Merged

Conversation

black-sliver
Copy link
Member

@black-sliver black-sliver commented Jul 2, 2023

What is this fixing or adding?

Adds optimized pure python wrapper around locations dict
Adds optimized cython implementation of the wrapper, saving cpu time and 80% memory use
Allows releasing gil during !hint

I'm not sure yet how to automatically build the _speedups and if we really want it in the root directory. Happy for feedback.

How was this tested?

Performance was evaluated using original and new function calls with 1000 slot async dummy data. Testing was done running MultiServer.py. Do we want to add tests for the two implementations?

Pure python implementation

  • find_item for /hint is as slow as it was before
  • get_for_player for /collect is as slow as it was before
  • get_checked for Connect and /checked is a lot¹ faster for blank state and up to 4x for non-blank
  • get_missing for Connect and /missing is a lot¹ faster for blank state and up to 4x for non-blank
  • get_remaining for /remaining is up to 4x faster

Cython implementation

  • uses 80% less memory
  • regular access as a fake dict is ~10 - 20ns slower because it has to construct the result tuple from native integers
    • converting it to json takes >1us, so where it's actually being used for output, the slowdown does not matter
    • using specialized accessors in slow places saves more than this adds
  • find_item for /hint is ~50x as fast and releases the gil
  • get_for_player for /collect is ~50x as fast and releases the gil
  • get_checked for Connect /checked is a lot¹ faster for blank state and 2x-5x for non-blank
  • get_missing for Connect /missing is a lot¹ faster for blank state and 2x-5x for non-blank
  • get_remaining for /remaining is a bit slower than the new pure python get_remaining because of native<->python ints, but a lot faster than the original code.

¹ "a lot" is in the ball park of 600x for get_checked and 6x for get_missing for slots with 1000 locations.
This only saves 0.1ms for each connect to such a blank slot, but that's still a big chunk of the CPU time used during Connect.

Adds optimized pure python wrapper around locations dict
Adds optimized cython implementation of the wrapper, saving cpu time and 80% memory use
@ThePhar ThePhar added is: bug/fix Issues that are reporting bugs or pull requests that are fixing bugs. affects: core Issues/PRs that touch core and may need additional validation. labels Jul 2, 2023
@black-sliver black-sliver force-pushed the cython-location-collection branch from dc0aa79 to e8b0732 Compare July 2, 2023 22:47
@black-sliver black-sliver force-pushed the cython-location-collection branch from 4dd880c to 3ac07a2 Compare July 2, 2023 23:47
@black-sliver black-sliver requested a review from Berserker66 July 3, 2023 09:26
@black-sliver black-sliver force-pushed the cython-location-collection branch from 60dec18 to 825baec Compare July 4, 2023 08:31
now with 2x nicer names
@black-sliver black-sliver force-pushed the cython-location-collection branch from 825baec to 2ed2163 Compare July 4, 2023 08:39
@black-sliver black-sliver merged commit b6e78bd into ArchipelagoMW:main Jul 4, 2023
@black-sliver black-sliver deleted the cython-location-collection branch July 4, 2023 17:12
ThePhar added a commit to ThePhar/Archipelago that referenced this pull request Jul 4, 2023
FlySniper pushed a commit to FlySniper/Archipelago that referenced this pull request Nov 14, 2023
* MultiServer: speed up location commands

Adds optimized pure python wrapper around locations dict
Adds optimized cython implementation of the wrapper, saving cpu time and 80% memory use

* Speedups: auto-build on import and build during setup

* Speedups: add requirements

* CI: don't break with build_ext

* Speedups: use C++ compiler for pyximport

* Speedups: cleanup and more validation

* Speedups: add tests for LocationStore

* Setup: delete temp in-place build modules

* Speedups: more tests and safer indices

The change has no security implications, but ensures that entries[IndexEntry.start] is always valid.

* Speedups: add cython3 compatibility

* Speedups: remove unused import

* Speedups: reformat

* Speedup: fix empty set in test

* Speedups: use regular dict in Locations.get_for_player

* CI: run unittests with beta cython

now with 2x nicer names
Jouramie pushed a commit to Jouramie/Archipelago that referenced this pull request Feb 28, 2024
* MultiServer: speed up location commands

Adds optimized pure python wrapper around locations dict
Adds optimized cython implementation of the wrapper, saving cpu time and 80% memory use

* Speedups: auto-build on import and build during setup

* Speedups: add requirements

* CI: don't break with build_ext

* Speedups: use C++ compiler for pyximport

* Speedups: cleanup and more validation

* Speedups: add tests for LocationStore

* Setup: delete temp in-place build modules

* Speedups: more tests and safer indices

The change has no security implications, but ensures that entries[IndexEntry.start] is always valid.

* Speedups: add cython3 compatibility

* Speedups: remove unused import

* Speedups: reformat

* Speedup: fix empty set in test

* Speedups: use regular dict in Locations.get_for_player

* CI: run unittests with beta cython

now with 2x nicer names
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: bug/fix Issues that are reporting bugs or pull requests that are fixing bugs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants