[Python-checkins] python/dist/src/Objects listobject.c,2.186,2.187

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Tue Mar 9 03:04:37 EST 2004


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

Modified Files:
	listobject.c 
Log Message:
Optimize slice assignments.

* Replace sprintf message with a constant message string -- this error
  message ran on every invocation except straight deletions but it was
  only needed when the rhs was not iterable.  The message was also
  out-of-date and did not reflect that iterable arguments were allowed.

* For inner loops that do not make ref count adjustments, use memmove()
  for fast copying and better readability.

* For inner loops that do make ref count adjustments, speed them up by
  factoring out the constant structure reference and using vitem[] instead.



Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.186
retrieving revision 2.187
diff -C2 -d -r2.186 -r2.187
*** listobject.c	8 Mar 2004 07:25:04 -0000	2.186
--- listobject.c	9 Mar 2004 08:04:33 -0000	2.187
***************
*** 461,464 ****
--- 461,465 ----
  	PyObject **recycle, **p;
  	PyObject **item;
+ 	PyObject **vitem = NULL;
  	PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
  	int n; /* Size of replacement list */
***************
*** 470,474 ****
  		n = 0;
  	else {
- 		char msg[256];
  		if (a == b) {
  			/* Special case "a[i:j] = a" -- copy b first */
--- 471,474 ----
***************
*** 481,493 ****
  			return ret;
  		}
! 
! 		PyOS_snprintf(msg, sizeof(msg),
! 			      "must assign sequence"
! 			      " (not \"%.200s\") to slice",
! 			      v->ob_type->tp_name);
! 		v_as_SF = PySequence_Fast(v, msg);
  		if(v_as_SF == NULL)
  			return -1;
  		n = PySequence_Fast_GET_SIZE(v_as_SF);
  	}
  	if (ilow < 0)
--- 481,494 ----
  			return ret;
  		}
! 		v_as_SF = PySequence_Fast(v, "can only assign an iterable");
  		if(v_as_SF == NULL)
  			return -1;
  		n = PySequence_Fast_GET_SIZE(v_as_SF);
+ 		if (PyList_Check(v_as_SF))
+ 			vitem = ((PyListObject *)v_as_SF)->ob_item;
+ 		else {
+ 			assert (PyTuple_Check(v_as_SF));
+ 			vitem = ((PyTupleObject *)v_as_SF)->ob_item;
+ 		}
  	}
  	if (ilow < 0)
***************
*** 511,519 ****
  		p = recycle = NULL;
  	if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
! 		for (k = ilow; k < ihigh; k++)
! 			*p++ = item[k];
  		if (d < 0) {
! 			for (/*k = ihigh*/; k < a->ob_size; k++)
! 				item[k+d] = item[k];
  			list_resize(a, a->ob_size + d);
  			item = a->ob_item;
--- 512,520 ----
  		p = recycle = NULL;
  	if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
! 		memmove(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *));
! 		p += ihigh - ilow;
  		if (d < 0) {
! 			memmove(&item[ihigh+d], &item[ihigh], 
! 				(a->ob_size - ihigh)*sizeof(PyObject *));
  			list_resize(a, a->ob_size + d);
  			item = a->ob_item;
***************
*** 528,538 ****
  		}
  		item = a->ob_item;
! 		for (k = s; --k >= ihigh; )
! 			item[k+d] = item[k];
! 		for (/*k = ihigh-1*/; k >= ilow; --k)
! 			*p++ = item[k];
  	}
  	for (k = 0; k < n; k++, ilow++) {
! 		PyObject *w = PySequence_Fast_GET_ITEM(v_as_SF, k);
  		Py_XINCREF(w);
  		item[ilow] = w;
--- 529,539 ----
  		}
  		item = a->ob_item;
! 		memmove(&item[ihigh+d], &item[ihigh],
! 			(s - ihigh)*sizeof(PyObject *));
! 		memmove(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *));
! 		p += ihigh - ilow;
  	}
  	for (k = 0; k < n; k++, ilow++) {
! 		PyObject *w = vitem[k];
  		Py_XINCREF(w);
  		item[ilow] = w;




More information about the Python-checkins mailing list