diff -r 9214f8440c44 Objects/object.c --- a/Objects/object.c Sat Nov 10 14:52:10 2012 +0000 +++ b/Objects/object.c Sun Nov 11 00:38:31 2012 +0200 @@ -771,7 +771,19 @@ } x = (Py_uhash_t) _Py_HashSecret.prefix; x ^= (Py_uhash_t) *p << 7; - for (i = 0; i < len; i++) + i = 0; +#define HASH_BLOCK size_t + if (_Py_IS_ALIGNED(p, sizeof(HASH_BLOCK)) && len >= sizeof(HASH_BLOCK)) { + HASH_BLOCK *q = (HASH_BLOCK *)p; + HASH_BLOCK *e = (HASH_BLOCK *)_Py_ALIGN_DOWN(p + len, + sizeof(HASH_BLOCK)); + while (q < e) + x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *q++; + i = (unsigned char *)q - p; + p = (unsigned char *)q; + } +#undef HASH_BLOCK + for (; i < len; i++) x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++; x ^= (Py_uhash_t) len; x ^= (Py_uhash_t) _Py_HashSecret.suffix; diff -r 9214f8440c44 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sat Nov 10 14:52:10 2012 +0000 +++ b/Objects/unicodeobject.c Sun Nov 11 00:38:31 2012 +0200 @@ -10880,7 +10880,6 @@ static Py_hash_t unicode_hash(PyObject *self) { - Py_ssize_t len; Py_uhash_t x; #ifdef Py_DEBUG @@ -10890,48 +10889,8 @@ return _PyUnicode_HASH(self); if (PyUnicode_READY(self) == -1) return -1; - len = PyUnicode_GET_LENGTH(self); - /* - We make the hash of the empty string be 0, rather than using - (prefix ^ suffix), since this slightly obfuscates the hash secret - */ - if (len == 0) { - _PyUnicode_HASH(self) = 0; - return 0; - } - - /* The hash function as a macro, gets expanded three times below. */ -#define HASH(P) \ - x ^= (Py_uhash_t) *P << 7; \ - while (--len >= 0) \ - x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ - - x = (Py_uhash_t) _Py_HashSecret.prefix; - switch (PyUnicode_KIND(self)) { - case PyUnicode_1BYTE_KIND: { - const unsigned char *c = PyUnicode_1BYTE_DATA(self); - HASH(c); - break; - } - case PyUnicode_2BYTE_KIND: { - const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); - HASH(s); - break; - } - default: { - Py_UCS4 *l; - assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && - "Impossible switch case in unicode_hash"); - l = PyUnicode_4BYTE_DATA(self); - HASH(l); - break; - } - } - x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); - x ^= (Py_uhash_t) _Py_HashSecret.suffix; - - if (x == -1) - x = -2; + x = _Py_HashBytes(PyUnicode_DATA(self), + PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); _PyUnicode_HASH(self) = x; return x; }