Skip to content

Commit e6f7d35

Browse files
GH-103182: use vectorcall in _asyncio instead of variadic calling APIs (#103175)
1 parent 385b5d6 commit e6f7d35

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

Modules/_asynciomodule.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -355,33 +355,26 @@ call_soon(asyncio_state *state, PyObject *loop, PyObject *func, PyObject *arg,
355355
PyObject *ctx)
356356
{
357357
PyObject *handle;
358-
PyObject *stack[3];
359-
Py_ssize_t nargs;
360358

361359
if (ctx == NULL) {
362-
handle = PyObject_CallMethodObjArgs(
363-
loop, &_Py_ID(call_soon), func, arg, NULL);
360+
PyObject *stack[] = {loop, func, arg};
361+
size_t nargsf = 3 | PY_VECTORCALL_ARGUMENTS_OFFSET;
362+
handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf, NULL);
364363
}
365364
else {
366-
/* Use FASTCALL to pass a keyword-only argument to call_soon */
367-
368-
PyObject *callable = PyObject_GetAttr(loop, &_Py_ID(call_soon));
369-
if (callable == NULL) {
370-
return -1;
371-
}
372-
373365
/* All refs in 'stack' are borrowed. */
374-
nargs = 1;
375-
stack[0] = func;
366+
PyObject *stack[4];
367+
size_t nargs = 2;
368+
stack[0] = loop;
369+
stack[1] = func;
376370
if (arg != NULL) {
377-
stack[1] = arg;
371+
stack[2] = arg;
378372
nargs++;
379373
}
380374
stack[nargs] = (PyObject *)ctx;
381-
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
382-
handle = PyObject_Vectorcall(callable, stack, nargs,
383-
state->context_kwname);
384-
Py_DECREF(callable);
375+
size_t nargsf = nargs | PY_VECTORCALL_ARGUMENTS_OFFSET;
376+
handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf,
377+
state->context_kwname);
385378
}
386379

387380
if (handle == NULL) {
@@ -2359,8 +2352,9 @@ _asyncio_Task_get_stack_impl(TaskObj *self, PyTypeObject *cls,
23592352
/*[clinic end generated code: output=6774dfc10d3857fa input=8e01c9b2618ae953]*/
23602353
{
23612354
asyncio_state *state = get_asyncio_state_by_cls(cls);
2362-
return PyObject_CallFunctionObjArgs(
2363-
state->asyncio_task_get_stack_func, self, limit, NULL);
2355+
PyObject *stack[] = {(PyObject *)self, limit};
2356+
return PyObject_Vectorcall(state->asyncio_task_get_stack_func,
2357+
stack, 2, NULL);
23642358
}
23652359

23662360
/*[clinic input]
@@ -2387,8 +2381,9 @@ _asyncio_Task_print_stack_impl(TaskObj *self, PyTypeObject *cls,
23872381
/*[clinic end generated code: output=b38affe9289ec826 input=150b35ba2d3a7dee]*/
23882382
{
23892383
asyncio_state *state = get_asyncio_state_by_cls(cls);
2390-
return PyObject_CallFunctionObjArgs(
2391-
state->asyncio_task_print_stack_func, self, limit, file, NULL);
2384+
PyObject *stack[] = {(PyObject *)self, limit, file};
2385+
return PyObject_Vectorcall(state->asyncio_task_print_stack_func,
2386+
stack, 3, NULL);
23922387
}
23932388

23942389
/*[clinic input]

0 commit comments

Comments
 (0)