From a3303aceeb49c6c7a5fc21e083715ddbb989a9b0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 16 Nov 2021 16:34:32 +0100 Subject: [PATCH] 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 --- src/corelib/io/qloggingregistry.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qloggingregistry.cpp b/src/corelib/io/qloggingregistry.cpp index 03c0a538a65..73b98190a71 100644 --- a/src/corelib/io/qloggingregistry.cpp +++ b/src/corelib/io/qloggingregistry.cpp @@ -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); } }