[Python-checkins] python/dist/src/Objects stringobject.c,2.201,2.202

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Mon, 06 Jan 2003 02:33:59 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv28415

Modified Files:
	stringobject.c 
Log Message:
Optimize string_repeat.

Christian Tismer pointed out the high cost of the loop overhead and
function call overhead for 'c' * n where n is large.  Accordingly,
the new code only makes lg2(n) loops.

Interestingly, 'c' * 1000 * 1000 ran a bit faster with old code.  At some
point, the loop and function call overhead became cheaper than invalidating
the cache with lengthy memcpys.  But for more typical sizes of n, the new
code runs much faster and for larger values of n it runs only a bit slower.



Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.201
retrieving revision 2.202
diff -C2 -d -r2.201 -r2.202
*** stringobject.c	29 Dec 2002 19:44:06 -0000	2.201
--- stringobject.c	6 Jan 2003 10:33:56 -0000	2.202
***************
*** 934,937 ****
--- 934,938 ----
  {
  	register int i;
+ 	register int j;
  	register int size;
  	register PyStringObject *op;
***************
*** 966,971 ****
  	op->ob_shash = -1;
  	op->ob_sstate = SSTATE_NOT_INTERNED;
! 	for (i = 0; i < size; i += a->ob_size)
! 		memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
  	op->ob_sval[size] = '\0';
  	return (PyObject *) op;
--- 967,980 ----
  	op->ob_shash = -1;
  	op->ob_sstate = SSTATE_NOT_INTERNED;
! 	i = 0;
! 	if (i < size) {
! 		memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
! 		i = (int) a->ob_size;
! 	}
! 	while (i < size) {
! 		j = (i <= size-i)  ?  i  :  size-i;
! 		memcpy(op->ob_sval+i, op->ob_sval, j);
! 		i += j;
! 	}
  	op->ob_sval[size] = '\0';
  	return (PyObject *) op;