On Sun, Oct 25, 2009 at 2:39 PM, Sturla Molden <stu...@molden.no> wrote: > I just realized the Python does not provide a fast way of popping an > item from a list, just append. I.e. there is a PyList_Append, but no > PyList_Pop. And in Python's code for _listobject.c, the function listpop > is declared static. When using a list as a stack (a common case for > Python lists), a fast pop is just as important as a fast append. >
Indeed. +1 for the idea, but... > Since the function listpop is static, the fastest way to pop a list is > to grab the method named "pop" from the PyTypeObject. This might not be > obvious to everyone, so I think it is an optimization that Cython should do. > -0.5 for the implementation... The hack is cute, but still have to make a listpop(self,args), were you need to create a tuple to make the call... I think Cython could generate a custom, inline function implementing l.pop([n]) and fast dispatch to it if the object is exactly a list ... > > > cdef extern from "Python.h": > > ctypedef object (*PyCFunction)(object self, object args) > > ctypedef struct PyMethodDef: > char *ml_name > PyCFunction ml_meth > > ctypedef struct PyTypeObject: > PyMethodDef *tp_methods > > PyTypeObject PyList_Type > > > cdef PyCFunction __listpop = NULL > > > cpdef listpop(list self, Py_ssize_t n=-1): > > global __listpop > > cdef PyMethodDef *ptr > cdef int ok = 0 > > if not __listpop: > ptr = PyList_Type.tp_methods; > while ptr: > if str(ptr[0].ml_name) == "pop": > __listpop = ptr.ml_meth > ok = 1 > break > ptr += 1 > if not ok: > raise ValueError, \ > 'Could not find method "pop" in list type object' > return __listpop(self, (int(n),)) > > > > _______________________________________________ > Cython-dev mailing list > Cython-dev@codespeak.net > http://codespeak.net/mailman/listinfo/cython-dev > -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 _______________________________________________ Cython-dev mailing list Cython-dev@codespeak.net http://codespeak.net/mailman/listinfo/cython-dev