[Python-Dev] proposal: core support for "fast" sequence iteration
Fredrik Lundh
Fredrik Lundh" <effbot@telia.com
Sat, 17 Jun 2000 17:44:58 +0200
mark wrote:
> Its a fair bit of code to duplicate everywhere you want the speed =
increase.
> How can we add a similar scheme to the core, so the changes people =
need to
> make are trivial (to the point where the change is zero!)?
okay, here's my current proposal:
PyObject* PySequence_Fast(PyObject *o)=20
Return value: New reference.=20
Returns the o as a tuple or a list on success, and
NULL on failure. If o doesn't have the right type,
it is converted to a tuple using PySequence_Tuple.
This is equivalent to the following Python code:
if type(o) in (ListType, TupleType):
return o
return tuple(o)
This function is intended to be used together with
PySequence_Fast_GET_ITEM, for functions that need
to loop over a read-only sequence as fast as they
possibly can, while still supporting any object that
implements the sequence protocol.
PyObject* PySequence_Fast_GET_ITEM(PyObject *seq, int i)
Return value: Borrowed reference.=20
Returns the object at position i, from the sequence
seq (which must be a list or a tuple). This is a
macro, and has no error checking.
If you need error checking, use PySequence_GetItem.
for the rationale, see my earlier posts in the "list.extend" thread.
unless somebody comes up with a more efficient solution, I'll wrap
this up together with the list.extend patch (first thing tomorrow).
</F>