From aefd414ce2418bee5d6dacf9092f7a3949f02af8 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Fri, 18 Sep 2020 19:10:35 +0300 Subject: QWinEventNotifier: unlink from event dispatcher Instead of multiplexing all notifications into a single Qt event for the event dispatcher, we can send 'WinEventAct' event directly for each notifier which activated. This trick improves the performance (esp. on a large number of events) and allows us to remove notifiers handling from the event dispatcher completely. As an alternative to sending Qt events, use of Windows' APC queue in conjunction with waking up the Qt event loop from within the Windows thread pool has been considered. However, that would lead to signal emission asynchronous to the Qt event loop's operation, which is not acceptable. Thanks to Oswald Buddenhagen for the proposed idea. [ChangeLog][QtCore][QAbstractEventDispatcher] The {un}registerEventNotifier() member functions have been removed. QWinEventNotifier is no longer needed to be registered in the event dispatcher. Change-Id: I140892fb909eaae0eabf2e07ebabcab78c43841c Reviewed-by: Oswald Buddenhagen --- tests/benchmarks/corelib/kernel/CMakeLists.txt | 3 + tests/benchmarks/corelib/kernel/kernel.pro | 6 +- .../kernel/qwineventnotifier/CMakeLists.txt | 15 +++ .../corelib/kernel/qwineventnotifier/main.cpp | 136 +++++++++++++++++++++ .../kernel/qwineventnotifier/qwineventnotifier.pro | 6 + 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 tests/benchmarks/corelib/kernel/qwineventnotifier/CMakeLists.txt create mode 100644 tests/benchmarks/corelib/kernel/qwineventnotifier/main.cpp create mode 100644 tests/benchmarks/corelib/kernel/qwineventnotifier/qwineventnotifier.pro (limited to 'tests/benchmarks') diff --git a/tests/benchmarks/corelib/kernel/CMakeLists.txt b/tests/benchmarks/corelib/kernel/CMakeLists.txt index 2b41ea30f7..556785fd62 100644 --- a/tests/benchmarks/corelib/kernel/CMakeLists.txt +++ b/tests/benchmarks/corelib/kernel/CMakeLists.txt @@ -9,3 +9,6 @@ if(TARGET Qt::Widgets) add_subdirectory(qmetaobject) add_subdirectory(qobject) endif() +if(win32_x_) + add_subdirectory(qwineventnotifier) +endif() diff --git a/tests/benchmarks/corelib/kernel/kernel.pro b/tests/benchmarks/corelib/kernel/kernel.pro index 92f7174419..b7cb23aad6 100644 --- a/tests/benchmarks/corelib/kernel/kernel.pro +++ b/tests/benchmarks/corelib/kernel/kernel.pro @@ -6,8 +6,12 @@ SUBDIRS = \ qobject \ qvariant \ qcoreapplication \ - qtimer_vs_qmetaobject + qtimer_vs_qmetaobject \ + qwineventnotifier !qtHaveModule(widgets): SUBDIRS -= \ qmetaobject \ qobject + +# This test is only applicable on Windows +!win32: SUBDIRS -= qwineventnotifier diff --git a/tests/benchmarks/corelib/kernel/qwineventnotifier/CMakeLists.txt b/tests/benchmarks/corelib/kernel/qwineventnotifier/CMakeLists.txt new file mode 100644 index 0000000000..a2bf2e9574 --- /dev/null +++ b/tests/benchmarks/corelib/kernel/qwineventnotifier/CMakeLists.txt @@ -0,0 +1,15 @@ +# Generated from qwineventnotifier.pro. + +##################################################################### +## tst_bench_qwineventnotifier Binary: +##################################################################### + +qt_add_benchmark(tst_bench_qwineventnotifier + SOURCES + main.cpp + PUBLIC_LIBRARIES + Qt::Test +) + +#### Keys ignored in scope 1:.:.:qwineventnotifier.pro:: +# TEMPLATE = "app" diff --git a/tests/benchmarks/corelib/kernel/qwineventnotifier/main.cpp b/tests/benchmarks/corelib/kernel/qwineventnotifier/main.cpp new file mode 100644 index 0000000000..5ee59f8fb8 --- /dev/null +++ b/tests/benchmarks/corelib/kernel/qwineventnotifier/main.cpp @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +class QWinEventNotifierBenchmark : public QObject +{ + Q_OBJECT + +private slots: + void waves_data(); + void waves(); +}; + +class EventsFactory : public QObject +{ + Q_OBJECT + +public: + explicit EventsFactory(int waves, int notifiers, int iterations) + : numberOfWaves(waves), numberOfNotifiers(notifiers), + numberOfIterations(iterations) + { + events.resize(notifiers); + for (int i = 0; i < notifiers; ++i) { + events[i] = CreateEvent(NULL, TRUE, FALSE, NULL); + QVERIFY(events[i] != NULL); + QWinEventNotifier *notifier = new QWinEventNotifier(events[i], this); + Q_CHECK_PTR(notifier); + + connect(notifier, &QWinEventNotifier::activated, [i, this]() { + ResetEvent(this->events[i]); + if (--this->numberOfIterations == 0) + this->eventLoop.quit(); + else + SetEvent(this->events[(i + 1) % this->numberOfNotifiers]); + }); + + connect(this, &EventsFactory::stop, [notifier]() { + notifier->setEnabled(false); + }); + } + } + virtual ~EventsFactory() + { + for (auto event : events) + CloseHandle(event); + } + + void run() + { + Q_ASSERT(numberOfWaves != 0); + + int offset = 0; + for (int i = 0; i < numberOfWaves; ++i) { + SetEvent(events[offset]); + offset += qMax(1, numberOfNotifiers / numberOfWaves); + offset %= numberOfNotifiers; + } + eventLoop.exec(); + } + +signals: + void stop(); + +protected: + QVector events; + QEventLoop eventLoop; + int numberOfWaves; + int numberOfNotifiers; + int numberOfIterations; +}; + +void QWinEventNotifierBenchmark::waves_data() +{ + QTest::addColumn("waves"); + QTest::addColumn("notifiers"); + for (int waves : {1, 3, 10}) { + for (int notifiers : {10, 100, 1000}) + QTest::addRow("waves: %d, notifiers: %d", waves, notifiers) << waves << notifiers; + } +} + +void QWinEventNotifierBenchmark::waves() +{ + QFETCH(int, waves); + QFETCH(int, notifiers); + + const int iterations = 100000; + + EventsFactory factory(waves, notifiers, iterations); + + QElapsedTimer timer; + timer.start(); + + factory.run(); + + qDebug("Elapsed time: %.1f s", timer.elapsed() / 1000.0); + + emit factory.stop(); +} + +QTEST_MAIN(QWinEventNotifierBenchmark) + +#include "main.moc" diff --git a/tests/benchmarks/corelib/kernel/qwineventnotifier/qwineventnotifier.pro b/tests/benchmarks/corelib/kernel/qwineventnotifier/qwineventnotifier.pro new file mode 100644 index 0000000000..5c70ff6cc7 --- /dev/null +++ b/tests/benchmarks/corelib/kernel/qwineventnotifier/qwineventnotifier.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += benchmark +QT = core testlib + +TARGET = tst_bench_qwineventnotifier +SOURCES += main.cpp -- cgit v1.2.3