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

PYTHON-4770 Improve CPU overhead of async locks and latency on Windows TLS sendall #1866

Closed
wants to merge 1 commit into from

Conversation

ShaneHarvey
Copy link
Member

@ShaneHarvey ShaneHarvey commented Sep 18, 2024

PYTHON-4770 Improve CPU overhead of async locks and latency on Windows TLS sendall

CPU usage baseline with the sync api:

$ time python bench-lock.py sync         
Python: 3.12.4, PyMongo: 4.9.0.dev0
python bench-lock.py sync  0.19s user 0.05s system 4% cpu 5.333 total

Before:

$ time python bench-lock.py async
Python: 3.12.4, PyMongo: 4.9.0.dev0
python bench-lock.py async  0.77s user 0.42s system 23% cpu 5.168 total

After this change (both user and system CPU are much lower):

$ time python bench-lock.py async
Python: 3.12.4, PyMongo: 4.9.0.dev0
python bench-lock.py async  0.34s user 0.09s system 8% cpu 5.196 total

@blink1073
Copy link
Member

There's another asyncio.sleep(0) in asynchronous/network.py.

@ShaneHarvey
Copy link
Member Author

ShaneHarvey commented Sep 18, 2024

I intentionally left the other sleep(0) in because that code path is already blocking the I/O loop entirely: https://jira.mongodb.org/browse/PYTHON-4636

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM!

@ShaneHarvey
Copy link
Member Author

The loops that I changed here are different because each iteration performs a synchronous non-blocking call which turns the whole thing into a busy loop that eats CPU. wait_for_read is terrible in other ways but it doesn't have that CPU problem.

@ShaneHarvey ShaneHarvey marked this pull request as draft September 18, 2024 15:23
@ShaneHarvey
Copy link
Member Author

It looks like this change makes the Windows test suite run a few minutes slower so I did not merge it. Will need more investigation. Could be better to measure the Lock and sendall changes independently.

@ShaneHarvey
Copy link
Member Author

Closing because we already implemented the backoff in PYTHON-4636 and we're planning to replace these lock classes soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants