https://github.com/python/cpython/commit/0d12f87f13c52052381b3302dc2bcf39168a2b44
commit: 0d12f87f13c52052381b3302dc2bcf39168a2b44
branch: main
author: Andrew Svetlov <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-11-22T15:37:46Z
summary:
gh-162350: Add AC @vectorcall for asyncio module objects (#162439)
files:
A Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst
M Modules/_asynciomodule.c
M Modules/clinic/_asynciomodule.c.h
diff --git
a/Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst
b/Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst
new file mode 100644
index 00000000000000..df101c5386cb45
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst
@@ -0,0 +1,3 @@
+Add ``@vectorcall`` for :class:`asyncio.Future` and :class:`asyncio.Task`
+constructors. The performance boost is 33% for the future creation and 15%
+for the task.
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index d7415aef91532a..5381cda4067ac0 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -872,6 +872,7 @@ future_cancel(asyncio_state *state, FutureObj *fut,
PyObject *msg)
}
/*[clinic input]
+@vectorcall
_asyncio.Future.__init__
*
@@ -893,7 +894,7 @@ This class is *almost* compatible with
concurrent.futures.Future.
static int
_asyncio_Future___init___impl(FutureObj *self, PyObject *loop)
-/*[clinic end generated code: output=9ed75799eaccb5d6 input=89af317082bc0bf8]*/
+/*[clinic end generated code: output=9ed75799eaccb5d6 input=28ff6d3b6c3aa828]*/
{
return future_init(self, loop);
@@ -1734,6 +1735,7 @@ static PyType_Slot Future_slots[] = {
{Py_tp_methods, FutureType_methods},
{Py_tp_getset, FutureType_getsetlist},
{Py_tp_init, _asyncio_Future___init__},
+ {Py_tp_vectorcall, _asyncio_Future_vectorcall},
{Py_tp_new, PyType_GenericNew},
{Py_tp_finalize, FutureObj_finalize},
@@ -2290,6 +2292,7 @@ swap_current_task(_PyThreadStateImpl *ts, PyObject *loop,
PyObject *task)
/* ----- Task */
/*[clinic input]
+@vectorcall
_asyncio.Task.__init__
coro: object
@@ -2306,7 +2309,7 @@ static int
_asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
PyObject *name, PyObject *context,
int eager_start)
-/*[clinic end generated code: output=7aced2d27836f1a1 input=18e3f113a51b829d]*/
+/*[clinic end generated code: output=7aced2d27836f1a1 input=de87c52b0800ab4b]*/
{
if (future_init((FutureObj*)self, loop)) {
return -1;
@@ -2952,6 +2955,7 @@ static PyType_Slot Task_slots[] = {
{Py_tp_methods, TaskType_methods},
{Py_tp_getset, TaskType_getsetlist},
{Py_tp_init, _asyncio_Task___init__},
+ {Py_tp_vectorcall, _asyncio_Task_vectorcall},
{Py_tp_new, PyType_GenericNew},
{Py_tp_finalize, TaskObj_finalize},
diff --git a/Modules/clinic/_asynciomodule.c.h
b/Modules/clinic/_asynciomodule.c.h
index 7ffddc20ea3153..e86ad04d4847e3 100644
--- a/Modules/clinic/_asynciomodule.c.h
+++ b/Modules/clinic/_asynciomodule.c.h
@@ -4,10 +4,10 @@ preserve
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
# include "pycore_gc.h" // PyGC_Head
-# include "pycore_runtime.h" // _Py_ID()
#endif
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
+#include "pycore_runtime.h" // _Py_SINGLETON()
PyDoc_STRVAR(_asyncio_Future___init____doc__,
"Future(*, loop=None)\n"
@@ -30,7 +30,8 @@ static int
_asyncio_Future___init___impl(FutureObj *self, PyObject *loop);
static int
-_asyncio_Future___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+_asyncio_Future___init___helper(PyObject *self, PyObject *const *args,
+ Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
{
int return_value = -1;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
@@ -62,11 +63,10 @@ _asyncio_Future___init__(PyObject *self, PyObject *args,
PyObject *kwargs)
#undef KWTUPLE
PyObject *argsbuf[1];
PyObject * const *fastargs;
- Py_ssize_t nargs = PyTuple_GET_SIZE(args);
- Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
+ Py_ssize_t noptargs = nargs + nkw - 0;
PyObject *loop = Py_None;
- fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs,
kwargs, NULL, &_parser,
+ fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
/*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
if (!fastargs) {
goto exit;
@@ -82,6 +82,42 @@ _asyncio_Future___init__(PyObject *self, PyObject *args,
PyObject *kwargs)
return return_value;
}
+static int
+_asyncio_Future___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ return _asyncio_Future___init___helper(self, _PyTuple_CAST(args)->ob_item,
+ PyTuple_GET_SIZE(args),
+ kwargs ? PyDict_GET_SIZE(kwargs) : 0,
+ kwargs, NULL);
+}
+
+static PyObject *
+_asyncio_Future_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject *self;
+ int _result;
+
+ assert(_PyType_CAST(type)->tp_init == _asyncio_Future___init__);
+ /* Make sure the type object is immutable: the generated
+ * vectorcall doesn't deal e.g. with users reassigning __init__. */
+ assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
+ self = _PyType_CAST(type)->tp_new(_PyType_CAST(type),
+ (PyObject *)&_Py_SINGLETON(tuple_empty), NULL);
+ if (self == NULL) {
+ return NULL;
+ }
+ _result = _asyncio_Future___init___helper(self, args, nargs,
+ kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
+ NULL, kwnames);
+ if (_result != 0) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ return self;
+}
+
PyDoc_STRVAR(_asyncio_Future_result__doc__,
"result($self, /)\n"
"--\n"
@@ -755,7 +791,8 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro,
PyObject *loop,
int eager_start);
static int
-_asyncio_Task___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+_asyncio_Task___init___helper(PyObject *self, PyObject *const *args,
+ Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
{
int return_value = -1;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
@@ -787,15 +824,14 @@ _asyncio_Task___init__(PyObject *self, PyObject *args,
PyObject *kwargs)
#undef KWTUPLE
PyObject *argsbuf[5];
PyObject * const *fastargs;
- Py_ssize_t nargs = PyTuple_GET_SIZE(args);
- Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
+ Py_ssize_t noptargs = nargs + nkw - 1;
PyObject *coro;
PyObject *loop = Py_None;
PyObject *name = Py_None;
PyObject *context = Py_None;
int eager_start = 0;
- fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs,
kwargs, NULL, &_parser,
+ fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
/*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
if (!fastargs) {
goto exit;
@@ -833,6 +869,42 @@ _asyncio_Task___init__(PyObject *self, PyObject *args,
PyObject *kwargs)
return return_value;
}
+static int
+_asyncio_Task___init__(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ return _asyncio_Task___init___helper(self, _PyTuple_CAST(args)->ob_item,
+ PyTuple_GET_SIZE(args),
+ kwargs ? PyDict_GET_SIZE(kwargs) : 0,
+ kwargs, NULL);
+}
+
+static PyObject *
+_asyncio_Task_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject *self;
+ int _result;
+
+ assert(_PyType_CAST(type)->tp_init == _asyncio_Task___init__);
+ /* Make sure the type object is immutable: the generated
+ * vectorcall doesn't deal e.g. with users reassigning __init__. */
+ assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
+ self = _PyType_CAST(type)->tp_new(_PyType_CAST(type),
+ (PyObject *)&_Py_SINGLETON(tuple_empty), NULL);
+ if (self == NULL) {
+ return NULL;
+ }
+ _result = _asyncio_Task___init___helper(self, args, nargs,
+ kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
+ NULL, kwnames);
+ if (_result != 0) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ return self;
+}
+
static int
_asyncio_Task__log_destroy_pending_get_impl(TaskObj *self);
@@ -2104,4 +2176,4 @@ _asyncio_future_discard_from_awaited_by(PyObject *module,
PyObject *const *args,
#define _ASYNCIO_TASK__FUT_WAITER_GETSETDEF {"_fut_waiter",
(getter)_asyncio_Task__fut_waiter_get, (setter)NULL, NULL},
-/*[clinic end generated code: output=419f6d2b73047616 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=da5f44b3f6de9883 input=a9049054013a1b77]*/
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]