]> granicus.if.org Git - python/commitdiff
Optimize PyList_AsTuple(). Improve cache performance by doing the
authorRaymond Hettinger <python@rcn.com>
Sat, 15 Dec 2007 00:07:25 +0000 (00:07 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 15 Dec 2007 00:07:25 +0000 (00:07 +0000)
pointer copy and object increment in one pass.  For small lists,
save the overhead of the call to memcpy() -- this comes up in
calls like f(*listcomp).

Objects/listobject.c

index ca767da9dcc5f011605085bb83a08b6ae64cc965..3fa256ed64653bccc94507e127dda0cec69baa5b 100644 (file)
@@ -2186,7 +2186,7 @@ PyObject *
 PyList_AsTuple(PyObject *v)
 {
        PyObject *w;
-       PyObject **p;
+       PyObject **p, **q;
        Py_ssize_t n;
        if (v == NULL || !PyList_Check(v)) {
                PyErr_BadInternalCall();
@@ -2197,12 +2197,12 @@ PyList_AsTuple(PyObject *v)
        if (w == NULL)
                return NULL;
        p = ((PyTupleObject *)w)->ob_item;
-       memcpy((void *)p,
-              (void *)((PyListObject *)v)->ob_item,
-              n*sizeof(PyObject *));
+       q = ((PyListObject *)v)->ob_item;
        while (--n >= 0) {
-               Py_INCREF(*p);
+               Py_INCREF(*q);
+               *p = *q;
                p++;
+               q++;
        }
        return w;
 }