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

Jeremy Hylton jeremy@beopen.com
Fri, 16 Jun 2000 12:45:07 -0400 (EDT)


>>>>> "FL" == Fredrik Lundh <effbot@telia.com> writes:

  FL> has anyone benchmarked the abstract sequence API?  how much
  FL> slower is it?  would it be a good idea to treat lists as a
  FL> special case, to avoid slowing down existing code?

The abstract interface is fairly expensive.  The current code uses
PyList_GET_ITEM to access the elements of the list.  The abstract
interface adds two function calls (PySequence_GetItem,
PyList_GetItem).  The first function just does a couple of sanity
checks (verify arg is sequence & that it has get_item).  The second
function verifies that the arg is a list, then does a bounds check.
None of the work that either of these functions does is necessary for
the list case!

In the extended call syntax implementation, I used PySequence_Tuple to
handle non-standard sequences.  Once converted to a tuple, they can be
processed using the macros.  If you support user-defined sequences,
the sanity checking can get pretty hairy if you don't coerce to a
tuple; e.g. the PySequence_Length method can lie!  

I don't remember considering the cost of allocating the new tuple,
which is only used internally.

Jeremy