From e7a388d1c7adf034feddc5ba10d6d8b0c70e5f60 Mon Sep 17 00:00:00 2001 From: "Sven M. Hallberg" <pesco@khjk.org> Date: Thu, 6 Jun 2013 13:01:54 +0200 Subject: [PATCH] move djbhash into general availability as h_djbhash --- src/backends/packrat.c | 10 +--------- src/datastructures.c | 11 ++++++++++- src/internal.h | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/backends/packrat.c b/src/backends/packrat.c index c5c9565f..8aa1f8ed 100644 --- a/src/backends/packrat.c +++ b/src/backends/packrat.c @@ -3,14 +3,6 @@ #include "../internal.h" #include "../parsers/parser_internal.h" -static uint32_t djbhash(const uint8_t *buf, size_t len) { - uint32_t hash = 5381; - while (len--) { - hash = hash * 33 + *buf++; - } - return hash; -} - // short-hand for constructing HCachedResult's static HCachedResult *cached_result(const HParseState *state, HParseResult *result) { HCachedResult *ret = a_new(HCachedResult, 1); @@ -214,7 +206,7 @@ void h_packrat_free(HParser *parser) { } static uint32_t cache_key_hash(const void* key) { - return djbhash(key, sizeof(HParserCacheKey)); + return h_djbhash(key, sizeof(HParserCacheKey)); } static bool cache_key_equal(const void* key1, const void* key2) { return memcmp(key1, key2, sizeof(HParserCacheKey)) == 0; diff --git a/src/datastructures.c b/src/datastructures.c index bd9b4ebe..730c6b99 100644 --- a/src/datastructures.c +++ b/src/datastructures.c @@ -329,6 +329,15 @@ bool h_eq_ptr(const void *p, const void *q) { } HHashValue h_hash_ptr(const void *p) { - // XXX just djbhash it + // XXX just djbhash it? it does make the benchmark ~7% slower. + //return h_djbhash((const uint8_t *)&p, sizeof(void *)); return (uintptr_t)p >> 4; } + +uint32_t h_djbhash(const uint8_t *buf, size_t len) { + uint32_t hash = 5381; + while (len--) { + hash = hash * 33 + *buf++; + } + return hash; +} diff --git a/src/internal.h b/src/internal.h index 11836826..2f3018df 100644 --- a/src/internal.h +++ b/src/internal.h @@ -276,6 +276,7 @@ bool h_hashset_equal(const HHashSet *a, const HHashSet *b); bool h_eq_ptr(const void *p, const void *q); HHashValue h_hash_ptr(const void *p); +uint32_t h_djbhash(const uint8_t *buf, size_t len); typedef struct HCFSequence_ HCFSequence; -- GitLab