From 7a52e5948c0305ca41a9b66d591a12d171fd2bae Mon Sep 17 00:00:00 2001 From: Markus Goetz Date: Mon, 8 Mar 2010 15:21:38 +0100 Subject: Improve performance of QTimer::singleShot Avoid allocation of QObject and OS timer. Reviewed-by: Olivier Goffart --- src/corelib/kernel/qtimer.cpp | 14 +++++++++++++- tests/auto/qtimer/tst_qtimer.cpp | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index e17c995..7650f6a 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -339,8 +339,20 @@ QT_END_INCLUDE_NAMESPACE void QTimer::singleShot(int msec, QObject *receiver, const char *member) { - if (receiver && member) + if (receiver && member) { + if (msec == 0) { + // special code shortpath for 0-timers + const char* bracketPosition = strchr(member, '('); + if (!bracketPosition || !(member[0] >= '0' && member[0] <= '3')) { + qWarning("QTimer::singleShot: Invalid slot specification"); + return; + } + QByteArray methodName(member+1, bracketPosition - 1 - member); // extract method name + QMetaObject::invokeMethod(receiver, methodName.constData(), Qt::QueuedConnection); + return; + } (void) new QSingleShotTimer(msec, receiver, member); + } } /*! diff --git a/tests/auto/qtimer/tst_qtimer.cpp b/tests/auto/qtimer/tst_qtimer.cpp index cc97e4e..a0408ef 100644 --- a/tests/auto/qtimer/tst_qtimer.cpp +++ b/tests/auto/qtimer/tst_qtimer.cpp @@ -85,6 +85,7 @@ private slots: void timerFiresOnlyOncePerProcessEvents(); void timerIdPersistsAfterThreadExit(); void cancelLongTimer(); + void singleShotStaticFunctionZeroTimeout(); }; class TimerHelper : public QObject @@ -611,5 +612,16 @@ void tst_QTimer::cancelLongTimer() QVERIFY(!timer.isActive()); } +void tst_QTimer::singleShotStaticFunctionZeroTimeout() +{ + TimerHelper helper; + + QTimer::singleShot(0, &helper, SLOT(timeout())); + QTest::qWait(500); + QCOMPARE(helper.count, 1); + QTest::qWait(500); + QCOMPARE(helper.count, 1); +} + QTEST_MAIN(tst_QTimer) #include "tst_qtimer.moc" -- cgit v0.12