QMetaTypeFunctionRegistry: avoid double-lookup in insertIfNotContains()
Because there's no insertIfNotContains()-like functionality in QHash (unlike std::unordered_map, where insert() doesn't overwrite an existing entry), the code first called contains(k) and then insert(k, ~~~), causing two lookups in the case where the insertion actually happens. Fix by using the pattern QDuplicateTracker's QSet fall-back uses, too: recording the size before and after the call to the indexing operator and using a size increase as the criterion that an insertion should happen. This reduces the number of lookups to one, at the cost of a mapped_type default construction (which, given mapped_type is std::function, should be cheap). Change-Id: I24b31107b3e26f2eea2edce7b46f8cb5e7cb35bf Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>bb10
parent
826e1963e3
commit
02aa236256
|
|
@ -1560,9 +1560,11 @@ public:
|
|||
bool insertIfNotContains(Key k, const T &f)
|
||||
{
|
||||
const QWriteLocker locker(&lock);
|
||||
if (map.contains(k))
|
||||
const qsizetype oldSize = map.size();
|
||||
auto &e = map[k];
|
||||
if (map.size() == oldSize) // already present
|
||||
return false;
|
||||
map.insert(k, f);
|
||||
e = f;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue