You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide C implementation for asyncio.current_task #100344
By providing a C implementation for asyncio.current_task, its performance can be improved.
Pitch
Performance improvement.
From Instagram profiling data, we've found that this function is called frequently, and a C implementation (in Cinder 3.8) showed more than 4x speedup in a microbenchmark.
As a note of support for making this faster, current_task is used in the asgiref.localimplementation, which is used a lot by Django, and it shows up in profiles.
Why bother speeding up the Python version if we have the C version? There really aren't any interesting situations where the C accelerator is unavailable (that I know of).
Why bother speeding up the Python version if we have the C version? There really aren't any interesting situations where the C accelerator is unavailable (that I know of).
In my mind it's "why not speed up the Python version?"
I'm not aware of situations where it matters for cpython users, but maybe alternative implementations that use cpython's stdlib it would be valuable?
anyway, I don't feel strongly about it. happy to revert to the existing python implementation if you'd prefer!
In my mind it's "why not speed up the Python version?"
Because you're replacing one line of code with a well-known idiom with four lines of code that require the reader to follow carefully what's going on and why. For me, reading the version with .get() is much quicker than the try/except version.
If we wrote hyper-optimized code like that everywhere, even when speed doesn't matter, we'd end up with considerably less readable code. So, in my mind the question very much needs to be "why speed it up".
Reacted by Itamar Oren, Paweł Rubin and AlenPaulVarghese
The optimization in the Python version is also very much tuned for the current performance characteristics of the adaptive interpreter (i.e. that dict subscripts are much better optimized than dict method calls.) If alternate Python implementations are the only likely users of the Python implementation, this code change won't necessarily give similar speedups for them.
Reacted by Itamar Oren and Asif Saif Uddin {"Auvi":"অভি"}
Activity
pythongh-100344: Provide C implementation for asyncio.current_task
kumaraditya303 commentedon Dec 19, 2022
Can you post benchmarks with pyperf?
itamaro commentedon Dec 19, 2022
You mean pyperformance suite with and without the C acceleration?
kumaraditya303 commentedon Dec 19, 2022
No this microbenchmark with pyperf.
bluetech commentedon Dec 19, 2022
As a note of support for making this faster,
current_taskis used in theasgiref.localimplementation, which is used a lot by Django, and it shows up in profiles.itamaro commentedon Dec 19, 2022
thanks for the clarification :)
I don't know how to isolate the overhead of the event loop when using pyperf, so hope this is helpful:
C implementation:
Python implementation:
kumaraditya303 commentedon Dec 20, 2022
The numbers looks interesting, it seems to be because there is no fastpath for
dict.getlike there is forlist.appendin ceval.@markshannon Do you have plans to optimize this?
markshannon commentedon Dec 20, 2022
@kumaraditya303 No plans at the moment.
I'd be interested to see how this compared:
I assume that
current_task()is expected to return a task, notNone, most of the time.itamaro commentedon Dec 20, 2022
makes sense!
this optimization make the python impl about 40% faster!
the C impl is still more than 2x faster than this, so maybe do both?
gvanrossum commentedon Dec 21, 2022
Why bother speeding up the Python version if we have the C version? There really aren't any interesting situations where the C accelerator is unavailable (that I know of).
itamaro commentedon Dec 21, 2022
In my mind it's "why not speed up the Python version?"
I'm not aware of situations where it matters for cpython users, but maybe alternative implementations that use cpython's stdlib it would be valuable?
anyway, I don't feel strongly about it. happy to revert to the existing python implementation if you'd prefer!
gvanrossum commentedon Dec 21, 2022
Because you're replacing one line of code with a well-known idiom with four lines of code that require the reader to follow carefully what's going on and why. For me, reading the version with
.get()is much quicker than thetry/exceptversion.If we wrote hyper-optimized code like that everywhere, even when speed doesn't matter, we'd end up with considerably less readable code. So, in my mind the question very much needs to be "why speed it up".
carljm commentedon Dec 22, 2022
The optimization in the Python version is also very much tuned for the current performance characteristics of the adaptive interpreter (i.e. that dict subscripts are much better optimized than dict method calls.) If alternate Python implementations are the only likely users of the Python implementation, this code change won't necessarily give similar speedups for them.
gh-100344: Add C implementation for `asyncio.current_task` (#100345)
Backport gh-100345 / Port D39555934: Provide C implementation for asy…
Merged main into regmachine branch (#50)