Make generator (and coroutine) creation explicit in the bytecode. #217
markshannon
started this conversation in
Ideas
Replies: 3 comments
-
I needed to do this to simplify #54 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The basic idea is to have a instruction that makes a generator (or coroutine) from the current frame and immediately returns it.
So the code for
would change from
to
The
GEN_CREATE
instruction would create the generator and clear the current frame. The reason for clearing the current frame, is to avoid lots ofPy_INCREF
s. Moving all references from the frame to the generator can be done cheaply with amemcpy
.I've been in two minds about this change until recently.
It has a certain elegance, and removes the code for special casing calls to generator functions.
But, until now, it was not the most efficient way to create a generator.
Treating calls to generator functions specially had the advantage that we could potentially specialize calls to generator functions at the call site, which would be more efficient than making the call and returning a generator.
However, with #210, specialization for generator creation would effectively need us to call a special generator-creating function, which is what we will be doing anyway with the new bytecode. In other words, this approach is now the fastest approach that I am aware of, plus it is simpler and doesn't prevent specializations of complex calls.
Beta Was this translation helpful? Give feedback.
All reactions