subexpressions

Paul Boddie paul at boddie.org.uk
Fri Jun 1 16:28:42 EDT 2007


Steve Howell wrote:
>
> The compiler doesn't know the types up front, but if
> you wanted to do this kind of optimization (and you
> believed that 95% of x*x cases would benefit from it,
> and you're willing to sacrifice performance for the 5%
> of folks that overload multiply), then the compiler
> could generate bytecode that set the stage for later
> conditional caching of the first execution of x*x.

True.

> You'd then need the execution of the bytecodes at
> runtime (ceval.c or something called by it) to work in
> such a way that they only cache the result when side
> effects are not an issue.  At runtime you can reliably
> detect whether something is still a virgin builtin,
> correct?

I've no idea, but I imagine that psyco knows whether or not it has a
proper built-in number object when it generates specialised code for
similar cases.

> To my disclaimer, you would only undertake such an
> optimization if multiplication were really, really
> expensive (which I don't think is even true for floats
> today), and even then you'd proceed cautiously.

Indeed. Some believe that for "full Python" you can only introduce
such measures at run-time, although extensive enough analysis of the
code could perhaps suggest suitable specialisations in advance, as
presumably demonstrated by Shed Skin.

Paul




More information about the Python-list mailing list