[issue4715] optimize bytecode for conditional branches

Antoine Pitrou report at bugs.python.org
Mon Dec 22 02:49:52 CET 2008


Antoine Pitrou <pitrou at free.fr> added the comment:

Here is an optional patch which provides the two opcodes I was talking
about (I've called them POP_OR_JUMP and JUMP_OR_POP). Together with a
bit of work on the peepholer they make the bytecode expression of
boolean calculations very concise.

A somewhat contrived example: "f = lambda x,y,z,v: (z if x else y) or v"

Without the patch:

  1           0 LOAD_FAST                0 (x) 
              3 JUMP_IF_FALSE            7 (to 13) 
              6 POP_TOP              
              7 LOAD_FAST                2 (z) 
             10 JUMP_FORWARD             4 (to 17) 
        >>   13 POP_TOP              
             14 LOAD_FAST                1 (y) 
        >>   17 JUMP_IF_TRUE             4 (to 24) 
             20 POP_TOP              
             21 LOAD_FAST                3 (v) 
        >>   24 RETURN_VALUE         

With the patch:

  1           0 LOAD_FAST                0 (x) 
              3 POP_JUMP_IF_FALSE       12 
              6 LOAD_FAST                2 (z) 
              9 JUMP_FORWARD             3 (to 15) 
        >>   12 LOAD_FAST                1 (y) 
        >>   15 JUMP_OR_POP             21 
             18 LOAD_FAST                3 (v) 
        >>   21 RETURN_VALUE

Added file: http://bugs.python.org/file12421/condbranches-plus.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4715>
_______________________________________


More information about the Python-bugs-list mailing list