Skip to content

Commit ef15bd9

Browse files
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.
1 parent 5a400ce commit ef15bd9

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Modules/_asynciomodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,9 +1202,11 @@ _asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls,
12021202
j++;
12031203
continue;
12041204
}
1205-
ret = PyList_Append(newlist, item);
1205+
ret = _PyList_AppendTakeRef((PyListObject *)newlist, item);
1206+
}
1207+
else {
1208+
Py_DECREF(item);
12061209
}
1207-
Py_DECREF(item);
12081210
if (ret < 0) {
12091211
goto fail;
12101212
}

0 commit comments

Comments
 (0)