compressEvents: limit iteration

Slightly improves performance in the new benchmark

Change-Id: I2d71143ff7bc1f32ebb172f20be1843dec123e6c
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
Mårten Nordheim 2021-08-27 16:48:21 +02:00
parent 422880c9db
commit 3e6b42ae9d
2 changed files with 38 additions and 6 deletions

View File

@ -1729,15 +1729,21 @@ bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEven
Q_ASSERT(receiver);
Q_ASSERT(postedEvents);
auto receiverPostedEvents = receiver->d_func()->postedEvents;
// compress posted timers to this object.
if (event->type() == QEvent::Timer && receiver->d_func()->postedEvents > 0) {
int timerId = ((QTimerEvent *) event)->timerId();
for (const QPostEvent &e : std::as_const(*postedEvents)) {
if (e.receiver == receiver && e.event && e.event->type() == QEvent::Timer
&& ((QTimerEvent *) e.event)->timerId() == timerId) {
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;
}
if (--receiverPostedEvents)
it = std::find_if(it + 1, postedEvents->cend(), sameReceiver);
}
return false;
}
@ -1753,7 +1759,7 @@ bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEven
return false;
}
if (event->type() == QEvent::Quit && receiver->d_func()->postedEvents > 0) {
if (event->type() == QEvent::Quit && receiverPostedEvents > 0) {
for (const QPostEvent &cur : std::as_const(*postedEvents)) {
if (cur.receiver != receiver
|| cur.event == nullptr

View File

@ -10,6 +10,9 @@ Q_OBJECT
private slots:
void event_posting_benchmark_data();
void event_posting_benchmark();
void event_posting_multiple_objects_benchmark_data();
void event_posting_multiple_objects_benchmark();
};
void tst_QCoreApplication::event_posting_benchmark_data()
@ -39,6 +42,29 @@ void tst_QCoreApplication::event_posting_benchmark()
}
}
void tst_QCoreApplication::event_posting_multiple_objects_benchmark_data()
{
event_posting_benchmark_data();
}
void tst_QCoreApplication::event_posting_multiple_objects_benchmark()
{
QFETCH(int, size);
QObject objects[15]; // The size of the array has not been chosen through any meaningful means
QRandomGenerator gen;
// benchmark posting & sending events
QBENCHMARK {
for (int i = 0; i < size; ++i) {
QCoreApplication::postEvent(&objects[gen.bounded(0, int(std::size(objects)))],
new QTimerEvent(i % 10));
}
QCoreApplication::sendPostedEvents();
}
}
QTEST_MAIN(tst_QCoreApplication)
#include "tst_bench_qcoreapplication.moc"