[Python-checkins] python/dist/src/Python bltinmodule.c,2.263,2.264

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Fri, 16 Aug 2002 00:04:58 -0700


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

Modified Files:
	bltinmodule.c 
Log Message:
A nice little speed-up for filter():

- Use PyObject_Call() instead of PyEval_CallObject(), saves several
  layers of calls and checks.

- Pre-allocate the argument tuple rather than calling Py_BuildValue()
  each time round the loop.

- For filter(None, seq), avoid an INCREF and a DECREF.


Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.263
retrieving revision 2.264
diff -C2 -d -r2.263 -r2.264
*** bltinmodule.c	14 Aug 2002 15:46:02 -0000	2.263
--- bltinmodule.c	16 Aug 2002 07:04:56 -0000	2.264
***************
*** 123,127 ****
  builtin_filter(PyObject *self, PyObject *args)
  {
! 	PyObject *func, *seq, *result, *it;
  	int len;   /* guess for result list size */
  	register int j;
--- 123,127 ----
  builtin_filter(PyObject *self, PyObject *args)
  {
! 	PyObject *func, *seq, *result, *it, *arg;
  	int len;   /* guess for result list size */
  	register int j;
***************
*** 152,155 ****
--- 152,160 ----
  		len = 8;  /* arbitrary */
  
+ 	/* Pre-allocate argument list tuple. */
+ 	arg = PyTuple_New(1);
+ 	if (arg == NULL)
+ 		goto Fail_arg;
+ 
  	/* Get a result list. */
  	if (PyList_Check(seq) && seq->ob_refcnt == 1) {
***************
*** 167,171 ****
  	j = 0;
  	for (;;) {
! 		PyObject *item, *good;
  		int ok;
  
--- 172,176 ----
  	j = 0;
  	for (;;) {
! 		PyObject *item;
  		int ok;
  
***************
*** 178,199 ****
  
  		if (func == Py_None) {
! 			good = item;
! 			Py_INCREF(good);
  		}
  		else {
! 			PyObject *arg = Py_BuildValue("(O)", item);
! 			if (arg == NULL) {
! 				Py_DECREF(item);
! 				goto Fail_result_it;
! 			}
! 			good = PyEval_CallObject(func, arg);
! 			Py_DECREF(arg);
  			if (good == NULL) {
  				Py_DECREF(item);
  				goto Fail_result_it;
  			}
  		}
- 		ok = PyObject_IsTrue(good);
- 		Py_DECREF(good);
  		if (ok) {
  			if (j < len)
--- 183,200 ----
  
  		if (func == Py_None) {
! 			ok = PyObject_IsTrue(item);
  		}
  		else {
! 			PyObject *good;
! 			PyTuple_SET_ITEM(arg, 0, item);
! 			good = PyObject_Call(func, arg, NULL);
! 			PyTuple_SET_ITEM(arg, 0, NULL);
  			if (good == NULL) {
  				Py_DECREF(item);
  				goto Fail_result_it;
  			}
+ 			ok = PyObject_IsTrue(good);
+ 			Py_DECREF(good);
  		}
  		if (ok) {
  			if (j < len)
***************
*** 217,220 ****
--- 218,222 ----
  
  	Py_DECREF(it);
+ 	Py_DECREF(arg);
  	return result;
  
***************
*** 223,226 ****
--- 225,230 ----
  Fail_it:
  	Py_DECREF(it);
+ Fail_arg:
+ 	Py_DECREF(arg);
  	return NULL;
  }