[Python-checkins] python/dist/src/Objects listobject.c,2.114,2.115

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 11 Jul 2002 14:46:18 -0700


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

Modified Files:
	listobject.c 
Log Message:
docompare():  Use PyTuple_New instead of Py_BuildValue to build compare's
arg tuple.  This was suggested on c.l.py but afraid I can't find the msg
again for proper attribution.  For

    list.sort(cmp)

where list is a list of random ints, and cmp is __builtin__.cmp, this
yields an overall 50-60% speedup on my Win2K box.  Of course this is a
best case, because the overhead of calling cmp relative to the cost of
actually comparing two ints is at an extreme.  Nevertheless it's huge
bang for the buck.  An additionak 20-30% can be bought by making the arg
tuple an immortal static (avoiding all but "the first" PyTuple_New), but
that's tricky to make correct since docompare needs to be reentrant.  So
this picks the cherry and leaves the pits for Fred <wink>.

Note that this makes no difference to the

    list.sort()

case; an arg tuple gets built only if the user specifies an explicit
sort function.


Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.114
retrieving revision 2.115
diff -C2 -d -r2.114 -r2.115
*** listobject.c	19 Jun 2002 15:44:15 -0000	2.114
--- listobject.c	11 Jul 2002 21:46:16 -0000	2.115
***************
*** 757,761 ****
  docompare(PyObject *x, PyObject *y, PyObject *compare)
  {
! 	PyObject *args, *res;
  	int i;
  
--- 757,762 ----
  docompare(PyObject *x, PyObject *y, PyObject *compare)
  {
! 	PyObject *res;
! 	PyObject *args;
  	int i;
  
***************
*** 773,779 ****
  	}
  
! 	args = Py_BuildValue("(OO)", x, y);
  	if (args == NULL)
  		return CMPERROR;
  	res = PyEval_CallObject(compare, args);
  	Py_DECREF(args);
--- 774,784 ----
  	}
  
! 	args = PyTuple_New(2);
  	if (args == NULL)
  		return CMPERROR;
+ 	Py_INCREF(x);
+ 	Py_INCREF(y);
+ 	PyTuple_SET_ITEM(args, 0, x);
+ 	PyTuple_SET_ITEM(args, 1, y);
  	res = PyEval_CallObject(compare, args);
  	Py_DECREF(args);