[Patches] [ python-Patches-1442442 ] Faster list comprehensions

SourceForge.net noreply at sourceforge.net
Fri Mar 3 18:23:19 CET 2006


Patches item #1442442, was opened at 2006-03-03 07:41
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1442442&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Collin Winter (collinwinter)
>Assigned to: Nobody/Anonymous (nobody)
Summary: Faster list comprehensions

Initial Comment:
The attached patch results in a 16% speed increase for
list comprehensions. By changing how the LIST_APPEND
opcode works (which was previously totally unused),
it's possible to dramatically simplify the emitted
bytecode for listcomps.

More details are available on my blog:
http://oakwinter.com/code/?p=22.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2006-03-03 12:23

Message:
Logged In: YES 
user_id=80475

I believe the patch is not correct for comprehensions with 
multiple for-loops.  It needs to leave ceval.c, opcode.h, 
and opcode.py exactly as the were in Py2.4.  Also, the 
code for ceval.c should only access the stack through the 
stack access macros and avoid derefencing the stack 
pointer directly.

The patch should be resubmitted to only modify compile.c 
and to produce the same code as in Py2.4 (see example 
below and note the inclusion of the DUP_TOP after the 
BUILD_LIST).

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2006-03-03 11:21

Message:
Logged In: YES 
user_id=80475

Neal, did the LIST_APPEND generation get left out of the 
AST.  In Py2.4, is was working fine:

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 
bit (Intel)] on win32
>>> from dis import dis
>>> dis(compile('[x for x in range(10) if x % 
2]', '', 'single'))
  1           0 BUILD_LIST               0
              3 DUP_TOP             
              4 STORE_NAME               0 (_[1])
              7 LOAD_NAME                1 (range)
             10 LOAD_CONST               0 (10)
             13 CALL_FUNCTION            1
             16 GET_ITER            
        >>   17 FOR_ITER                28 (to 48)
             20 STORE_NAME               2 (x)
             23 LOAD_NAME                2 (x)
             26 LOAD_CONST               1 (2)
             29 BINARY_MODULO       
             30 JUMP_IF_FALSE           11 (to 44)
             33 POP_TOP             
             34 LOAD_NAME                0 (_[1])
             37 LOAD_NAME                2 (x)
             40 LIST_APPEND         
             41 JUMP_ABSOLUTE           17
        >>   44 POP_TOP             
             45 JUMP_ABSOLUTE           17
        >>   48 DELETE_NAME              0 (_[1])
             51 PRINT_EXPR          
             52 LOAD_CONST               2 (None)
             55 RETURN_VALUE 

----------------------------------------------------------------------

Comment By: Collin Winter (collinwinter)
Date: 2006-03-03 07:49

Message:
Logged In: YES 
user_id=1344176

I forgot to mention: the patch also includes updates to
things like Lib/test/test_dis.py and Lib/opcode.py to make
them work with the updated LIST_APPEND opcode and code
generation.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1442442&group_id=5470


More information about the Patches mailing list