From ef15bd920e2fb9aefb9c964e5206a17d7025af97 Mon Sep 17 00:00:00 2001 From: muhamedfazalps Date: Sat, 4 Jul 2026 15:47:24 +0530 Subject: [PATCH] gh-150942: Speed up asyncio Future.remove_done_callback Use _PyList_AppendTakeRef instead of PyList_Append to avoid an extra incref/decref pair when compaction appends callbacks to the new list. The item is already owned (Py_INCREF was called earlier in the loop), so _PyList_AppendTakeRef takes ownership of that reference directly without incrementing it again. --- Modules/_asynciomodule.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index bc0f7f3901e0ed..01c231bbb762a9 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1202,9 +1202,11 @@ _asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls, j++; continue; } - ret = PyList_Append(newlist, item); + ret = _PyList_AppendTakeRef((PyListObject *)newlist, item); + } + else { + Py_DECREF(item); } - Py_DECREF(item); if (ret < 0) { goto fail; }