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 <thiago.macieira@intel.com>
This commit is contained in:
Ievgenii Meshcheriakov 2023-08-29 15:25:49 +02:00
parent 9aaeb1bb3d
commit 588700dbfa
2 changed files with 24 additions and 12 deletions

View File

@ -857,19 +857,15 @@ bool QDBusConnectionPrivate::activateCall(QObject* object, int flags, const QDBu
cacheKey += signature; cacheKey += signature;
} }
QDBusSlotCache::Hash::ConstIterator cacheIt = slotCache.hash.constFind(cacheKey); QDBusSlotCache::Key compoundKey{ std::move(cacheKey), flags };
while (cacheIt != slotCache.hash.constEnd() && cacheIt->flags != flags && QDBusSlotCache::Hash::ConstIterator cacheIt = slotCache.hash.constFind(compoundKey);
cacheIt.key() == cacheKey) if (cacheIt == slotCache.hash.constEnd()) {
++cacheIt;
if (cacheIt == slotCache.hash.constEnd() || cacheIt.key() != cacheKey)
{
// not cached, analyze the meta object // not cached, analyze the meta object
const QMetaObject *mo = object->metaObject(); const QMetaObject *mo = object->metaObject();
QByteArray memberName = msg.member().toUtf8(); QByteArray memberName = msg.member().toUtf8();
// find a slot that matches according to the rules above // find a slot that matches according to the rules above
QDBusSlotCache::Data slotData; QDBusSlotCache::Data slotData;
slotData.flags = flags;
slotData.slotIdx = ::findSlot(mo, memberName, flags, msg.signature(), slotData.metaTypes); slotData.slotIdx = ::findSlot(mo, memberName, flags, msg.signature(), slotData.metaTypes);
if (slotData.slotIdx == -1) { if (slotData.slotIdx == -1) {
// ### this is where we want to add the connection as an arg too // ### 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 // save the negative lookup
slotData.slotIdx = -1; slotData.slotIdx = -1;
slotData.metaTypes.clear(); slotData.metaTypes.clear();
slotCache.hash.insert(cacheKey, slotData); slotCache.hash.insert(compoundKey, slotData);
object->setProperty(cachePropertyName, QVariant::fromValue(slotCache)); object->setProperty(cachePropertyName, QVariant::fromValue(slotCache));
qCWarning(dbusIntegration).nospace() << "Could not find slot " << mo->className() 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 // save to the cache
slotCache.hash.insert(cacheKey, slotData); slotCache.hash.insert(compoundKey, slotData);
object->setProperty(cachePropertyName, QVariant::fromValue(slotCache)); object->setProperty(cachePropertyName, QVariant::fromValue(slotCache));
// found the slot to be called // found the slot to be called

View File

@ -46,18 +46,34 @@ struct QDBusSlotCache
{ {
struct Data struct Data
{ {
int flags;
int slotIdx; int slotIdx;
QList<QMetaType> metaTypes; QList<QMetaType> metaTypes;
void swap(Data &other) noexcept void swap(Data &other) noexcept
{ {
qSwap(flags, other.flags);
qSwap(slotIdx, other.slotIdx); qSwap(slotIdx, other.slotIdx);
qSwap(metaTypes, other.metaTypes); qSwap(metaTypes, other.metaTypes);
} }
}; };
typedef QMultiHash<QString, Data> 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<Key, Data>;
Hash hash; Hash hash;
void swap(QDBusSlotCache &other) noexcept { qSwap(hash, other.hash); } void swap(QDBusSlotCache &other) noexcept { qSwap(hash, other.hash); }