compressEvents: simplify TimerEvent compression

While better in some benchmarks, it turns out to be less efficient to
first find an event to the receiver, and then checking the event-ptr
and -type in other benchmarks, compared to just iterating the list and
checking these values.

Partially reverts 3e6b42ae9dbf4f90ba890d78a4c49f9936f4976b

Fixes: QTBUG-126394
Pick-to: 6.7
Change-Id: I748bda3d31350aea6e87db9bd57359ab17cf5d67
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: YAMAMOTO Atsushi - Signal Slot <atsushi.yamamoto@signal-slot.co.jp>
(cherry picked from commit b039d2251a14281947a43e226bae6c6fbae7312a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mårten Nordheim 2024-06-17 15:11:51 +02:00 committed by Qt Cherry-pick Bot
parent e7cf150caa
commit eb208a6bb5

View File

@ -1729,18 +1729,17 @@ bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEven
int receiverPostedEvents = receiver->d_func()->postedEvents.loadRelaxed();
// compress posted timers to this object.
if (event->type() == QEvent::Timer && receiverPostedEvents > 0) {
int timerId = static_cast<QTimerEvent *>(event)->timerId();
auto sameReceiver = [receiver](const QPostEvent &e) { return e.receiver == receiver; };
auto it = std::find_if(postedEvents->cbegin(), postedEvents->cend(), sameReceiver);
while (receiverPostedEvents > 0 && it != postedEvents->cend()) {
if (it->event && it->event->type() == QEvent::Timer
&& static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
delete event;
return true;
const int timerId = static_cast<QTimerEvent *>(event)->timerId();
auto it = postedEvents->cbegin();
const auto end = postedEvents->cend();
while (it != end) {
if (it->event && it->event->type() == QEvent::Timer && it->receiver == receiver) {
if (static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
delete event;
return true;
}
}
if (--receiverPostedEvents)
it = std::find_if(it + 1, postedEvents->cend(), sameReceiver);
++it;
}
return false;
}