Skip to content

Commit

Permalink
pythongh-115704: Improve DJBX33A hash algorithm
Browse files Browse the repository at this point in the history
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
  • Loading branch information
PeterYang12 committed Aug 27, 2024
1 parent fe85a82 commit 0364811
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions Python/pyhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,24 @@ _Py_HashBytes(const void *src, Py_ssize_t len)
const unsigned char *p = src;
hash = 5381; /* DJBX33A starts with 5381 */

switch(len) {
/* ((hash << 5) + hash) + *p == hash * 33 + *p */
case 7: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH;
case 6: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH;
case 5: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH;
case 4: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH;
case 3: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH;
case 2: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH;
case 1: hash = ((hash << 5) + hash) + *p++; break;
default:
Py_UNREACHABLE();
if (len >= 4) {
/* 1185921 = 33^4, 35937 = 33^3, 1089 = 33^2 */
hash = hash * 1185921 + p[0] * 35937 + p[1] * 1089 +
p[2] * 33 + p[3];
len -= 4;
p += 4;
}
else if (len >= 2) {
if (len > 2) {
hash = hash * 35937 + p[0] * 1089 + p[1] * 33 + p[2];
}
else {
hash = hash * 1089 + p[0] * 33 + p[1];
}
}
else if (len != 0 ) {
hash = hash * 33 + *p;
}
hash ^= len;
hash ^= (Py_uhash_t) _Py_HashSecret.djbx33a.suffix;
x = (Py_hash_t)hash;
Expand Down

0 comments on commit 0364811

Please sign in to comment.