summaryrefslogtreecommitdiffstats
path: root/Include/stringobject.h
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-01-18 07:53:23 (GMT)
committerGuido van Rossum <guido@python.org>1997-01-18 07:53:23 (GMT)
commit1e6e9a23688129b5b29fb8e8137f17617d22f483 (patch)
tree127bb53165c7abbad01669876df73d2ff493471f /Include/stringobject.h
parent3dfd53b4c83cc42e694a7c6e66678f2e7b0b1613 (diff)
downloadcpython-1e6e9a23688129b5b29fb8e8137f17617d22f483.zip
cpython-1e6e9a23688129b5b29fb8e8137f17617d22f483.tar.gz
cpython-1e6e9a23688129b5b29fb8e8137f17617d22f483.tar.bz2
Two speedup hacks. Caching the hash saves recalculation of a string's
hash value. Interning strings (which requires hash caching) tries to ensure that only one string object with a given value exists, so equality tests are one pointer comparison. Together, these can speed the interpreter up by as much as 20%. Each costs the size of a long or pointer per string object. In addition, interned strings live until the end of times. If you are concerned about memory footprint, simply comment the #define out here (and rebuild everything!).
Diffstat (limited to 'Include/stringobject.h')
-rw-r--r--Include/stringobject.h26
1 files changed, 23 insertions, 3 deletions
diff --git a/Include/stringobject.h b/Include/stringobject.h
index c2565d8..8466821 100644
--- a/Include/stringobject.h
+++ b/Include/stringobject.h
@@ -52,16 +52,28 @@ variant that assumes a zero-terminated string. Note that none of the
functions should be applied to nil objects.
*/
-/* NB The type is revealed here only because it is used in dictobject.c */
-
-/* Take this out to save 4 bytes per string object and to lose 2% speedup */
+/* Two speedup hacks. Caching the hash saves recalculation of a
+ string's hash value. Interning strings (which requires hash
+ caching) tries to ensure that only one string object with a given
+ value exists, so equality tests are one pointer comparison.
+ Together, these can speed the interpreter up by as much as 20%.
+ Each costs the size of a long or pointer per string object. In
+ addition, interned strings live until the end of times. If you are
+ concerned about memory footprint, simply comment the #define out
+ here (and rebuild everything!). */
#define CACHE_HASH
+#ifdef CACHE_HASH
+#define INTERN_STRINGS
+#endif
typedef struct {
PyObject_VAR_HEAD
#ifdef CACHE_HASH
long ob_shash;
#endif
+#ifdef INTERN_STRINGS
+ PyObject *ob_sinterned;
+#endif
char ob_sval[1];
} PyStringObject;
@@ -78,6 +90,14 @@ extern void PyString_ConcatAndDel Py_PROTO((PyObject **, PyObject *));
extern int _PyString_Resize Py_PROTO((PyObject **, int));
extern PyObject *PyString_Format Py_PROTO((PyObject *, PyObject *));
+#ifdef INTERN_STRINGS
+extern void PyString_InternInPlace Py_PROTO((PyObject **));
+extern PyObject *PyString_InternFromString Py_PROTO((const char *));
+#else
+#define PyString_InternInPlace(p)
+#define PyString_InternFromString(cp) PyString_FromString(cp)
+#endif
+
/* Macro, trading safety for speed */
#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)