Mercurial > cpython-lru_cache-weakref
diff Objects/stringobject.c @ 17710:6199e5bd370b legacy-trunk
Intern 1-character strings as soon as they're created. As-is, they aren't
interned when created, so the cached versions generally aren't ever
interned. With the patch, the
Py_INCREF(t);
*p = t;
Py_DECREF(s);
return;
indirection block in PyString_InternInPlace() is never executed during a
full run of the test suite, but was executed very many times before. So
I'm trading more work when creating one-character strings for doing less
work later. Note that the "more work" here can happen at most 256 times
per program run, so it's trivial. The same reasoning accounts for the
patch's simplification of string_item (the new version can call
PyString_FromStringAndSize() no more than 256 times per run, so there's
no point to inlining that stuff -- if we were serious about saving time
here, we'd pre-initialize the characters vector so that no runtime testing
at all was needed!).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Tue, 08 May 2001 22:33:50 +0000 |
| parents | 9e093bf8f6a9 |
| children | 350be19ca6a5 |
line wrap: on
line diff
--- a/Objects/stringobject.c Tue May 08 15:43:37 2001 +0000 +++ b/Objects/stringobject.c Tue May 08 22:33:50 2001 +0000 @@ -36,7 +36,7 @@ PyObject * PyString_FromStringAndSize(const char *str, int size) { - register PyStringObject *op; + PyStringObject *op; #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0 && (op = nullstring) != NULL) { #ifdef COUNT_ALLOCS @@ -73,9 +73,11 @@ op->ob_sval[size] = '\0'; #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0) { + PyString_InternInPlace(&(PyObject *)op); nullstring = op; Py_INCREF(op); } else if (size == 1 && str != NULL) { + PyString_InternInPlace(&(PyObject *)op); characters[*str & UCHAR_MAX] = op; Py_INCREF(op); } @@ -87,7 +89,7 @@ PyString_FromString(const char *str) { register size_t size = strlen(str); - register PyStringObject *op; + PyStringObject *op; if (size > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "string is too long for a Python string"); @@ -125,9 +127,11 @@ strcpy(op->ob_sval, str); #ifndef DONT_SHARE_SHORT_STRINGS if (size == 0) { + PyString_InternInPlace(&(PyObject *)op); nullstring = op; Py_INCREF(op); } else if (size == 1) { + PyString_InternInPlace(&(PyObject *)op); characters[*str & UCHAR_MAX] = op; Py_INCREF(op); } @@ -551,24 +555,17 @@ { int c; PyObject *v; + char *pchar; if (i < 0 || i >= a->ob_size) { PyErr_SetString(PyExc_IndexError, "string index out of range"); return NULL; } - c = a->ob_sval[i] & UCHAR_MAX; + pchar = a->ob_sval + i; + c = *pchar & UCHAR_MAX; v = (PyObject *) characters[c]; -#ifdef COUNT_ALLOCS - if (v != NULL) - one_strings++; -#endif - if (v == NULL) { - v = PyString_FromStringAndSize((char *)NULL, 1); - if (v == NULL) - return NULL; - characters[c] = (PyStringObject *) v; - ((PyStringObject *)v)->ob_sval[0] = c; - } - Py_INCREF(v); + if (v == NULL) + v = PyString_FromStringAndSize(pchar, 1); + Py_XINCREF(v); return v; }
