summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/tools
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <dangelog@gmail.com>2012-01-25 22:12:43 +0000
committerQt by Nokia <qt-info@nokia.com>2012-01-27 22:48:12 +0100
commite1149349c158364854bcfece3c6d80ebccb26f28 (patch)
tree06dff5c4acb10879f6c5a3e4b949f59826f46fca /tests/benchmarks/corelib/tools
parent071561f0eba5c92fc6363d54e8f7d4508a42df45 (diff)
QHash benchmark: improve Java's hash
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>
Diffstat (limited to 'tests/benchmarks/corelib/tools')
-rw-r--r--tests/benchmarks/corelib/tools/qhash/outofline.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/tests/benchmarks/corelib/tools/qhash/outofline.cpp b/tests/benchmarks/corelib/tools/qhash/outofline.cpp
index 88fd9c144d..21740536dd 100644
--- a/tests/benchmarks/corelib/tools/qhash/outofline.cpp
+++ b/tests/benchmarks/corelib/tools/qhash/outofline.cpp
@@ -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();