Style question on recursive generators

Carlos Ribeiro carribeiro at gmail.com
Wed Oct 20 13:18:19 EDT 2004


On Wed, 20 Oct 2004 08:43:22 -0500, Jeff Epler <jepler at unpythonic.net> wrote:
> On Wed, Oct 20, 2004 at 09:12:59AM +0200, Alex Martelli wrote:
> > Anyway, the bytecode that idiom makes is something like:
> >
> >               3 LOAD_FAST                0 (other)
> >               6 GET_ITER
> >         >>    7 FOR_ITER                10 (to 20)
> >              10 STORE_FAST               1 (x)
> >              13 LOAD_FAST                1 (x)
> >              16 YIELD_VALUE
> >              17 JUMP_ABSOLUTE            7
> 
> I think that the other thing the compiler must recognize, beyond this
> sequence, is that "x" is dead below the loop (no longer used, or stored
> to before the next use).

I've read somewhere else that there are plans to make the loop
variable to be limited in scope _to the loop only. Not sure about the
details though.
 
> On the other hand, I guess the new YIELD_VALUES opcode could leave the
> last value on the top of the stack, replacing this sequence with
>     LOAD_FAST    0 (other)
>     GET_ITER
>     YIELD_VALUES
>     STORE_FAST   1 (x)
> I was imagining that YIELD_VALUES would not put anything on the stack,
> if it was used to implement a new 'yield from'/'yield *' statement.

As per your own observation, the STORE_FAST above is not needed, now
that the loop on 'x' was removed. The removal of the bytecode-level
loop (the JUMP_ABSOLUTE bytecode) would probably give some measurable
performance gain. But -- again -- it all depends on whether such idiom
is common enough.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list