Optimising literals away

Stefan Behnel stefan_ml at behnel.de
Tue Aug 31 15:28:24 EDT 2010


John Nagle, 31.08.2010 21:03:
> On 8/30/2010 8:38 AM, Tobias Weber wrote:
>> whenever I type an "object literal" I'm unsure what optimisation will do
>> to it.
>
> CPython is a "naive interpreter". It has almost no optimization.
> It doesn't even really comprehend "constants".
> This is an implementation problem, not a language problem.
>
> Shed Skin has serious optimization but limits the language.
> PyPy has been trying for years, but it still barely works.
> Iron Python seems to be nearing end of life, as Microsoft
> phases it out. Unladen Swallow seems to be in trouble; it's
> been almost a year since the last "quarterly release".

To continue the list, Cython also has a couple of optimisations for 
literals. It caches simple immutable constants, applies numeric constant 
folding and it's obviously a lot faster in packing tuples and lists than 
CPython as it avoids the interpreter loop. It also optimises away the 
literal sequences in "in" tests such as

     if x in (1,2,3):
         ...

which, in the best case of integer literals, even compile down into C 
switch statements.

Stefan




More information about the Python-list mailing list