From 9b3820c7d32a1c13234ceea00c2b92de0c1992e0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 12 Jan 2024 08:02:07 +0100 Subject: [PATCH] QObject: de-pessimize installEventFilter() Don't loop over the list of event filters twice, do it once with a combined predicate. This code is still has a big impedance mismatch: the removeIf() will always leave empty space at the back while prepend() needs it at the front. We should fix this by reversing the meaning of the order of event filters to mean back()-is-applied-first, so new filters can be append()ed, but that is for another patch. Done-with: Jarek Kobus Change-Id: I08324697229a54c8f66c6c320cf7232d707a08f1 Reviewed-by: Giuseppe D'Angelo Reviewed-by: Jarek Kobus (cherry picked from commit edff6a33a1e3757b9bb4831091a3db8fa99e3219) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qobject.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index c000452b04a..7abda7ccc3a 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2351,9 +2351,9 @@ void QObject::installEventFilter(QObject *obj) d->ensureExtraData(); - // clean up unused items in the list - d->extraData->eventFilters.removeAll((QObject *)nullptr); - d->extraData->eventFilters.removeAll(obj); + // clean up unused items in the list along the way: + auto isNullOrEquals = [](auto obj) { return [obj](const auto &p) { return !p || p == obj; }; }; + d->extraData->eventFilters.removeIf(isNullOrEquals(obj)); d->extraData->eventFilters.prepend(obj); }