QLoggingRegistry: avoid double-lookup

The code used the if (!contains()) { insert() } anti-pattern,
necessitated by Qt's deviation from the STL of allowing insert() to
overwrite an existing entry, causing two lookups of the same key.

Fix by recording the size prior to the execution of the indexing
operator and taking a size increase as the cue to populate the (new)
entry. This way, we look up the key only once.

Change-Id: Ica039035fe9ea4b88c20184784c324c9fac33d49
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
This commit is contained in:
Marc Mutz 2021-11-16 16:34:32 +01:00
parent 7fe5611365
commit a3303aceeb

View File

@ -353,8 +353,11 @@ void QLoggingRegistry::registerCategory(QLoggingCategory *cat, QtMsgType enableF
{
const auto locker = qt_scoped_lock(registryMutex);
if (!categories.contains(cat)) {
categories.insert(cat, enableForLevel);
const auto oldSize = categories.size();
auto &e = categories[cat];
if (categories.size() != oldSize) {
// new entry
e = enableForLevel;
(*categoryFilter)(cat);
}
}