[Python-checkins] python/dist/src/Objects listobject.c,2.184,2.185

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Mon Mar 8 00:56:18 EST 2004


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29902

Modified Files:
	listobject.c 
Log Message:
Small optimizations for list_slice() and list_extend_internal().

* Using addition instead of substraction on array indices allows the 
  compiler to use a fast addressing mode.  Saves about 10%.

* Using PyTuple_GET_ITEM and PyList_SET_ITEM is about 7% faster than
  PySequenceFast_GET_ITEM which has to make a list check on every pass.



Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.184
retrieving revision 2.185
diff -C2 -d -r2.184 -r2.185
*** listobject.c	19 Feb 2004 06:12:06 -0000	2.184
--- listobject.c	8 Mar 2004 05:56:15 -0000	2.185
***************
*** 343,347 ****
  {
  	PyListObject *np;
! 	int i;
  	if (ilow < 0)
  		ilow = 0;
--- 343,347 ----
  {
  	PyListObject *np;
! 	int i, len;
  	if (ilow < 0)
  		ilow = 0;
***************
*** 352,362 ****
  	else if (ihigh > a->ob_size)
  		ihigh = a->ob_size;
! 	np = (PyListObject *) PyList_New(ihigh - ilow);
  	if (np == NULL)
  		return NULL;
! 	for (i = ilow; i < ihigh; i++) {
! 		PyObject *v = a->ob_item[i];
  		Py_INCREF(v);
! 		np->ob_item[i - ilow] = v;
  	}
  	return (PyObject *)np;
--- 352,364 ----
  	else if (ihigh > a->ob_size)
  		ihigh = a->ob_size;
! 	len = ihigh - ilow;
! 	np = (PyListObject *) PyList_New(len);
  	if (np == NULL)
  		return NULL;
! 
! 	for (i = 0; i < len; i++) {
! 		PyObject *v = a->ob_item[i+ilow];
  		Py_INCREF(v);
! 		np->ob_item[i] = v;
  	}
  	return (PyObject *)np;
***************
*** 677,684 ****
  
  	/* populate the end of self with b's items */
! 	for (i = 0; i < blen; i++) {
! 		PyObject *o = PySequence_Fast_GET_ITEM(b, i);
! 		Py_INCREF(o);
! 		PyList_SET_ITEM(self, i+selflen, o);
  	}
  	Py_DECREF(b);
--- 679,695 ----
  
  	/* populate the end of self with b's items */
! 	if (PyList_Check(b)) {
! 		for (i = 0; i < blen; i++) {
! 			PyObject *o = PyList_GET_ITEM(b, i);
! 			Py_INCREF(o);
! 			PyList_SET_ITEM(self, i+selflen, o);
! 		}
! 	} else {
! 		assert (PyTuple_Check(b));
! 		for (i = 0; i < blen; i++) {
! 			PyObject *o = PyTuple_GET_ITEM(b, i);
! 			Py_INCREF(o);
! 			PyList_SET_ITEM(self, i+selflen, o);
! 		}
  	}
  	Py_DECREF(b);




More information about the Python-checkins mailing list