mirror of
https://github.com/python/cpython.git
synced 2026-02-24 08:01:14 +00:00
bpo-41756: Introduce PyGen_Send C API (GH-22196)
The new API allows to efficiently send values into native generators and coroutines avoiding use of StopIteration exceptions to signal returns. ceval loop now uses this method instead of the old "private" _PyGen_Send C API. This translates to 1.6x increased performance of 'await' calls in micro-benchmarks. Aside from CPython core improvements, this new API will also allow Cython to generate more efficient code, benefiting high-performance IO libraries like uvloop.
This commit is contained in:
parent
ec8a15b034
commit
2b05361bf7
7 changed files with 148 additions and 39 deletions
|
|
@ -2621,6 +2621,20 @@ task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...)
|
|||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static inline int
|
||||
gen_status_from_result(PyObject **result)
|
||||
{
|
||||
if (*result != NULL) {
|
||||
return PYGEN_NEXT;
|
||||
}
|
||||
if (_PyGen_FetchStopIterationValue(result) == 0) {
|
||||
return PYGEN_RETURN;
|
||||
}
|
||||
|
||||
assert(PyErr_Occurred());
|
||||
return PYGEN_ERROR;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
task_step_impl(TaskObj *task, PyObject *exc)
|
||||
{
|
||||
|
|
@ -2679,26 +2693,29 @@ task_step_impl(TaskObj *task, PyObject *exc)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int gen_status = PYGEN_ERROR;
|
||||
if (exc == NULL) {
|
||||
if (PyGen_CheckExact(coro) || PyCoro_CheckExact(coro)) {
|
||||
result = _PyGen_Send((PyGenObject*)coro, Py_None);
|
||||
gen_status = PyGen_Send((PyGenObject*)coro, Py_None, &result);
|
||||
}
|
||||
else {
|
||||
result = _PyObject_CallMethodIdOneArg(coro, &PyId_send, Py_None);
|
||||
gen_status = gen_status_from_result(&result);
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = _PyObject_CallMethodIdOneArg(coro, &PyId_throw, exc);
|
||||
gen_status = gen_status_from_result(&result);
|
||||
if (clear_exc) {
|
||||
/* We created 'exc' during this call */
|
||||
Py_DECREF(exc);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == NULL) {
|
||||
if (gen_status == PYGEN_RETURN || gen_status == PYGEN_ERROR) {
|
||||
PyObject *et, *ev, *tb;
|
||||
|
||||
if (_PyGen_FetchStopIterationValue(&o) == 0) {
|
||||
if (result != NULL) {
|
||||
/* The error is StopIteration and that means that
|
||||
the underlying coroutine has resolved */
|
||||
|
||||
|
|
@ -2709,10 +2726,10 @@ task_step_impl(TaskObj *task, PyObject *exc)
|
|||
res = future_cancel((FutureObj*)task, task->task_cancel_msg);
|
||||
}
|
||||
else {
|
||||
res = future_set_result((FutureObj*)task, o);
|
||||
res = future_set_result((FutureObj*)task, result);
|
||||
}
|
||||
|
||||
Py_DECREF(o);
|
||||
Py_DECREF(result);
|
||||
|
||||
if (res == NULL) {
|
||||
return NULL;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue