[Patches] [ python-Patches-552433 ] Looping Optimization

noreply@sourceforge.net noreply@sourceforge.net
Mon, 06 May 2002 01:42:20 -0700


Patches item #552433, was opened at 2002-05-05 05:35
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=552433&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Looping Optimization

Initial Comment:
Optimized the inner loop for iterobjects resulting in 
a 19% speed-up for tuples and a 15% improvement for 
strings and xrange objects.  Dictionaries and lists 
are unaffected (already handled as a special case or 
with their own iterators).

1. Replaced PyList_Check with PyList_CheckExact.  
Loses the special case handling for list subtypes but 
gains speed because PyList_Check called the slow 
function, PyType_IsSubtype.

2. Added special case handling for tuples.  Identical 
to the existing special case for lists (calls ob_size 
and ob->item[i] directly).

3. Replaced the call to PySequence_GetItem function 
with a macro for direct access to the sequence's 
sq_item slot.  Saves a function call, unnecessary 
(previously checked) checks for tp_as_sequence and 
sq_item, and an unnecessary check for negative indices 
(sq_item has its own checks).

This small patch speeds-up looping throughout Python 
whether it is called through list(seq), a for-loop, or 
a functional.

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

>Comment By: Martin v. Löwis (loewis)
Date: 2002-05-06 10:42

Message:
Logged In: YES 
user_id=21627

The patch looks good, but needs some minor corrections:

Returning NULL in response to a failed _Check is incorrect -
no exception is set at that point.

I think the addition of PySequence_ITEM needs to be
mentioned in the documentation and Misc/NEWS.

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

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