[New-bugs-announce] [issue4715] optimize bytecode for conditional branches

Antoine Pitrou report at bugs.python.org
Sun Dec 21 23:58:05 CET 2008


New submission from Antoine Pitrou <pitrou at free.fr>:

This patch optimizes the bytecode for conditional branches.
For example, the list comprehension "[x for x in l if not x]" produced
the following bytecode:

  1           0 BUILD_LIST               0 
              3 LOAD_FAST                0 (.0) 
        >>    6 FOR_ITER                23 (to 32) 
              9 STORE_FAST               1 (x) 
             12 LOAD_FAST                1 (x) 
             15 JUMP_IF_TRUE            10 (to 28) 
             18 POP_TOP              
             19 LOAD_FAST                1 (x) 
             22 LIST_APPEND              2 
             25 JUMP_ABSOLUTE            6 
        >>   28 POP_TOP              
             29 JUMP_ABSOLUTE            6 
        >>   32 RETURN_VALUE         

but after the patch it produces the following bytecode:

  1           0 BUILD_LIST               0 
              3 LOAD_FAST                0 (.0) 
        >>    6 FOR_ITER                18 (to 27) 
              9 STORE_FAST               1 (x) 
             12 LOAD_FAST                1 (x) 
             15 POP_JUMP_IF_TRUE         6 
             18 LOAD_FAST                1 (x) 
             21 LIST_APPEND              2 
             24 JUMP_ABSOLUTE            6 
        >>   27 RETURN_VALUE         

Notice that not only the code is shorter, but the conditional jump
(POP_JUMP_IF_TRUE) jumps right to the start of the loop instead of going
through the JUMP_ABSOLUTE at the end. "continue" statements are helped
similarly.

Furthermore, the old jump opcodes (JUMP_IF_FALSE, JUMP_IF_TRUE) could
profitably be replaced by two new opcodes:
- one which jumps if true and pops otherwise
- one which jumps if false and pops otherwise

----------
components: Interpreter Core
files: condbranches.patch
keywords: patch
messages: 78159
nosy: pitrou
severity: normal
stage: patch review
status: open
title: optimize bytecode for conditional branches
type: resource usage
versions: Python 3.1
Added file: http://bugs.python.org/file12419/condbranches.patch

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


More information about the New-bugs-announce mailing list