diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2001-04-18 12:49:15 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2001-04-18 12:49:15 (GMT) |
commit | da3dc5b892af21c513781d8e725f0314d2a5171d (patch) | |
tree | 89636062bf8d8147d4d05050a6f653a268c06190 /Objects/unicodeobject.c | |
parent | f3848322fff5b4ffd53ca03e10446bf50abdc72d (diff) | |
download | cpython-da3dc5b892af21c513781d8e725f0314d2a5171d.zip cpython-da3dc5b892af21c513781d8e725f0314d2a5171d.tar.gz cpython-da3dc5b892af21c513781d8e725f0314d2a5171d.tar.bz2 |
Patch #416953: Cache ASCII characters to speed up ASCII decoding.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b623c20..d2aa08a 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -90,6 +90,8 @@ static PyUnicodeObject *unicode_empty; static PyUnicodeObject *unicode_freelist; 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(). @@ -251,6 +253,19 @@ PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, { 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) return NULL; @@ -1655,6 +1670,11 @@ PyObject *PyUnicode_DecodeASCII(const char *s, { 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. */ v = _PyUnicode_New(size); @@ -5189,6 +5209,8 @@ PyTypeObject PyUnicode_Type = { void _PyUnicode_Init(void) { + int i; + /* Doublecheck the configuration... */ if (sizeof(Py_UNICODE) != 2) Py_FatalError("Unicode configuration error: " @@ -5199,6 +5221,9 @@ void _PyUnicode_Init(void) unicode_freelist_size = 0; unicode_empty = _PyUnicode_New(0); strcpy(unicode_default_encoding, "ascii"); + + for (i = 0; i < 128; i++) + unicode_ascii[i] = NULL; } /* Finalize the Unicode implementation */ @@ -5207,10 +5232,18 @@ void _PyUnicode_Fini(void) { 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;) { PyUnicodeObject *v = u; u = *(PyUnicodeObject **)u; |