optimization with the "compiler" module
Andrew Dalke
dalke at dalkescientific.com
Tue Apr 16 19:35:54 EDT 2002
sean.bowman at acm.org:
>I'm wondering if there's a way to optimize code generated using the
>compiler module. for example, if I say::
>
> import dis
> from parser import compileast, expr
>
> code = compileast(expr('x+3+7.'))
> print dis.dis(code)
>
>I notice that the constants aren't folded. is there a way to do this, or
>do I have to fiddle with the ast myself?
Suppose you have
class Strange:
def __add__(self, other):
return self
x = Strange()
Then x+3+7.0 = (x+3)+7.0 = x+7.0 = x
The compiler can't know what 'x' might be so can't fold constants
in this case. You could if it was '3+7.0+x'.
One advantage to Python is that incorrect constant folding has
never caused a bug in user code. Compare this to C compilers where
I've had plenty of troubles with the optimizer. The Python
implementation leans more towards simple than squeeze out performance
-- the latter can be done either with hand optimization or switching
to C.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list