From 58b4e0736919e0504f5ca5307ad7aefa894a6f38 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Thu, 11 Jan 2018 17:46:24 +0100 Subject: Optimize QTimer::singleShot(0, ...) when taking PMF or Functor callable QTimer::singleShot is optimized for zero timeouts when using the API taking a string method name. This optimization was not used for the API taking a PMF or functor. This patch adds it, making the various API calls behave similarly from a performance point of view. The approach taken here requires a QObject context object. If none is available, e.g. a nullptr was passed explicitly, or the QTimer::singleShot(O, Functor) API was used, the optimization could not easily be applied. This is not only bad from a performance POV, but also poses as a potential source for heisenbugs: Using the different API versions of QTimer::singleShot would use different code paths internally, which then would not ensure the expected slot call order. This problem actually existed already when mixing the string-based slot syntax with PMF/functors in the QTimer::singleShot API. This patch overcomes this hurdle and fixes all of the above: When we encounter a 0ms single shot timer, and no QObject context object is available, we fall back to the main thread, or create a temporary QObject for any other thread. The updated and extended benchmark shows that this is still a significant performance improvement over using a timer: ********* Start testing of qtimer_vs_qmetaobject ********* Config: Using QtTest library 5.14.0, Qt 5.14.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 8.2.1 20181127) PASS : qtimer_vs_qmetaobject::initTestCase() PASS : qtimer_vs_qmetaobject::bench(singleShot_slot) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_slot": 7.48 msecs per iteration (total: 748, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(singleShot_pmf) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_pmf": 7.20 msecs per iteration (total: 720, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(singleShot_functor) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_functor": 6.79 msecs per iteration (total: 679, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(singleShot_functor_noctx) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_functor_noctx": 6.92 msecs per iteration (total: 693, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(invokeMethod_string) RESULT : qtimer_vs_qmetaobject::bench():"invokeMethod_string": 7.34 msecs per iteration (total: 735, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(invokeMethod_pmf) RESULT : qtimer_vs_qmetaobject::bench():"invokeMethod_pmf": 6.90 msecs per iteration (total: 690, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(invokeMethod_functor) RESULT : qtimer_vs_qmetaobject::bench():"invokeMethod_functor": 6.62 msecs per iteration (total: 662, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_slot) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_slot": 7.45 msecs per iteration (total: 745, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_pmf) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_pmf": 7.46 msecs per iteration (total: 747, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_functor) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_functor": 6.70 msecs per iteration (total: 671, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_functor_noctx) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_functor_noctx": 13.75 msecs per iteration (total: 1,376, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(invokeMethod_string) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"invokeMethod_string": 7.05 msecs per iteration (total: 706, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(invokeMethod_pmf) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"invokeMethod_pmf": 6.70 msecs per iteration (total: 670, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(invokeMethod_functor) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"invokeMethod_functor": 6.58 msecs per iteration (total: 658, iterations: 100) PASS : qtimer_vs_qmetaobject::cleanupTestCase() Totals: 16 passed, 0 failed, 0 skipped, 0 blacklisted, 20977ms ********* Finished testing of qtimer_vs_qmetaobject ********* Without the change to qtimer.cpp, the results are: ********* Start testing of qtimer_vs_qmetaobject ********* Config: Using QtTest library 5.14.0, Qt 5.14.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 8.2.1 20181127) PASS : qtimer_vs_qmetaobject::initTestCase() PASS : qtimer_vs_qmetaobject::bench(singleShot_slot) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_slot": 7.45 msecs per iteration (total: 745, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(singleShot_pmf) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_pmf": 112.84 msecs per iteration (total: 11,285, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(singleShot_functor) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_functor": 115.62 msecs per iteration (total: 11,563, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(singleShot_functor_noctx) RESULT : qtimer_vs_qmetaobject::bench():"singleShot_functor_noctx": 110.81 msecs per iteration (total: 11,082, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(invokeMethod_string) RESULT : qtimer_vs_qmetaobject::bench():"invokeMethod_string": 7.04 msecs per iteration (total: 704, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(invokeMethod_pmf) RESULT : qtimer_vs_qmetaobject::bench():"invokeMethod_pmf": 6.62 msecs per iteration (total: 662, iterations: 100) PASS : qtimer_vs_qmetaobject::bench(invokeMethod_functor) RESULT : qtimer_vs_qmetaobject::bench():"invokeMethod_functor": 6.62 msecs per iteration (total: 662, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_slot) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_slot": 7.45 msecs per iteration (total: 746, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_pmf) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_pmf": 118.42 msecs per iteration (total: 11,842, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_functor) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_functor": 119.35 msecs per iteration (total: 11,936, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(singleShot_functor_noctx) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"singleShot_functor_noctx": 130.96 msecs per iteration (total: 13,096, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(invokeMethod_string) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"invokeMethod_string": 8.08 msecs per iteration (total: 808, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(invokeMethod_pmf) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"invokeMethod_pmf": 6.79 msecs per iteration (total: 680, iterations: 100) PASS : qtimer_vs_qmetaobject::benchBackgroundThread(invokeMethod_functor) RESULT : qtimer_vs_qmetaobject::benchBackgroundThread():"invokeMethod_functor": 7.49 msecs per iteration (total: 749, iterations: 100) PASS : qtimer_vs_qmetaobject::cleanupTestCase() Totals: 16 passed, 0 failed, 0 skipped, 0 blacklisted, 153995ms ********* Finished testing of qtimer_vs_qmetaobject ********* Additionally, this patch adds a unit test to verify that the slot call order for 0ms single shot timers is followed while mixing the various API versions. It fails without this patch but passes now. Finally, another test is added to verify that using QTimer::singleShot before a QCoreApplication was constructed is still working properly. Change-Id: I0d6211554b6198cb3e527be9ec3adc572b1b54ee Reviewed-by: Thiago Macieira --- src/corelib/kernel/qobjectdefs.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/corelib/kernel/qobjectdefs.h') diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index 28674193c3..418b8cf1d2 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -576,6 +576,7 @@ struct Q_CORE_EXPORT QMetaObject private: static bool invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase *slot, Qt::ConnectionType type, void *ret); + friend class QTimer; }; class Q_CORE_EXPORT QMetaObject::Connection { -- cgit v1.2.3