Commit 76cbbc6
committed
gh-100344: Provide C implementation for asyncio.current_task
Implementing it in C makes it about 4x-6x faster
Microbenchmark:
```
# bench.py
import time
import timeit
import asyncio
ITERS: int = 10**6
NANO: int = 10**9
NANO_PER_ITER: float = NANO / ITERS
async def main():
# avoid load attr noise
py_current_task = asyncio.tasks._py_current_task
c_current_task = asyncio.tasks._c_current_task
asyncio.current_task() # warmup
py_current_task() # warmup
c_current_task() # warmup
print(
"current_task: {}ns".format(timeit.timeit(py_current_task, number=ITERS, timer=time.process_time) * NANO_PER_ITER)
)
print(
"current_task: {}ns".format(timeit.timeit(c_current_task, number=ITERS, timer=time.process_time) * NANO_PER_ITER)
)
asyncio.run(main())
```
a few runs on MacBook Pro
2.4 GHz 8-Core Intel Core i9
64 GB 2667 MHz DDR4:
debug build:
```
~/work/pyexe/main-dbg ⌚ 9:57:34
$ ./python.exe bench.py
[py] current_task: 606.234ns
[c] current_task: 104.47699999999993ns
~/work/pyexe/main-dbg ⌚ 9:57:59
$ ./python.exe bench.py
[py] current_task: 631.856ns
[c] current_task: 110.22500000000002ns
~/work/pyexe/main-dbg ⌚ 9:58:08
$ ./python.exe bench.py
[py] current_task: 637.746ns
[c] current_task: 105.03899999999999ns
~/work/pyexe/main-dbg ⌚ 9:58:16
$ ./python.exe bench.py
[py] current_task: 621.3169999999999ns
[c] current_task: 103.01300000000002ns
```
opt build:
```
~/work/pyexe/main-opt ⌚ 10:33:17
$ ./python.exe bench.py
[py] current_task: 128.743ns
[c] current_task: 31.997999999999998ns
~/work/pyexe/main-opt ⌚ 10:33:24
$ ./python.exe bench.py
[py] current_task: 126.388ns
[c] current_task: 32.64599999999998ns
~/work/pyexe/main-opt ⌚ 10:33:26
$ ./python.exe bench.py
[py] current_task: 137.053ns
[c] current_task: 32.066999999999986ns
~/work/pyexe/main-opt ⌚ 10:33:28
$ ./python.exe bench.py
[py] current_task: 131.17800000000003ns
[c] current_task: 32.06600000000001ns
```1 parent 702a5bc commit 76cbbc6
File tree
5 files changed
+109
-2
lines changed- Doc/whatsnew
- Lib/asyncio
- Misc/NEWS.d/next/Library
- Modules
- clinic
5 files changed
+109
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
228 | 231 | | |
229 | 232 | | |
230 | 233 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
964 | 964 | | |
965 | 965 | | |
966 | 966 | | |
| 967 | + | |
967 | 968 | | |
968 | 969 | | |
969 | 970 | | |
| |||
973 | 974 | | |
974 | 975 | | |
975 | 976 | | |
976 | | - | |
| 977 | + | |
| 978 | + | |
977 | 979 | | |
978 | 980 | | |
979 | 981 | | |
| 982 | + | |
980 | 983 | | |
981 | 984 | | |
982 | 985 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3344 | 3344 | | |
3345 | 3345 | | |
3346 | 3346 | | |
| 3347 | + | |
| 3348 | + | |
| 3349 | + | |
| 3350 | + | |
| 3351 | + | |
| 3352 | + | |
| 3353 | + | |
| 3354 | + | |
| 3355 | + | |
| 3356 | + | |
| 3357 | + | |
| 3358 | + | |
| 3359 | + | |
| 3360 | + | |
| 3361 | + | |
| 3362 | + | |
| 3363 | + | |
| 3364 | + | |
| 3365 | + | |
| 3366 | + | |
| 3367 | + | |
| 3368 | + | |
| 3369 | + | |
| 3370 | + | |
| 3371 | + | |
| 3372 | + | |
| 3373 | + | |
| 3374 | + | |
| 3375 | + | |
| 3376 | + | |
| 3377 | + | |
| 3378 | + | |
| 3379 | + | |
| 3380 | + | |
| 3381 | + | |
| 3382 | + | |
| 3383 | + | |
| 3384 | + | |
3347 | 3385 | | |
3348 | 3386 | | |
3349 | 3387 | | |
| |||
3599 | 3637 | | |
3600 | 3638 | | |
3601 | 3639 | | |
| 3640 | + | |
3602 | 3641 | | |
3603 | 3642 | | |
3604 | 3643 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments