From d027d6d9da037b128e164418a5ed43001e6e0db1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 18 Mar 2025 16:43:13 +0100 Subject: [PATCH] QUuid: fix qHash() on 64-bit platforms The old implementation from Qt 5 time only affected the 32 LSB of the result. The upper bits were completely determined by the seed, and the seed alone. To see this, note that all except the seed are at most 32-bit values, and no shifting out of that range occurs, either. Fix by just using qHashBits(), making sure (with a static_assert()) that QUuid has unique object representation (= can be compared for equality using memcmp()). [ChangeLog][QtCore][QUuid] Improved the performance of the qHash() function on 64-bit platforms by populating all bits of the output (was: only lower 32 bits). Amends 55d68a16aafb93aa15bcdbd78892006777b6067a. Pick-to: 6.8 6.5 Change-Id: Ibf67350f571889fd21e0acc82639c053c0d606b6 Reviewed-by: Thiago Macieira (cherry picked from commit bb846a22c37dcb085829676d8feb7c203d21c886) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/plugin/quuid.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index a28706d65d6..d89facf4c6a 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -998,10 +998,9 @@ QDebug operator<<(QDebug dbg, const QUuid &id) */ size_t qHash(const QUuid &uuid, size_t seed) noexcept { - return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) - ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) - ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) - ^ seed; + static_assert(std::has_unique_object_representations_v, + "Can't use qHashBits() if the type has padding holes."); + return qHashBits(&uuid, sizeof(QUuid), seed); }