[Python-checkins] r59513 - python/trunk/Objects/listobject.c

raymond.hettinger python-checkins at python.org
Sat Dec 15 01:07:25 CET 2007


Author: raymond.hettinger
Date: Sat Dec 15 01:07:25 2007
New Revision: 59513

Modified:
   python/trunk/Objects/listobject.c
Log:
Optimize PyList_AsTuple(). Improve cache performance by doing the
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).



Modified: python/trunk/Objects/listobject.c
==============================================================================
--- python/trunk/Objects/listobject.c	(original)
+++ python/trunk/Objects/listobject.c	Sat Dec 15 01:07:25 2007
@@ -2186,7 +2186,7 @@
 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 @@
 	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;
 }


More information about the Python-checkins mailing list