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

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Fri, 23 Aug 2002 23:57:51 -0700


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

Modified Files:
	stringobject.c 
Log Message:
string_contains(): speed up by avoiding function calls where
possible.  This always called PyUnicode_Check() and PyString_Check(),
at least one of which would call PyType_IsSubtype().  Also, this would
call PyString_Size() on known string objects.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.184
retrieving revision 2.185
diff -C2 -d -r2.184 -r2.185
*** stringobject.c	23 Aug 2002 18:21:28 -0000	2.184
--- stringobject.c	24 Aug 2002 06:57:49 -0000	2.185
***************
*** 992,1005 ****
  	const char *lhs, *rhs, *end;
  	int size;
  #ifdef Py_USING_UNICODE
! 	if (PyUnicode_Check(el))
! 		return PyUnicode_Contains(a, el);
  #endif
! 	if (!PyString_Check(el)) {
! 		PyErr_SetString(PyExc_TypeError,
! 			      "'in <string>' requires string as left operand");
! 		return -1;
  	}
! 	size = PyString_Size(el);
  	rhs = PyString_AS_STRING(el);
  	lhs = PyString_AS_STRING(a);
--- 992,1008 ----
  	const char *lhs, *rhs, *end;
  	int size;
+ 
+ 	if (!PyString_CheckExact(el)) {
  #ifdef Py_USING_UNICODE
! 		if (PyUnicode_Check(el))
! 			return PyUnicode_Contains(a, el);
  #endif
! 		if (!PyString_Check(el)) {
! 			PyErr_SetString(PyExc_TypeError,
! 			    "'in <string>' requires string as left operand");
! 			return -1;
! 		}
  	}
! 	size = PyString_GET_SIZE(el);
  	rhs = PyString_AS_STRING(el);
  	lhs = PyString_AS_STRING(a);
***************
*** 1007,1013 ****
  	/* optimize for a single character */
  	if (size == 1)
! 		return memchr(lhs, *rhs, PyString_Size(a)) != NULL;
  
! 	end = lhs + (PyString_Size(a) - size);
  	while (lhs <= end) {
  		if (memcmp(lhs++, rhs, size) == 0)
--- 1010,1016 ----
  	/* optimize for a single character */
  	if (size == 1)
! 		return memchr(lhs, *rhs, PyString_GET_SIZE(a)) != NULL;
  
! 	end = lhs + (PyString_GET_SIZE(a) - size);
  	while (lhs <= end) {
  		if (memcmp(lhs++, rhs, size) == 0)