[Patches] [ python-Patches-707257 ] Improve code generation

SourceForge.net noreply@sourceforge.net
Thu, 20 Mar 2003 17:56:55 -0800


Patches item #707257, was opened at 2003-03-20 17:03
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=707257&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Neal Norwitz (nnorwitz)
Summary: Improve code generation

Initial Comment:
Adds a single function to improve generated bytecode.

Has a two line attachment point, so it is completely 
de-coupled from both the compiler and ceval.c.

The first pass looks for the sequence LOAD_CONST 1, 
JUMP_IF_FALSE xx, POP_TOP.  It replaces the first 
instruction with JUMP_FORWARD +4.

The second pass looks for jumps to an unconditional 
jump.  The first jump target is replaced with the 
second jump target.

Both are safe, general purpose optimizations.  
Together, they eliminate 100% of the "while 1" loop 
overhead.

The structure of the code allows for other code 
improvements to be easily added.  This one focuses 
on low hanging fruit. It takes a simple, safe approach 
that does not change bytecode size or order and does 
not need a basic block analysis.

Improves timings on pybench, pystone, and two of 
my real applications.  timeit.py shows dramatic 
improvement to code using "while 1".

python timeit.py "while 1: break"

python timeit.py -s "i=0" "while 1:" "    if i==1: 
break" "    else: i=1"


----- Example -----

Disassembly of

def f(x):
    while 1:
        x -= 1
        if x == 0:
            break

shows two lines changing from:

  3 LOAD_CONST               1 (1)
38 JUMP_ABSOLUTE            3

and improving to:

3 JUMP_FORWARD             4 (to 10)
38 JUMP_ABSOLUTE           10

All of the other lines are left unchanged.

----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2003-03-20 17:56

Message:
Logged In: YES 
user_id=357491

Perhaps this should be made something that is done with the -O option?  Since this is changing the outputted bytecode from what the parser spits out I think it is classified as an optimization and thus should be made an optional optimization instead of a required one.

Love the idea, though.  Personally, I would love to see some pluggable system developed for -O that allows for easy adding of peephole optimizations.  This patch seems to be taking the initial steps toward a setup like that.

Besides, the poor -O option isn't worth much of anything these days thanks to Michael.  =)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=707257&group_id=5470