Python 2.5 WTF



  • From the python 2.5 release notes (http://docs.python.org/dev/whatsnew/other-lang.html):

    "The code generator's peephole optimizer now performs simple constant folding in expressions. If you write something like a = 2+3, the code generator will do the arithmetic and produce code corresponding to a = 5. (Proposed and implemented by Raymond Hettinger.)"



    How did they manage to overlook this trivial optimization for 16 years?



  • The performance benefit of that optimization is potentially pretty low.  In the provided example, we're talking about a savings of 1 integer operation.

    Python is a high level language.  Performance is not the highest priority.
     



  • [quote user="Zlodo"]
    "The code generator's peephole optimizer now performs simple constant folding in expressions. If you write something like a = 2+3, the code generator will do the arithmetic and produce code corresponding to a = 5. (Proposed and implemented by Raymond Hettinger.)"
    [/quote]
    <font face="tahoma,arial,helvetica,sans-serif">Butthead: Hey Beavis, this release note said peephole... uh huh huh huh huh
    Beavis: Yes! peephole! peephole!
    Butthead: Peephole... uh huh huh huh huh
    Beavis: Yes peephole! You need TP for your peephole! Peephole!
    Butthead: Shut up Beavis!



    </font>



  • [quote user="Zlodo"]

    How did they manage to overlook this trivial optimization for 16 years?[/quote]

    It happens.

    Look at little software WTFs that have been there since version 1. Such as disabling Contract Selection in Photoshop when you Select All.



  • This actually isn't a big wtf, Python is a scripting language in the first place, and is not being compiled that much.
    If you know that this is rather complicated to (cleanly) archieve in a compiler, it is perfectly understandable that this is not optimized by default.



  • @Zlodo said:

    How did they manage to overlook this trivial optimization for 16 years?

    Because everything the "compiler" did was directly translate the code into bytecode.

    With Python 2.5, they changed the backend so that the code generator would go through an AST, which means that it becomes practical to actually implement optimizations



  • [quote user="merreborn"]

    The performance benefit of that optimization is potentially pretty low.  In the provided example, we're talking about a savings of 1 integer operation.

    Python is a high level language.  Performance is not the highest priority.
     

    [/quote]
    One integer operation, plus the overhead of dereferencing two Python integer objects to get the operands for that integer operation, plus creating a new Python integer object (if the number is less than 0 or greater than 100) or of incrementing a reference count on an existing one (if the number is from 0 through 100).

    True, the improvement is likely negligible considering the sheer volume of object dereferencing and reference count inc/decrementing going on throughout the rest of your script, but it's still nice to have it.



  • Correct behavior should normally come before optimization.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.