From 588700dbfa1ca6c10ab9bc615bd10d065dfb4b72 Mon Sep 17 00:00:00 2001 From: Ievgenii Meshcheriakov Date: Tue, 29 Aug 2023 15:25:49 +0200 Subject: [PATCH] QDBusSlotCache: Include flags into the hash key This removes the need for doing two separate iteration to find entries in the hash. Also QMultyHash can now be replaced by plain QHash. Change-Id: Ie704e74c1dbb0c8e40d22a7cd572fcc8a3bfcd5d Reviewed-by: Thiago Macieira --- src/dbus/qdbusintegrator.cpp | 14 +++++--------- src/dbus/qdbusintegrator_p.h | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/dbus/qdbusintegrator.cpp b/src/dbus/qdbusintegrator.cpp index 5cef94216ba..ed5d424ae4c 100644 --- a/src/dbus/qdbusintegrator.cpp +++ b/src/dbus/qdbusintegrator.cpp @@ -857,19 +857,15 @@ bool QDBusConnectionPrivate::activateCall(QObject* object, int flags, const QDBu cacheKey += signature; } - QDBusSlotCache::Hash::ConstIterator cacheIt = slotCache.hash.constFind(cacheKey); - while (cacheIt != slotCache.hash.constEnd() && cacheIt->flags != flags && - cacheIt.key() == cacheKey) - ++cacheIt; - if (cacheIt == slotCache.hash.constEnd() || cacheIt.key() != cacheKey) - { + QDBusSlotCache::Key compoundKey{ std::move(cacheKey), flags }; + QDBusSlotCache::Hash::ConstIterator cacheIt = slotCache.hash.constFind(compoundKey); + if (cacheIt == slotCache.hash.constEnd()) { // not cached, analyze the meta object const QMetaObject *mo = object->metaObject(); QByteArray memberName = msg.member().toUtf8(); // find a slot that matches according to the rules above QDBusSlotCache::Data slotData; - slotData.flags = flags; slotData.slotIdx = ::findSlot(mo, memberName, flags, msg.signature(), slotData.metaTypes); if (slotData.slotIdx == -1) { // ### this is where we want to add the connection as an arg too @@ -881,7 +877,7 @@ bool QDBusConnectionPrivate::activateCall(QObject* object, int flags, const QDBu // save the negative lookup slotData.slotIdx = -1; slotData.metaTypes.clear(); - slotCache.hash.insert(cacheKey, slotData); + slotCache.hash.insert(compoundKey, slotData); object->setProperty(cachePropertyName, QVariant::fromValue(slotCache)); qCWarning(dbusIntegration).nospace() << "Could not find slot " << mo->className() @@ -891,7 +887,7 @@ bool QDBusConnectionPrivate::activateCall(QObject* object, int flags, const QDBu } // save to the cache - slotCache.hash.insert(cacheKey, slotData); + slotCache.hash.insert(compoundKey, slotData); object->setProperty(cachePropertyName, QVariant::fromValue(slotCache)); // found the slot to be called diff --git a/src/dbus/qdbusintegrator_p.h b/src/dbus/qdbusintegrator_p.h index ab5dba5d292..774fb79d0be 100644 --- a/src/dbus/qdbusintegrator_p.h +++ b/src/dbus/qdbusintegrator_p.h @@ -46,18 +46,34 @@ struct QDBusSlotCache { struct Data { - int flags; int slotIdx; QList metaTypes; void swap(Data &other) noexcept { - qSwap(flags, other.flags); qSwap(slotIdx, other.slotIdx); qSwap(metaTypes, other.metaTypes); } }; - typedef QMultiHash Hash; + + struct Key + { + QString memberWithSignature; + int flags; + + friend bool operator==(const Key &lhs, const Key &rhs) noexcept + { + return lhs.memberWithSignature == rhs.memberWithSignature && lhs.flags == rhs.flags; + } + + friend size_t qHash(const QDBusSlotCache::Key &key, size_t seed = 0) noexcept + { + return qHashMulti(seed, key.memberWithSignature, key.flags); + } + }; + + using Hash = QHash; + Hash hash; void swap(QDBusSlotCache &other) noexcept { qSwap(hash, other.hash); }