Optimisation problem

Jeff Epler jepler at unpythonic.net
Tue Nov 12 14:33:49 EST 2002


On Tue, Nov 12, 2002 at 12:43:28PM -0600, sismex01 at hebmex.com wrote:
> In this case which is quite simple, yes, it's the
> same result; but others might enlighten us on other
> cases where the "obvious" result isn't the one we'd
> get.
> 
> Any suspects, anyone?

a, b = b, a

?

This is bytecompiled to
          6 LOAD_NAME               0 (b)
          9 LOAD_NAME               1 (a)
         12 BUILD_TUPLE             2
         15 UNPACK_SEQUENCE         2
         18 STORE_NAME              1 (a)
         21 STORE_NAME              0 (b)

A creative individual could possibly write a peephole optimizer to get
rid of BUILD_TUPLE/UNPACK_SEQUENCE, but it means either reversing the
order of calculation or the order of assignment as well.

This approach can't always work, given statements like
        a, b = d or (b, a)

          6 LOAD_NAME                0 (d)
          9 JUMP_IF_TRUE            10 (to 22)
         12 POP_TOP             
         13 LOAD_NAME                1 (b)
         16 LOAD_NAME                2 (a)
         19 BUILD_TUPLE              2
    >>   22 UNPACK_SEQUENCE          2
         25 STORE_NAME               2 (a)
         28 STORE_NAME               1 (b)

Jeff




More information about the Python-list mailing list