[Python-Dev] Bytecode analysis

Damien Morton damien.morton@acm.org
Tue, 25 Feb 2003 17:19:30 -0500


Ive done a static analysis of the bytecodes from compiling the python
standard library:

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32


Some stats about JUMP_IF_FALSE opcodes

Of the 2768 JUMP_IF_FALSE opcodes encountered, 2429 have a POP_TOP on
both branches.

Id like to propose that JUMP_IF_FALSE consume the top-of-stack.


Some stats about constants

50% of constant accesses are to a fixed set of 5 constants
http://www.bitfurnace.com/python/stats-consts.txt
rank, freq, cum%, const
   1, 1277, 18.7, None
   2,  929, 32.3, 1
   3,  741, 43.1, 0
   4,  254, 46.8, ''
   5,  228, 50.1, 2

Id like to propose the following opcodes be added
LOAD_CONST(NONE)
LOAD_CONST(1)
LOAD_CONST(0)
LOAD_CONST(EMPTY_STR)

Some stats about the number of constants and locals used in functions

97% of functions use 16 or less constants
83% of functions use 8 or less constants
http://www.bitfurnace.com/python/stats-func-consts.txt

98% of functions use 16 or less locals
85% of functions use 8 or less locals
http://www.bitfurnace.com/python/stats-func-locals.txt

Id like to propose the following opcodes be added (I suggest n=15)

LOAD_CONST_[0..n]
LOAD_FAST_[0..n]
STORE_FAST_[0..n]
DELETE_FAST_[0..n]


Some stats about instruction traces
Please see the following links for detailed stats
http://www.bitfurnace.com/python/stats-traces-original.txt
http://www.bitfurnace.com/python/stats-traces-modified.txt
The second file contains stats on instruction traces incorporating the
proposed changes
The score column, in both files, is computed by multiplying the
frequency by the length of the trace

Id like to propose the following opcodes, which should reduce number of
bytecode instructions used by 20%:

RETURN_FAST == LOAD_FAST, RETURN_VALUE
RETURN_CONST == LOAD_CONST, RETURN_VALUE
LOAD_FAST+1 == LOAD_FAST, LOAD_FAST
STORE_FAST+1 == STORE_FAST, STORE_FAST
POP_TOP+1 == POP_TOP, POP_TOP
POP_TOP+2 == POP_TOP, POP_TOP, POP_TOP
BRANCH_IF == COMPARE_OP, JUMP_IF_FALSE, POP_TOP

Notes:

LOAD_FAST+1 and STORE_FAST+1 could be implemented as a 1 byte
instruction code followed by two nibbles encoding the local index
numbers. See above for a discussion of local variable index numbers.

BRANCH_IF could be implimented as a set of opcodes, one for each of the
possible compare_ops