-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
bpo-32204: Optimize asyncio.Future _schedule_callbacks #4186
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
bpo-32204: Optimize asyncio.Future _schedule_callbacks #4186
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. Thanks again to your contribution and we look forward to looking at it! |
This requires an issue in the bug tracker. Please create one and reference the issue number here in the title. Thanks. |
@Mariatta comments addressed. |
This changes the intended behavior. If callbacks add other callbacks the latter will be lost. |
The change is safe from my point of view. @1st1 could you take a look? |
Why the code was written in this way at first time? While we don't know the reasons it would be safer to not touch it. I don't see any difference in benchmarks. And it shouldn't be, since seems this code is not executed in normal circumstances. |
@asvetlov The PR improves performance by ~3-5% which is noticeable at larger scales. I found the issue while benchmarking and figured the python project would benefit from it.
I was trying to to find the original reason but the code is from around 2015, when asyncio was in a separate repository. I personally believe it was an oversight. I personally believe it would be safer to assume that since tests pass, this PR should be considered safe, but I do not know the breadth of asyncio's tests. The original commit is big and do not dive into implementation detail. |
The Python version of |
I'm sorry, but I have to close this PR. I'll take a look at your benchmark of async/await later. |
FWIW we can further optimize |
This is actually true. I suggest we leave Python code as is. C code in |
Summary
While migrating our code to asyncio, we've noticed that performance was not up to our standards. We have been performing profiles to determine what are some quick wins we could contribute to python in order to increase asyncio's performance.
While investigating, we have noticed that the method
set_result
ofasyncio.Future
is very slow when a lot of callbacks are waiting for the future. When digging, we noticed that a full copy of all callbacks is made.This PR corrects this issue and reduces the amount of time
set_result
takes on our benchmarks from 17% down to 2.5%Test plan
In addition to passing all unit tests, I performed a before and after profiles using
cProfile
:Before:
After:
https://bugs.python.org/issue32204