From 91b5f65fd5d8d8d5aabf3247effec4ec6b2a8a63 Mon Sep 17 00:00:00 2001 From: Andrea Shepard <andrea@special-circumstanc.es> Date: Sun, 12 Jan 2020 04:20:24 +0000 Subject: [PATCH] Unroll djbhash for ~25% speedup --- src/datastructures.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/datastructures.c b/src/datastructures.c index ba012b76..6971e0e0 100644 --- a/src/datastructures.c +++ b/src/datastructures.c @@ -437,11 +437,26 @@ HHashValue h_hash_ptr(const void *p) { } uint32_t h_djbhash(const uint8_t *buf, size_t len) { - uint32_t hash = 5381; + uint32_t h = 5381; + + while (len >= 16) { + h = h * 33 + buf[0]; h = h * 33 + buf[1]; + h = h * 33 + buf[2]; h = h * 33 + buf[3]; + h = h * 33 + buf[4]; h = h * 33 + buf[5]; + h = h * 33 + buf[6]; h = h * 33 + buf[7]; + h = h * 33 + buf[8]; h = h * 33 + buf[9]; + h = h * 33 + buf[10]; h = h * 33 + buf[11]; + h = h * 33 + buf[12]; h = h * 33 + buf[13]; + h = h * 33 + buf[14]; h = h * 33 + buf[15]; + len -= 16; + buf += 16; + } + while (len--) { - hash = hash * 33 + *buf++; + h = h * 33 + *buf++; } - return hash; + + return h; } void h_symbol_put(HParseState *state, const char* key, void *value) { -- GitLab