parse-time optimizations

Delaney, Timothy tdelaney at avaya.com
Tue May 29 21:22:15 EDT 2001


> Thing is, if you're writing code that benefits from constant folding -
> WHY?  If your code contains things like:
> 
>     "a" + "b"
> 
> then you have bigger problems than Python's lack of optimizations...

There are three main things which would benefit from constant folding.

1. Negative numbers. Currently -3 performs a runtime negation each time
through the code. This could be folded to a stored negative number.

2. Complex numbers. Complex numbers are often written as 2+2j (for example),
which again is a runtime computation each time through the code.

3. (Future) Rational numbers. In the future, I think rationals will
primarily be written as 2+2/3. This involves both a division and an addition
at runtime, when such a constant could be folded at compile-time.

These are (or will be) the most common cases, and I do not feel that any of
these indicate "big problems" in design. The only way to write these without
performing the computation each time is to bind them to a variable name.
Often this is the right thing to do anyway (remove magic numbers), but even
in this case you will take the performance hit at least once.

I don't see this as an urgent thing to do, but I think it would be a
worthwhile project for someone (anyone, possibly me) when they get the time
or have a desire to play with the python source. There are no disadvantages
to constant folding, no backwards compatibility issues, and I'm certain a
number of applications will benefit.

Tim Delaney




More information about the Python-list mailing list