Simple question about how the optimizer works

Andrew Dalke dalke at dalkescientific.com
Fri May 10 04:11:40 EDT 2002


James J. Besemer:
>True.  And this is exactly what the best optimizers do.  Look at all the
>relevant code.

That is *so* not something that interests me.  :)

>The way most optimizers work is they detect patterns known to be safe and
also
>sufficiently common to be worth dealing with.  Anything that does not fit
the
>pattern 100% does not get optimized.

I believe there are very few patterns like this in Python.  Eg, the
one you gave is not one, because what they do is slightly different,
and there's ways to take advantage of those differences to cause different
results.

There are ways to get performance out of dynamically typed languages
like Python (besides the oft-discussed addition of static type info).
But they are hard and call for program analysis at a level more complex
than what I would deem "pattern detection."

There has been previous work on various optimizations.  I believe
the consensus is that it's a hard problem and there are more interesting
and useful things to work on.  OTOH, if someone has a few years to
spend on it, or a lot of money to throw at it, work in languages like
Smalltalk, Lisp, and Self show that it's possible.

(I exclude for this discussion "simple" performance improvements like
speeding up list comprehensions, or replacing one algorithm for another.
Those are simple, local tweaks as compared to larger scale analysis
of how different parts of code interrelate.)

>Recognizing just a few of the most common cases might make a significant
>difference.  I understand Perl does this for certain common top-level loop
>constructs, in order to realize significant gains for those cases.

Yeah, and I've reported at least one bug in Perl's optimizer that
caused a crash in Perl.  Hasn't happened in Python (except on the
SGIs where the C compiler's bad optimizations caused problems.)

Many people over the years have tweaked things to improve performance
besides this.  For example, I like Skip Montanaro's work to improve
lookup performance for module/builtin variables since that's something
which improve a lot of my code by at least a bit, rather than a few
idioms which handle only a few things well.

But as I said, it's a level of complexity and thought which doesn't
interest me any where near enough to contemplate implementing it.  And
the current style makes Python's source easier to understand.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list