diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-01-07 09:10:51 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-01-12 01:03:15 +0000 |
commit | 66a183298a8ff3d51da02f25ffe5eeea0ea76862 (patch) | |
tree | 52d47b2261eccd02611496c3c7d38edfa7d73bc5 /src/gui | |
parent | d4611ba3a5b46ee790e6c790ef6c3d771d3507ee (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.
Pick-to: 6.3
Change-Id: I4c52d2f2d5175991db4931d29c688de125f5b032
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/kernel/qpointingdevice.cpp | 7 |
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(); } |