Skip to content

Commit

Permalink
gh-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 <[email protected]>
  • Loading branch information
PeterYang12 committed Feb 27, 2024
1 parent 6087315 commit d951214
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions Python/pyhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,28 @@ _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++; /* fallthrough */
case 6: hash = ((hash << 5) + hash) + *p++; /* fallthrough */
case 5: hash = ((hash << 5) + hash) + *p++; /* fallthrough */
case 4: hash = ((hash << 5) + hash) + *p++; /* fallthrough */
case 3: hash = ((hash << 5) + hash) + *p++; /* fallthrough */
case 2: hash = ((hash << 5) + hash) + *p++; /* fallthrough */
case 1: hash = ((hash << 5) + hash) + *p++; break;
default:
Py_UNREACHABLE();
if (len >= 4) {
hash = hash * 33 * 33 * 33 * 33 +
p[0] * 33 * 33 * 33 +
p[1] * 33 * 33 +
p[2] * 33 +
p[3];
len -= 4;
p += 4;
}
if (len >= 2) {
if (len > 2) {
hash = hash * 33 * 33 * 33 +
p[0] * 33 * 33 +
p[1] * 33 +
p[2];
}
else {
hash = hash * 33 * 33 + p[0] * 33 + p[1];
}
}
else if (len != 0 ) {
hash = hash * 33UL + *p;
}
hash ^= len;
hash ^= (Py_uhash_t) _Py_HashSecret.djbx33a.suffix;
Expand Down

0 comments on commit d951214

Please sign in to comment.