CPython 是否实现了 PEP 380 中提到的优化?

Does CPython implement the mentioned optimizations from PEP 380?

PEP 380 提到 yield from expr 语法可以在 Python.

中优化

PEP 380 - Optimizations

Using a specialised syntax opens up possibilities for optimisation when there is a long chain of generators. Such chains can arise, for instance, when recursively traversing a tree structure. The overhead of passing __next__() calls and yielded values down and up the chain can cause what ought to be an O(n) operation to become, in the worst case, O(n**2).

A possible strategy is to add a slot to generator objects to hold a generator being delegated to. When a __next__() or send() call is made on the generator, this slot is checked first, and if it is nonempty, the generator that it references is resumed instead. If it raises StopIteration, the slot is cleared and the main generator is resumed.

This would reduce the delegation overhead to a chain of C function calls involving no Python code execution. A possible enhancement would be to traverse the whole chain of generators in a loop and directly resume the one at the end, although the handling of StopIteration is more complicated then.

CPython是否实现了这个优化?

看起来不像。从 Python 3.6 开始,generator object structure doesn't have the proposed field, and the code path for yield from through a chain of generators always goes through the process of resuming their Python stack frames individually. (This code path goes from the YIELD_FROM opcode through _PyGen_Send/gen_iternext to gen_send_ex,从那里到 PyEval_EvalFrameEx,再到链中下一个生成器的 YIELD_FROM。)