From b902b152af0bf144c9c9833502e1dcfae0166620 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 10 Jul 2023 18:48:17 +0300 Subject: [PATCH] QObject: replace _q_reregisterTimers with a lambda - Pass the QList by value, no heap allocation and no plain new/delete - A lambda means that there isn't runtime string-based lookup to find the member method in QObjectPrivate The code is only a couple of lines and used in a single place, so might as well move the code from _q_reregisterTimers to the local lambda. Modify tst_moc to account for the numer of methods in QObjectPrivate changing. The test had hardcoded numbers. Change-Id: I07906fc7138b8e601e4695f8d2de1b5fdd88449c Reviewed-by: Thiago Macieira --- src/corelib/kernel/qobject.cpp | 22 +++++++--------------- src/corelib/kernel/qobject.h | 1 - src/corelib/kernel/qobject_p.h | 1 - tests/auto/tools/moc/tst_moc.cpp | 30 ++++++++++++++++-------------- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index baad864a39..002b9bcab1 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -1451,8 +1451,13 @@ bool QObject::event(QEvent *e) Q_ASSERT_X(res, Q_FUNC_INFO, "QAbstractEventDispatcher::unregisterTimers() returned false," " but there are timers associated with this object."); - QMetaObject::invokeMethod(this, "_q_reregisterTimers", Qt::QueuedConnection, - Q_ARG(void*, (new QList(timers)))); + auto reRegisterTimers = [this, timers = std::move(timers)]() { + QAbstractEventDispatcher *eventDispatcher = + d_func()->threadData.loadRelaxed()->eventDispatcher.loadRelaxed(); + for (const auto &ti : timers) + eventDispatcher->registerTimer(ti.timerId, ti.interval, ti.timerType, this); + }; + QMetaObject::invokeMethod(this, std::move(reRegisterTimers), Qt::QueuedConnection); } } break; @@ -1808,19 +1813,6 @@ void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData } } -void QObjectPrivate::_q_reregisterTimers(void *pointer) -{ - Q_Q(QObject); - QList *timerList = reinterpret_cast *>(pointer); - QAbstractEventDispatcher *eventDispatcher = threadData.loadRelaxed()->eventDispatcher.loadRelaxed(); - for (int i = 0; i < timerList->size(); ++i) { - const QAbstractEventDispatcher::TimerInfo &ti = timerList->at(i); - eventDispatcher->registerTimer(ti.timerId, ti.interval, ti.timerType, q); - } - delete timerList; -} - - // // The timer flag hasTimer is set when startTimer is called. // It is not reset when killing the timer because more than diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 88ba5318a1..eb99504647 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -353,7 +353,6 @@ private: bool doSetProperty(const char *name, const QVariant *lvalue, QVariant *rvalue); Q_DISABLE_COPY(QObject) - Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *)) private: static QMetaObject::Connection connectImpl(const QObject *sender, void **signal, diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h index 41f1c2f820..f7886c7ce3 100644 --- a/src/corelib/kernel/qobject_p.h +++ b/src/corelib/kernel/qobject_p.h @@ -140,7 +140,6 @@ public: void setParent_helper(QObject *); void moveToThread_helper(); void setThreadData_helper(QThreadData *currentData, QThreadData *targetData, QBindingStatus *status); - void _q_reregisterTimers(void *pointer); bool isSender(const QObject *receiver, const char *signal) const; QObjectList receiverList(const char *signal) const; diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 0cd33288fc..58bc00b055 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -1589,41 +1589,43 @@ void tst_Moc::specifyMetaTagsFromCmdline() { void tst_Moc::invokable() { + const int fooIndex = 4; { const QMetaObject &mobj = InvokableBeforeReturnType::staticMetaObject; - QCOMPARE(mobj.methodCount(), 6); - QCOMPARE(mobj.method(5).methodSignature(), QByteArray("foo()")); + QCOMPARE(mobj.methodCount(), 5); + QCOMPARE(mobj.method(fooIndex).methodSignature(), QByteArray("foo()")); } { const QMetaObject &mobj = InvokableBeforeInline::staticMetaObject; - QCOMPARE(mobj.methodCount(), 7); - QCOMPARE(mobj.method(5).methodSignature(), QByteArray("foo()")); - QCOMPARE(mobj.method(6).methodSignature(), QByteArray("bar()")); + QCOMPARE(mobj.methodCount(), 6); + QCOMPARE(mobj.method(fooIndex).methodSignature(), QByteArray("foo()")); + QCOMPARE(mobj.method(fooIndex + 1).methodSignature(), QByteArray("bar()")); } } void tst_Moc::singleFunctionKeywordSignalAndSlot() { + const int mySignalIndex = 4; { const QMetaObject &mobj = SingleFunctionKeywordBeforeReturnType::staticMetaObject; - QCOMPARE(mobj.methodCount(), 7); - QCOMPARE(mobj.method(5).methodSignature(), QByteArray("mySignal()")); - QCOMPARE(mobj.method(6).methodSignature(), QByteArray("mySlot()")); + QCOMPARE(mobj.methodCount(), 6); + QCOMPARE(mobj.method(mySignalIndex).methodSignature(), QByteArray("mySignal()")); + QCOMPARE(mobj.method(mySignalIndex + 1).methodSignature(), QByteArray("mySlot()")); } { const QMetaObject &mobj = SingleFunctionKeywordBeforeInline::staticMetaObject; - QCOMPARE(mobj.methodCount(), 7); - QCOMPARE(mobj.method(5).methodSignature(), QByteArray("mySignal()")); - QCOMPARE(mobj.method(6).methodSignature(), QByteArray("mySlot()")); + QCOMPARE(mobj.methodCount(), 6); + QCOMPARE(mobj.method(mySignalIndex).methodSignature(), QByteArray("mySignal()")); + QCOMPARE(mobj.method(mySignalIndex + 1).methodSignature(), QByteArray("mySlot()")); } { const QMetaObject &mobj = SingleFunctionKeywordAfterInline::staticMetaObject; - QCOMPARE(mobj.methodCount(), 7); - QCOMPARE(mobj.method(5).methodSignature(), QByteArray("mySignal()")); - QCOMPARE(mobj.method(6).methodSignature(), QByteArray("mySlot()")); + QCOMPARE(mobj.methodCount(), 6); + QCOMPARE(mobj.method(mySignalIndex).methodSignature(), QByteArray("mySignal()")); + QCOMPARE(mobj.method(mySignalIndex + 1).methodSignature(), QByteArray("mySlot()")); } }