Staging
v0.5.1
https://github.com/python/cpython
Revision 9de7ec7868554e92700e4bda6c5f9d8fcad634dc authored by Jeffrey Yasskin on 25 February 2009, 02:25:04 UTC, committed by Jeffrey Yasskin on 25 February 2009, 02:25:04 UTC
This patch by Antoine Pitrou optimizes the bytecode for conditional branches by
merging the following "POP_TOP" instruction into the conditional jump.  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) have been
replaced by two new opcodes:
- JUMP_IF_TRUE_OR_POP, which jumps if true and pops otherwise
- JUMP_IF_FALSE_OR_POP, which jumps if false and pops otherwise
1 parent 0a68b01
History
Tip revision: 9de7ec7868554e92700e4bda6c5f9d8fcad634dc authored by Jeffrey Yasskin on 25 February 2009, 02:25:04 UTC
http://bugs.python.org/issue4715
Tip revision: 9de7ec7
File Mode Size
Demo
Doc
Grammar
Include
Lib
Mac
Misc
Modules
Objects
PC
PCbuild
Parser
Python
Tools
.bzrignore -rw-r--r-- 673 bytes
.hgignore -rw-r--r-- 866 bytes
.hgtags -rw-r--r-- 3.3 KB
LICENSE -rw-r--r-- 13.7 KB
Makefile.pre.in -rw-r--r-- 38.1 KB
README -rw-r--r-- 6.5 KB
RELNOTES -rw-r--r-- 1.4 KB
configure -rwxr-xr-x 657.1 KB
configure.in -rw-r--r-- 103.1 KB
install-sh -rwxr-xr-x 7.0 KB
pyconfig.h.in -rw-r--r-- 29.0 KB
runtests.sh -rwxr-xr-x 2.1 KB
setup.py -rw-r--r-- 63.7 KB

README

back to top