From 76a699701cda144af96f77061b036c7afc10fce4 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 18 Sep 2012 04:34:38 +0300 Subject: [PATCH] Enhanced string hash to permit calculating string length on the same call - String hashing needs to walk the entire string anyhow, might as well take advantage of this and have it return the string length to avoid having to separately call strlen() in the cases where this matters. - Move the implementation into rpmstrpool.c for inlining possibilities, rstrhash() is now just a wrapper to rstrlenhash(). The generic hash implementation could not take advantage of this anyway really. --- rpmio/rpmstring.c | 17 ----------------- rpmio/rpmstrpool.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/rpmio/rpmstring.c b/rpmio/rpmstring.c index d6bc0bf06..0022b6075 100644 --- a/rpmio/rpmstring.c +++ b/rpmio/rpmstring.c @@ -190,20 +190,3 @@ size_t rstrlcpy(char *dest, const char *src, size_t n) return s - src - 1; /* count does not include NUL */ } - -unsigned int rstrhash(const char * string) -{ - /* Jenkins One-at-a-time hash */ - unsigned int hash = 0xe4721b68; - - while (*string != '\0') { - hash += *string; - hash += (hash << 10); - hash ^= (hash >> 6); - string++; - } - hash += (hash << 3); - hash ^= (hash >> 11); - hash += (hash << 15); - return hash; -} diff --git a/rpmio/rpmstrpool.c b/rpmio/rpmstrpool.c index dc208a622..0d832f28e 100644 --- a/rpmio/rpmstrpool.c +++ b/rpmio/rpmstrpool.c @@ -38,6 +38,34 @@ struct rpmstrPool_s { int nrefs; /* refcount */ }; +/* calculate hash and string length on at once */ +static inline unsigned int rstrlenhash(const char * str, size_t * len) +{ + /* Jenkins One-at-a-time hash */ + unsigned int hash = 0xe4721b68; + const char * s = str; + + while (*s != '\0') { + hash += *s; + hash += (hash << 10); + hash ^= (hash >> 6); + s++; + } + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + + if (len) + *len = (s - str); + + return hash; +} + +unsigned int rstrhash(const char * string) +{ + return rstrlenhash(string, NULL); +} + static poolHash poolHashCreate(int numBuckets) { poolHash ht;