From cb2246ad15241016063689de1600519202b87a93 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 30 Mar 2017 14:01:02 +0200 Subject: [PATCH] Allow to chain qt_hash() calls There are some callers of qt_hash that first build a string just to hash it. By allowing to pass an initial value for 'h', we can chain qt_hash() calls to avoid having to allocate memory just to hash a two-part string. Change-Id: Ifaca82d47b2fb8c707912342c3ddd84f91e70267 Reviewed-by: Lars Knoll --- src/corelib/tools/qhash.cpp | 20 +++++++++----------- src/corelib/tools/qhashfunctions.h | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index f83643e90a..662c304a2e 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -412,27 +412,25 @@ void qSetGlobalQHashSeed(int newSeed) results. The qt_hash functions must *never* change their results. + + This function can hash discontiguous memory by invoking it on each chunk, + passing the previous's result in the next call's \a chained argument. */ -static uint qt_hash(const QChar *p, size_t n) Q_DECL_NOTHROW +uint qt_hash(QStringView key, uint chained) Q_DECL_NOTHROW { - uint h = 0; + auto n = key.size(); + auto p = key.utf16(); + + uint h = chained; while (n--) { - h = (h << 4) + (*p++).unicode(); + h = (h << 4) + *p++; h ^= (h & 0xf0000000) >> 23; h &= 0x0fffffff; } return h; } -/*! - \internal -*/ -uint qt_hash(QStringView key) Q_DECL_NOTHROW -{ - return qt_hash(key.data(), key.size()); -} - /* The prime_deltas array contains the difference between a power of two and the next prime number: diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h index 2c3abe6af0..f75b310e4e 100644 --- a/src/corelib/tools/qhashfunctions.h +++ b/src/corelib/tools/qhashfunctions.h @@ -102,7 +102,7 @@ Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QStringRef &key, uint seed = Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QStringView key, uint seed = 0) Q_DECL_NOTHROW; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed = 0) Q_DECL_NOTHROW; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) Q_DECL_NOTHROW; template inline uint qHash(const T *key, uint seed = 0) Q_DECL_NOTHROW {