Replace hash.keys().contains(k) with hash.contains(k) (35 call sites)

QHash/QMap::keys() allocates a list of every key on each call, then
contains() searches it linearly - an accidental O(n) plus allocation
where a direct O(1) lookup was meant. 35 occurrences across 7 files,
found while profiling project load times (context: #553/#560).

The hot one is ElementPictureFactory::getPictures(), which runs once
per element instance on project load: on the 3399 KiB example project
(191 instances, 129 cache hits) the keys() detour cost 45 ms of the
1.34 s total - measured, not estimated; the fix reproducibly shaves
~35-45 ms off that load. The remaining call sites are UI paths
(search&replace, dynamic text model, undo commands) where the waste
scales with selection/model size.

No behavior change: for QHash/QMap, keys().contains(k) and
contains(k) are equivalent by definition.

(cherry picked from commit 0a7f8f072fa68de7c01a9fc134a4bc8e16d62062)
This commit is contained in:
Dieter Mayer
2026-07-27 09:57:17 +02:00
parent 66129fd15c
commit 96f6fa44ad
7 changed files with 35 additions and 35 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ void ElementPictureFactory::getPictures(const ElementsLocation &location, QPictu
return;
}
if(m_pictures_H.keys().contains(uuid))
if(m_pictures_H.contains(uuid))
{
picture = m_pictures_H.value(uuid);
low_picture = m_low_pictures_H.value(uuid);