switch BLI_ghashutil_strhash() to "djb" hash (as used by glib),

Gives approx 10% speedup in my own micro-benchmark looking up operators.
This commit is contained in:
Campbell Barton
2012-12-11 13:57:58 +00:00
parent 3261338aef
commit 361eb23a42

View File

@@ -312,17 +312,25 @@ int BLI_ghashutil_intcmp(const void *a, const void *b)
return (a < b) ? -1 : 1;
}
/**
* This function implements the widely used "djb" hash apparently posted
* by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
* unsigned hash value starts at 5381 and for each byte 'c' in the
* string, is updated: <literal>hash = hash * 33 + c</literal>. This
* function uses the signed value of each byte.
*
* note: this is the same hash method that glib 2.34.0 uses.
*/
unsigned int BLI_ghashutil_strhash(const void *ptr)
{
const char *s = ptr;
unsigned int i = 0;
unsigned char c;
const signed char *p;
unsigned int h = 5381;
while ((c = *s++)) {
i = i * 37 + c;
for (p = ptr; *p != '\0'; p++) {
h = (h << 5) + h + *p;
}
return i;
return h;
}
int BLI_ghashutil_strcmp(const void *a, const void *b)
{
@@ -376,4 +384,3 @@ void BLI_ghashutil_pairfree(void *ptr)
{
MEM_freeN((void *)ptr);
}