diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-01-31 10:36:34 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-04-09 20:03:45 +0200 |
commit | f14559790b95506b1e3231ee6fc1d95b730d572c (patch) | |
tree | 09bb76df5f67850c7a071088b4a8a3d48a62b973 /tests/auto/corelib/tools | |
parent | 03d990fa157abbb5f7554a1e3c6404cf4d82f307 (diff) |
Change qHashBits to use MurmurHash2
The old implementation was either using CRC32 on modern processors
or a trivial, but rather slow implementation.
We can't continue with CRC32, as that implementation can only
give us 32bit hashes, where we now need to support 64bit in Qt 6.
Change the implementation to use MurmurHash, as public domain
implementation that is both very fast and leads to well distributed hashes.
This hash function is about as fast as the SSE optimized CRC32 implementation
but works everywhere and gives us 64 bit hash values.
Here are some numbers (time for 10M hashes):
14 char 16 char
QByteArray QString float
old qHash (non CRC32) 127ms 134ms 48ms
old qHash (using SSE CRC32 instructions 60ms 62ms 46ms
new qHash 52ms 43ms 46ms
Unfortunately MurmurHash is not safe against hash table DoS attacks, as
potential hash collisions are indepenent of the seed. This will get
addressed in followup commit, where we use SipHash or an SSE optimized
AES based hashing algorithm that does not have those issues.
Change-Id: I4fbc0ac299215b6db78c7a0a2a1d7689b0ea848b
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r-- | tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp index c23c08bd7c..fcd227f1bf 100644 --- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp +++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp @@ -55,7 +55,7 @@ private Q_SLOTS: void qhash(); void qhash_of_empty_and_null_qstring(); void qhash_of_empty_and_null_qbytearray(); - void fp_qhash_of_zero_is_seed(); + void qhash_of_zero_floating_points(); void qthash_data(); void qthash(); void range(); @@ -97,7 +97,6 @@ void tst_QHashFunctions::qhash() { QBitArray a1; QBitArray a2; - QCOMPARE(qHash(a1, seed), seed); a1.resize(1); a1.setBit(0, true); @@ -193,17 +192,12 @@ void tst_QHashFunctions::qhash_of_empty_and_null_qbytearray() QCOMPARE(qHash(null, seed), qHash(empty, seed)); } -void tst_QHashFunctions::fp_qhash_of_zero_is_seed() +void tst_QHashFunctions::qhash_of_zero_floating_points() { - QCOMPARE(qHash(-0.0f, seed), seed); - QCOMPARE(qHash( 0.0f, seed), seed); - - QCOMPARE(qHash(-0.0 , seed), seed); - QCOMPARE(qHash( 0.0 , seed), seed); - + QCOMPARE(qHash(-0.0f, seed), qHash(0.0f, seed)); + QCOMPARE(qHash(-0.0 , seed), qHash(0.0 , seed)); #ifndef Q_OS_DARWIN - QCOMPARE(qHash(-0.0L, seed), seed); - QCOMPARE(qHash( 0.0L, seed), seed); + QCOMPARE(qHash(-0.0L, seed), qHash(0.0L, seed)); #endif } |