Optimized bytecode in exec statement

Terry Reedy tjreedy at udel.edu
Mon Feb 1 20:02:01 EST 2010


On 2/1/2010 6:05 PM, Hermann Lauer wrote:
> Dear All,
>
> while trying to optimize some unpack operations with self compiling
> code I wondered howto invoke the optimization in the exec statement.
> Even starting with -O or -OO yields in Python 2.5.2 the same dis.dis code, which
> is appended below.

Currently, as far as I know, -0 just removes asserts.

>
> My interest would be to reduce the LOAD_FAST ops of "s" with DUP_TOP on the stack
> (are there hash lookups behind those ?)

Load_fast is a locals array lookup. load_fast      0 (s) means "put 
locals[0] on the top of the stack". The '(s)' is added for the human reader.

> and of course to optimize the integer
> arithmetics following below (removing +-0 etc).

Something + 0 is no necessarily something. It depends on the type of 
something. So the interpreter will not optimize it away. What concretely 
happens with int+0 is a different matter. You can remove it though, if 
you want to get into byte-code hacking, but why write '+0' if you do not 
want it?


> Thanks for any ideas,
> greetings
>    Hermann
>
>
>>>> import dis
>>>> g=r.rra.LAST3.getter(ds=0,row=0)
>
> class fa(struct.Struct):
>    def __init__(s):
>      super(fa,s).__init__('d')
>
>    def __call__(s,):
>        return s.unpack_from(s.buf,5392+(((s.lastrow()-0)%2880)*3+0)*8)[0]
>
>>>> dis.dis(g.__call__)
>    7           0 LOAD_FAST                0 (s)
>                3 LOAD_ATTR                0 (unpack_from)
>                6 LOAD_FAST                0 (s)
>                9 LOAD_ATTR                1 (buf)
>               12 LOAD_CONST               1 (5392)
>               15 LOAD_FAST                0 (s)
>               18 LOAD_ATTR                2 (lastrow)
>               21 CALL_FUNCTION            0
>               24 LOAD_CONST               2 (0)
>               27 BINARY_SUBTRACT
>               28 LOAD_CONST               3 (2880)
>               31 BINARY_MODULO
>               32 LOAD_CONST               4 (3)
>               35 BINARY_MULTIPLY
>               36 LOAD_CONST               2 (0)
>               39 BINARY_ADD
>               40 LOAD_CONST               5 (8)
>               43 BINARY_MULTIPLY
>               44 BINARY_ADD
>               45 CALL_FUNCTION            2
>               48 LOAD_CONST               2 (0)
>               51 BINARY_SUBSCR
>               52 RETURN_VALUE

Terry Jan Reedy






More information about the Python-list mailing list