[Python-Dev] Q: why doesn't list.extend use the sequence interface?

M.-A. Lemburg mal@lemburg.com
Sat, 17 Jun 2000 16:18:26 +0200


Fredrik Lundh wrote:
> 
> the eff-bot wrote:
> 
> > the first part is trivial; just add something like this to
> > Objects/abstract.c:
> >
> > PyObject*
> > PySequence_TupleOrList(PyObject* seq)
> > ...
> >
> > the second parts is harder -- and that's of course where the
> > real performance boost comes from.  as far as I'm aware, very
> > few C compilers can "specialize" loops for you...
> 
> in the same test setup, moving the PyList_Check inside the
> loop gives a 25-30% slowdown (or to look at it from the other
> side, 3 times instead of 4 times faster than naive code).
> 
> a compromise solution could be to combine the above function
> with a macro:
> 
>     #define PySequence_TupleOrList_GET_ITEM(seq, index)\
>         (PyList_Check(seq) ? PyList_GET_ITEM(seq, index) :\
>         PyTuple_GET_ITEM(seq, index)
> 
> (anyone got a better name? ;-)
> 
> standard usage:
> 
>     seq = PySequence_TupleOrList(seq_in);
> 
>     size = PyObject_Length(seq);
> 
>     for (i = 0; i < size; i++) {
>         PyObject* obj = PySequence_TupleOrList_GET_ITEM(seq, i);
>         ...
>     }
> 
> comments?

The above looks a lot like an iterator... how about
providing a standard PySequence_Iterate(obj, callback)
with the callback being called for every element of the
sequence ?!

The iterator could then be optimized for lists and tuples.
Not as fast as inlining, but a more generic solution...

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/