From 4d0277233e184dabefdbc966f5764034737cd2b9 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 18 Sep 2007 04:30:42 +0000 Subject: Micro optimizations after staring at gprof output for a while. --- Objects/dictobject.c | 13 +++++-------- Objects/unicodeobject.c | 7 ++++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 539d734..96089a1 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -566,8 +566,8 @@ PyDict_GetItem(PyObject *op, PyObject *key) PyThreadState *tstate; if (!PyDict_Check(op)) return NULL; - if (!PyString_CheckExact(key) || - (hash = ((PyStringObject *) key)->ob_shash) == -1) + if (!PyUnicode_CheckExact(key) || + (hash = ((PyUnicodeObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) { @@ -650,12 +650,9 @@ PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) assert(key); assert(value); mp = (dictobject *)op; - if (PyString_CheckExact(key)) { - hash = ((PyStringObject *)key)->ob_shash; - if (hash == -1) - hash = PyObject_Hash(key); - } - else { + if (!PyUnicode_CheckExact(key) || + (hash = ((PyUnicodeObject *) key)->hash) == -1) + { hash = PyObject_Hash(key); if (hash == -1) return -1; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d52c080..140ffaf 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6597,9 +6597,10 @@ unicode_hash(PyUnicodeObject *self) /* Since Unicode objects compare equal to their UTF-8 string counterparts, we hash the UTF-8 string. */ PyObject *v = _PyUnicode_AsDefaultEncodedString((PyObject*)self, NULL); - long x = PyObject_Hash(v); - self->hash = x; - return x; + if (v == NULL) + return -1; + assert(PyString_CheckExact(v)); + return self->hash = v->ob_type->tp_hash(v); } } -- cgit v0.12