Skip to content

Commit a6535d0

Browse files
committed
pythongh-150942: Speed up asyncio future add_done_callback
Use _PyList_AppendTakeRef() in future_add_done_callback() to avoid an extra DECREF on the successful append path.
1 parent ab93017 commit a6535d0

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up adding callbacks to pending :class:`asyncio.Future` objects.

‎Modules/_asynciomodule.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,12 +836,9 @@ future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg,
836836
}
837837

838838
if (fut->fut_callbacks != NULL) {
839-
int err = PyList_Append(fut->fut_callbacks, tup);
840-
if (err) {
841-
Py_DECREF(tup);
839+
if (_PyList_AppendTakeRef(fut->fut_callbacks, tup) < 0) {
842840
return NULL;
843841
}
844-
Py_DECREF(tup);
845842
}
846843
else {
847844
fut->fut_callbacks = PyList_New(1);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from __future__ import annotations
2+
3+
import argparse
4+
import asyncio
5+
import contextvars
6+
import statistics
7+
import sys
8+
import time
9+
10+
11+
def callback(fut: asyncio.Future[None]) -> None:
12+
pass
13+
14+
15+
def bench_add_done_callback(count: int) -> float:
16+
loop = asyncio.new_event_loop()
17+
ctx = contextvars.copy_context()
18+
try:
19+
fut = loop.create_future()
20+
fut.add_done_callback(callback, context=ctx)
21+
fut.add_done_callback(callback, context=ctx)
22+
23+
start = time.perf_counter_ns()
24+
for _ in range(count):
25+
fut.add_done_callback(callback, context=ctx)
26+
return (time.perf_counter_ns() - start) / 1e9
27+
finally:
28+
loop.close()
29+
30+
31+
def main() -> int:
32+
parser = argparse.ArgumentParser()
33+
parser.add_argument("--count", type=int, default=1_000_000)
34+
parser.add_argument("--trials", type=int, default=15)
35+
parser.add_argument("--warmup", type=int, default=3)
36+
args = parser.parse_args()
37+
38+
for _ in range(args.warmup):
39+
bench_add_done_callback(args.count)
40+
41+
timings = [bench_add_done_callback(args.count) for _ in range(args.trials)]
42+
best = min(timings)
43+
median = statistics.median(timings)
44+
mean = statistics.fmean(timings)
45+
stdev = statistics.pstdev(timings)
46+
47+
print(sys.version.replace("\n", " "))
48+
print(f"count: {args.count}")
49+
print(
50+
f"best: {best * 1e3:.3f} ms; "
51+
f"median: {median * 1e3:.3f} ms; "
52+
f"mean: {mean * 1e3:.3f} ms; "
53+
f"stdev: {stdev * 1e3:.3f} ms"
54+
)
55+
print(f"callbacks/sec at best: {args.count / best:.3f}")
56+
return 0
57+
58+
59+
if __name__ == "__main__":
60+
raise SystemExit(main())

0 commit comments

Comments
 (0)