[Python-Dev] New opcode to simplifiy/speedup list comprehensions (original) (raw)

Raymond Hettinger raymond.hettinger at verizon.net
Sat Mar 6 08:27:02 EST 2004


If there are no objections, I would like to add an opcode for calling PyList_Append(). This simplifies the generated code for list comprehensions and reduces the overhead on each pass, improving the timings by about 35% on [i for i in itertools.repeat(None, 500)].

The patch is amazingly brief and clear: www.python.org/sf/910929

Raymond Hettinger

Current disassembly of a list comprehension

from dis import dis def f(x): y = [i+i for i in x]

dis(f) 2 0 BUILD_LIST 0 3 DUP_TOP
4 LOAD_ATTR 0 (append) <-- delete this line 7 STORE_FAST 3 ([1]) 10 LOAD_FAST 0 (x) 13 GET_ITER
14 FOR_ITER 20 (to 37) 17 STORE_FAST 2 (i) 20 LOAD_FAST 3 (_[1]) 23 LOAD_FAST 2 (i) 26 LOAD_FAST 2 (i) 29 BINARY_ADD
30 CALL_FUNCTION 1 --> replace with LIST_APPEND 33 POP_TOP <-- delete this line 34 JUMP_ABSOLUTE 14 37 DELETE_FAST 3 (
[1]) 40 STORE_FAST 1 (y) 43 LOAD_CONST 0 (None) 46 RETURN_VALUE

Proposed disassembly of a list comprehension

2 0 BUILD_LIST 0 3 DUP_TOP
4 STORE_FAST 3 ([1]) 7 LOAD_FAST 0 (x) 10 GET_ITER
>> 11 FOR_ITER 17 (to 31) 14 STORE_FAST 2 (i) 17 LOAD_FAST 3 (
[1]) 20 LOAD_FAST 2 (i) 23 LOAD_FAST 2 (i) 26 BINARY_ADD
27 LIST_APPEND
28 JUMP_ABSOLUTE 11 >> 31 DELETE_FAST 3 (_[1]) 34 STORE_FAST 1 (y) 37 LOAD_CONST 0 (None) 40 RETURN_VALUE



More information about the Python-Dev mailing list