This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Looping Optimization
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: loewis Nosy List: loewis, rhettinger
Priority: normal Keywords: patch

Created on 2002-05-05 03:35 by rhettinger, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
iter3.diff rhettinger, 2002-05-06 17:29 Revised patch. Added BadInternalCall
Messages (8)
msg39865 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-05 03:35
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.
msg39866 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-06 08:42
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.
msg39867 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-06 16:27
Logged In: YES 
user_id=80475

Done.

Added news and docs.

Instead of raising an exception, converted PySequence_Check 
to an assertion because it is a pre-condition of the 
function and has already been checked by PyObject_GetIter.  
The only way for the assertion to fail is if an extension 
writer by-passes _GetIter and directly calls PySeqIter_New 
with a non-sequence argument.
msg39868 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-06 16:56
Logged In: YES 
user_id=21627

I'm not sure what the convention for assertions is; the
alternative is to raise BadInternalCall.

If I would have to define a convention, it would go like
this: If this can be called incorrectly by extension
modules, it's a bad internal call; if the only callers would
be the core, it's an assert. It is also an assert if you can
prove that it can never occur with the current code base.
msg39869 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-06 17:29
Logged In: YES 
user_id=80475

Agreed.  Replaced assertion with BadInternalCall.
msg39870 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-07 18:02
Logged In: YES 
user_id=21627

It appears that this code might still return NULL without
setting an exception, if it_index is larger then the size.
msg39871 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-07 20:23
Logged In: YES 
user_id=80475

This is the current and documented behavior of the next 
routine for iterobjects.  From the docs, "If the object is 
an iterator, this retrieves the next value from the 
iteration, and returns NULL with no exception set if there 
are no remaining items".

Also, note the existing special case for lists has 
identical coding and behavior.  

Also, compare to the code fragment below it, for the 
general case, which explicitly calls PyErr_Clear() and then 
returns NULL.

IOW, I believe the code is correct as it stands and is 
ready to be loaded to CVS.
msg39872 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-08 08:45
Logged In: YES 
user_id=21627

Thanks for the patch; applied as

iterobject.c 1.9
NEWS 1.404
abstract.h 2.44
abstract.tex 1.14
History
Date User Action Args
2022-04-10 16:05:17adminsetgithub: 36554
2002-05-05 03:35:25rhettingercreate