diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-07-27 16:32:28 +0300 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-07-31 11:05:39 +0300 |
commit | f3b6963f8b873c190cc1033cb8dabaf1d63ee2b6 (patch) | |
tree | 1e1031de0e959ca0c7e72a8ad5eaa5eed32d79d3 | |
parent | d2c52680884095238ec3859fabaad03157af3985 (diff) |
QEventFilterService: these aren't the algorithms you're looking for
The event filters are ordered by priority, so don't use a linear scan
in order to find whether one with the same position already exists,
use lower_bound, whose result at the same time gives the position of
an existing filter (if any), as well as the insertion position for the
new element. So no need to do a full sort afterwards, either.
Change the exisitng op< into an aptly-named lambda, and don't depend on
transitive includes.
Also, since this code does not need the CoW feature of QVector, port to
std::vector, which is faster and produces more compact code.
Altogether saves ~2KiB (0.47%) in text size on optimized Linux AMD64
GCC 9.1 builds.
Change-Id: Ie46ca2fbb2dcbea23a0098a72336604776dcc296
Reviewed-by: Mike Krus <mike.krus@kdab.com>
-rw-r--r-- | src/core/services/qeventfilterservice.cpp | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/core/services/qeventfilterservice.cpp b/src/core/services/qeventfilterservice.cpp index 4855d69ed..036bcd7b4 100644 --- a/src/core/services/qeventfilterservice.cpp +++ b/src/core/services/qeventfilterservice.cpp @@ -40,10 +40,12 @@ #include "qeventfilterservice_p.h" #include <QtCore/QObject> -#include <QtCore/QVector> #include <Qt3DCore/private/qabstractserviceprovider_p.h> +#include <algorithm> +#include <vector> + QT_BEGIN_NAMESPACE namespace { @@ -53,10 +55,10 @@ namespace { int priority; }; - bool operator <(const FilterPriorityPair &a, const FilterPriorityPair &b) + const auto byPriority = [](const FilterPriorityPair &a, const FilterPriorityPair &b) noexcept { return a.priority < b.priority; - } + }; } Q_DECLARE_TYPEINFO(FilterPriorityPair, Q_PRIMITIVE_TYPE); @@ -88,32 +90,26 @@ public: void registerEventFilter(QObject *eventFilter, int priority) { - for (int i = 0, m = m_eventFilters.size(); i < m; ++i) - if (m_eventFilters.at(i).priority == priority) - return; - FilterPriorityPair fpPair; fpPair.filter = eventFilter; fpPair.priority = priority; - m_eventFilters.push_back(fpPair); - std::sort(m_eventFilters.begin(), m_eventFilters.end()); + const auto it = std::lower_bound(m_eventFilters.begin(), m_eventFilters.end(), fpPair, byPriority); + if (it == m_eventFilters.end() || it->priority != priority) + m_eventFilters.insert(it, std::move(fpPair)); } void unregisterEventFilter(QObject *eventFilter) { - QVector<FilterPriorityPair>::iterator it = m_eventFilters.begin(); - const QVector<FilterPriorityPair>::iterator end = m_eventFilters.end(); - while (it != end) { + for (auto it = m_eventFilters.begin(), end = m_eventFilters.end(); it != end; ++it) { if (it->filter == eventFilter) { m_eventFilters.erase(it); return; } - ++it; } } QScopedPointer<InternalEventListener> m_eventDispatcher; - QVector<FilterPriorityPair> m_eventFilters; + std::vector<FilterPriorityPair> m_eventFilters; }; /* !\internal @@ -178,8 +174,8 @@ InternalEventListener::InternalEventListener(QEventFilterServicePrivate *filterS bool InternalEventListener::eventFilter(QObject *obj, QEvent *e) { - for (int i = m_filterService->m_eventFilters.size() - 1; i >= 0; --i) { - const FilterPriorityPair &fPPair = m_filterService->m_eventFilters.at(i); + for (auto i = m_filterService->m_eventFilters.size(); i > 0; --i) { + const FilterPriorityPair &fPPair = m_filterService->m_eventFilters[i - 1]; if (fPPair.filter->eventFilter(obj, e)) return true; } |