summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-01-07 09:10:51 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-01-12 14:58:16 +0000
commit1b464c0a22c4ef7e066bf50123e067fa3eb60e5a (patch)
tree74e38fc42a05d7aa1d15befa60d83d18357cc529 /src/gui/kernel
parent673cb83599cae382af4721ebda55b3479ce64bde (diff)
QPointingDevice: avoid double lookup in Private::pointById()
Use the newly-added try_emplace() function, to work around Qt's associative container's broken insert() behavior (which is equivalent to STL's insert_or_assign()), forcing either a double lookup: auto it = c.find(key); // lookup 1 if (it == c.end()) c.insert(key, ~~~); // lookup 2 or the usual size-check trick: const auto oldSize = c.size(); auto &e = c[key]; if (c.size() != oldSize) { // newly inserted onto any lookup-or-initialize-like code. try_emplace beats them both in terms of efficiency and readability. Change-Id: I4c52d2f2d5175991db4931d29c688de125f5b032 Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io> (cherry picked from commit 66a183298a8ff3d51da02f25ffe5eeea0ea76862) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/gui/kernel')
-rw-r--r--src/gui/kernel/qpointingdevice.cpp7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/gui/kernel/qpointingdevice.cpp b/src/gui/kernel/qpointingdevice.cpp
index 32ca48b23d..e9ff3fc785 100644
--- a/src/gui/kernel/qpointingdevice.cpp
+++ b/src/gui/kernel/qpointingdevice.cpp
@@ -422,13 +422,12 @@ QPointingDevicePrivate::EventPointData *QPointingDevicePrivate::queryPointById(i
*/
QPointingDevicePrivate::EventPointData *QPointingDevicePrivate::pointById(int id) const
{
- auto it = activePoints.find(id);
- if (it == activePoints.end()) {
+ const auto [it, inserted] = activePoints.try_emplace(id);
+ if (inserted) {
Q_Q(const QPointingDevice);
- QPointingDevicePrivate::EventPointData epd;
+ auto &epd = it.value();
QMutableEventPoint::setId(epd.eventPoint, id);
QMutableEventPoint::setDevice(epd.eventPoint, q);
- return &activePoints.insert(id, epd).first.value();
}
return &it.value();
}