[Python-checkins] CVS: python/dist/src/Objects unicodeobject.c,2.82,2.83

Martin v. L?wis loewis@users.sourceforge.net
Wed, 18 Apr 2001 05:49:17 -0700


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

Modified Files:
	unicodeobject.c 
Log Message:
Patch #416953: Cache ASCII characters to speed up ASCII decoding.


Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.82
retrieving revision 2.83
diff -C2 -r2.82 -r2.83
*** unicodeobject.c	2001/04/12 18:38:48	2.82
--- unicodeobject.c	2001/04/18 12:49:15	2.83
***************
*** 91,94 ****
--- 91,96 ----
  static int unicode_freelist_size;
  
+ static PyUnicodeObject *unicode_ascii[128];
+ 
  /* Default encoding to use and assume when NULL is passed as encoding
     parameter; it is initialized by _PyUnicode_Init().
***************
*** 252,255 ****
--- 254,270 ----
      PyUnicodeObject *unicode;
  
+     if (size == 1 && *u < 128) {
+ 	    unicode = unicode_ascii[*u];
+ 	    if (!unicode) {
+ 		    unicode = _PyUnicode_New(1);
+ 		    unicode->str[0] = *u;
+ 		    if (!unicode)
+ 			    return NULL;
+ 		    unicode_ascii[*u] = unicode;
+ 	    }
+ 	    Py_INCREF(unicode);
+ 	    return (PyObject*)unicode;
+     }
+ 
      unicode = _PyUnicode_New(size);
      if (!unicode)
***************
*** 1656,1659 ****
--- 1671,1679 ----
      PyUnicodeObject *v;
      Py_UNICODE *p;
+ 
+     if (size == 1 && *(unsigned char*)s < 128) {
+ 	    Py_UNICODE r = *(unsigned char*)s;
+ 	    return PyUnicode_FromUnicode(&r, 1);
+     }
      
      /* ASCII is equivalent to the first 128 ordinals in Unicode. */
***************
*** 5190,5193 ****
--- 5210,5215 ----
  void _PyUnicode_Init(void)
  {
+     int i;
+ 
      /* Doublecheck the configuration... */
      if (sizeof(Py_UNICODE) != 2)
***************
*** 5200,5203 ****
--- 5222,5228 ----
      unicode_empty = _PyUnicode_New(0);
      strcpy(unicode_default_encoding, "ascii");
+ 
+     for (i = 0; i < 128; i++)
+ 	unicode_ascii[i] = NULL;
  }
  
***************
*** 5208,5214 ****
--- 5233,5247 ----
  {
      PyUnicodeObject *u;
+     int i;
  
      Py_XDECREF(unicode_empty);
      unicode_empty = NULL;
+ 
+     for (i = 0; i < 128; i++) {
+ 	if (unicode_ascii[i]) {
+ 	    Py_DECREF(unicode_ascii[i]);
+ 	    unicode_ascii[i] = NULL;
+ 	}
+     }
  
      for (u = unicode_freelist; u != NULL;) {