From: Kumar Aditya Date: Mon, 10 Feb 2025 09:34:33 +0000 (+0530) Subject: gh-128002: add fast path for native tasks in `asyncio.all_tasks` (#129943) X-Git-Tag: v3.14.0a5~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bff4bfeae1f428a815dc9a57b87f913217188fdd;p=thirdparty%2FPython%2Fcpython.git gh-128002: add fast path for native tasks in `asyncio.all_tasks` (#129943) --- diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 4070076d756f..449c8a949926 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3991,6 +3991,19 @@ static inline int add_one_task(asyncio_state *state, PyObject *tasks, PyObject *task, PyObject *loop) { assert(PySet_CheckExact(tasks)); + if (Task_CheckExact(state, task)) { + int pending = 0; + Py_BEGIN_CRITICAL_SECTION(task); + pending = ((TaskObj *)task)->task_state == STATE_PENDING && ((TaskObj *)task)->task_loop == loop; + Py_END_CRITICAL_SECTION(); + if (pending) { + if (PySet_Add(tasks, task) < 0) { + return -1; + } + } + return 0; + } + PyObject *done = PyObject_CallMethodNoArgs(task, &_Py_ID(done)); if (done == NULL) { return -1;