QHash benchmark: improve Java's hash
authorGiuseppe D'Angelo <dangelog@gmail.com>
Wed, 25 Jan 2012 22:12:43 +0000 (22:12 +0000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 27 Jan 2012 21:48:12 +0000 (22:48 +0100)
Added a bit of documentation to the Java-like hashing function.

Change-Id: I3f44eee305d91b76f0f89cd1acf21f6430b9482b
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
tests/benchmarks/corelib/tools/qhash/outofline.cpp

index 88fd9c1..2174053 100644 (file)
@@ -87,6 +87,17 @@ uint qHash(const String &str)
     return h;
 }
 
+// The Java's hashing algorithm for strings is a variation of D. J. Bernstein
+// hashing algorithm appeared here http://cr.yp.to/cdb/cdb.txt
+// and informally known as DJB33XX - DJB's 33 Times Xor.
+// Java uses DJB31XA, that is, 31 Times Add.
+// The original algorithm was a loop around  "(h << 5) + h ^ c",
+// which is indeed "h * 33 ^ c"; it was then changed to
+// "(h << 5) - h ^ c", so "h * 31 ^ c", and the XOR changed to a sum:
+// "(h << 5) - h + c", which can save some assembly instructions.
+// Still, we can avoid writing the multiplication as "(h << 5) - h"
+// -- the compiler will turn it into a shift and an addition anyway
+// (for instance, gcc 4.4 does that even at -O0).
 uint qHash(const JavaString &str)
 {
     const unsigned short *p = (unsigned short *)str.constData();