Optimized bytecode in exec statement

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Feb 1 20:19:43 EST 2010


En Mon, 01 Feb 2010 20:05:16 -0300, Hermann Lauer  
<Hermann.Lauer at iwr.uni-heidelberg.de> escribió:

> 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.

There is not much difference with -O or -OO; assert statements are  
removed, __debug__ is False, docstrings are not stored (in -OO). Code  
generation is basically the same.

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

No, no lookup is performed at run time. LOAD_FAST takes an integer  
argument, an index into the locals array. The compiler knows (by static  
analysis, at compile time) which names are local; those objects are stored  
in a C array. I've not done such microbenchmark, but using DUP_TOP instead  
might even be slower.

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

Can't you remove it from source beforehand? (or, from  
`r.rra.LAST3.getter(ds=0,row=0)` since it looks like a code generator)

In general, x+0 may execute arbitrary code, depending on the type of x.  
Only if you could determine that x will always be an integer, you could  
optimize +0 away. Python (the current version of CPython) does not perform  
such analysis; you may investigate psyco, Unladen Swallow, ShedSkin for  
such things.

-- 
Gabriel Genellina




More information about the Python-list mailing list