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:
Vladimir Matveev 2020-09-18 18:38:38 -07:00 committed by GitHub
parent ec8a15b034
commit 2b05361bf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 148 additions and 39 deletions

View file

@ -15,6 +15,11 @@ than explicitly calling :c:func:`PyGen_New` or :c:func:`PyGen_NewWithQualName`.
The C structure used for generator objects.
.. c:type:: PySendResult
The enum value used to represent different results of :c:func:`PyGen_Send`.
.. c:var:: PyTypeObject PyGen_Type
The type object corresponding to generator objects.
@ -42,3 +47,13 @@ than explicitly calling :c:func:`PyGen_New` or :c:func:`PyGen_NewWithQualName`.
with ``__name__`` and ``__qualname__`` set to *name* and *qualname*.
A reference to *frame* is stolen by this function. The *frame* argument
must not be ``NULL``.
.. c:function:: PySendResult PyGen_Send(PyGenObject *gen, PyObject *arg, PyObject **presult)
Sends the *arg* value into the generator *gen*. Coroutine objects
are also allowed to be as the *gen* argument but they need to be
explicitly casted to PyGenObject*. Returns:
- ``PYGEN_RETURN`` if generator returns. Return value is returned via *presult*.
- ``PYGEN_NEXT`` if generator yields. Yielded value is returned via *presult*.
- ``PYGEN_ERROR`` if generator has raised and exception. *presult* is set to ``NULL``.