[Python-3000] Set literals

Raymond Hettinger rhettinger at ewtllc.com
Mon Aug 28 22:08:48 CEST 2006

Georg Brandl wrote:

>In the meantime, I played around with the peepholer and tried to copy
>the "for x in tuple_or_list" optimization for sets. Results are in SF
>patch #1548082.
Did you mean "if x in tuple_or_list"?   IIRC, there was some reason that 
mutable lists were not supposed to be made into constants in for-loops.

>* list comprehensions are special-cased because of the LIST_APPEND opcode.
>   If there isn't going to be a special-cased SET_ADD, it's probably the
>   easiest thing to transform {x for x in a} into set(x for x in a) in the
>   AST step, with "set" of course always being the builtin set.

Set comprehensions and list comprehensions are fundamentally the same 
and therefore should have identical implementations. 

While transformation to a generator expression may seem like a good idea 
now, I expect that you'll observe a two-fold performance hit and end-up 
abandoning that approach in favor of the current LIST_APPEND approach.

So it would probably be best to start by teaching the compiler to hide 
the loop variable in a LIST_APPEND approach to list comprehensions and 
then duplicate that approach for set comprehensions.


More information about the Python-3000 mailing list