[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [52873] trunk/blender/source/blender/ blenlib/intern/BLI_ghash.c: switch BLI_ghashutil_strhash() to "djb" hash ( as used by glib),

Campbell Barton ideasman42 at gmail.com
Tue Dec 11 14:57:58 CET 2012


Revision: 52873
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=52873
Author:   campbellbarton
Date:     2012-12-11 13:57:58 +0000 (Tue, 11 Dec 2012)
Log Message:
-----------
switch BLI_ghashutil_strhash() to "djb" hash (as used by glib),

Gives approx 10% speedup in my own micro-benchmark looking up operators.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/intern/BLI_ghash.c

Modified: trunk/blender/source/blender/blenlib/intern/BLI_ghash.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_ghash.c	2012-12-11 13:56:01 UTC (rev 52872)
+++ trunk/blender/source/blender/blenlib/intern/BLI_ghash.c	2012-12-11 13:57:58 UTC (rev 52873)
@@ -312,17 +312,25 @@
 		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 @@
 {
 	MEM_freeN((void *)ptr);
 }
-




More information about the Bf-blender-cvs mailing list