From 1b464c0a22c4ef7e066bf50123e067fa3eb60e5a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 7 Jan 2022 09:10:51 +0100 Subject: 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 Reviewed-by: Shawn Rutledge (cherry picked from commit 66a183298a8ff3d51da02f25ffe5eeea0ea76862) Reviewed-by: Qt Cherry-pick Bot --- src/gui/kernel/qpointingdevice.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/gui/kernel') 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(); } -- cgit v1.2.3