From 3eb9061b6344a1de8973fc14085618b0c46909ae Mon Sep 17 00:00:00 2001 From: Alexander Golubev Date: Mon, 11 Mar 2024 13:47:16 +0300 Subject: Deduplicate code in TQObject::activate_signal() According to a benchmark the while() loop with direct use of iterators were ~10% slower than the current one which handled case when there is only one connection to a signal separately. But use of a range-based for loop sped up even this case for ~1.5%. Closes: https://mirror.git.trinitydesktop.org/gitea/TDE/tqt3/issues/121 Signed-off-by: Alexander Golubev --- src/kernel/qobject.cpp | 133 +++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 103 deletions(-) diff --git a/src/kernel/qobject.cpp b/src/kernel/qobject.cpp index 3ab2d9b97..44971cfe6 100644 --- a/src/kernel/qobject.cpp +++ b/src/kernel/qobject.cpp @@ -2785,14 +2785,15 @@ void TQObject::activate_signal( TQConnectionList *clist, TQUObject *o ) const TQThread *currentThread = TQThread::currentThreadObject(); #endif // TQT_THREAD_SUPPORT - TQObject *object; - TQSenderObjectList* sol; - TQObject* oldSender = 0; - TQConnection *c; - if ( clist->count() == 1 ) { // save iterator - c = clist->first(); - object = c->object(); - sol = object->senderObjects; + const TQConnection *cd = 0; + for(const TQConnection *c: *clist) { + Q_ASSERT( c ); + if ( c == cd ) + continue; + cd = c; + TQObject *object = c->object(); + TQObject *oldSender; + TQSenderObjectList* sol = object->senderObjects; #ifdef TQT_THREAD_SUPPORT sol->listMutex->lock(); #endif // TQT_THREAD_SUPPORT @@ -2802,13 +2803,12 @@ void TQObject::activate_signal( TQConnectionList *clist, TQUObject *o ) sol->currentSender = this; } if ( c->memberType() == TQ_SIGNAL_CODE ) { -#if !defined(TQT_THREAD_SUPPORT) - object->tqt_emit( c->member(), o ); -#else // defined(TQT_THREAD_SUPPORT) - if ((d->disableThreadPostedEvents) || - (object->d->disableThreadPostedEvents) || - (currentThread && currentThread->threadPostedEventsDisabled()) || - (currentThread && object->d->ownThread == currentThread)) { +#ifdef TQT_THREAD_SUPPORT + if ((d->disableThreadPostedEvents) || + (object->d->disableThreadPostedEvents) || + (currentThread && currentThread->threadPostedEventsDisabled()) || + (currentThread && object->d->ownThread == currentThread) + ) { sol->listMutex->unlock(); object->tqt_emit( c->member(), o ); sol->listMutex->lock(); @@ -2816,21 +2816,22 @@ void TQObject::activate_signal( TQConnectionList *clist, TQUObject *o ) else { if (object->d->ownThread && !object->d->ownThread->finished()) { #ifdef QT_DEBUG - tqDebug("TQObject::activate_signal: Emitting cross-thread signal from object %p (member %d receiver %p) (1)\n", this, c->member(), object); + tqDebug("TQObject::activate_signal: Emitting cross-thread signal from object %p (member %d receiver %p)\n", this, c->member(), object); #endif // QT_DEBUG TQApplication::postEvent(object, new TQMetaCallEvent(c->member(), this, deepCopyTQUObjectArray(o), TQMetaCallEvent::MetaCallEmit)); } } -#endif // !defined(TQT_THREAD_SUPPORT) +#else // !TQT_THREAD_SUPPORT + object->tqt_emit( c->member(), o ); +#endif // TQT_THREAD_SUPPORT } else { -#if !defined(TQT_THREAD_SUPPORT) - object->tqt_invoke( c->member(), o ); -#else // defined(TQT_THREAD_SUPPORT) - if ((d->disableThreadPostedEvents) || - (object->d->disableThreadPostedEvents) || - (currentThread && currentThread->threadPostedEventsDisabled()) || - (currentThread && object->d->ownThread == currentThread)) { +#ifdef TQT_THREAD_SUPPORT + if ((d->disableThreadPostedEvents) || + (object->d->disableThreadPostedEvents) || + (currentThread && currentThread->threadPostedEventsDisabled()) || + (currentThread && object->d->ownThread == currentThread) + ) { sol->listMutex->unlock(); object->tqt_invoke( c->member(), o ); sol->listMutex->lock(); @@ -2838,18 +2839,20 @@ void TQObject::activate_signal( TQConnectionList *clist, TQUObject *o ) else { if (object->d->ownThread && !object->d->ownThread->finished()) { #ifdef QT_DEBUG - tqDebug("TQObject::activate_signal: Invoking cross-thread method from object %p (member %d receiver %p) (1)\n", this, c->member(), object); + tqDebug("TQObject::activate_signal: Invoking cross-thread method from object %p (member %d receiver %p)\n", this, c->member(), object); #endif // QT_DEBUG TQApplication::postEvent(object, new TQMetaCallEvent(c->member(), this, deepCopyTQUObjectArray(o), TQMetaCallEvent::MetaCallInvoke)); } } -#endif // !defined(TQT_THREAD_SUPPORT) +#else // !TQT_THREAD_SUPPORT + object->tqt_invoke( c->member(), o ); +#endif // TQT_THREAD_SUPPORT } if ( sol ) { sol->currentSender = oldSender; if ( sol->deref() ) { #ifdef TQT_THREAD_SUPPORT - sol->listMutex->unlock(); + sol->listMutex->unlock(); #endif // TQT_THREAD_SUPPORT delete sol; sol = NULL; @@ -2858,82 +2861,6 @@ void TQObject::activate_signal( TQConnectionList *clist, TQUObject *o ) #ifdef TQT_THREAD_SUPPORT if (sol) sol->listMutex->unlock(); #endif // TQT_THREAD_SUPPORT - } else { - TQConnection *cd = 0; - TQConnectionListIt it(*clist); - while ( (c=it.current()) ) { - ++it; - if ( c == cd ) - continue; - cd = c; - object = c->object(); - sol = object->senderObjects; -#ifdef TQT_THREAD_SUPPORT - sol->listMutex->lock(); -#endif // TQT_THREAD_SUPPORT - if ( sol ) { - oldSender = sol->currentSender; - sol->ref(); - sol->currentSender = this; - } - if ( c->memberType() == TQ_SIGNAL_CODE ) { -#if !defined(TQT_THREAD_SUPPORT) - object->tqt_emit( c->member(), o ); -#else // defined(TQT_THREAD_SUPPORT) - if ((d->disableThreadPostedEvents) || - (object->d->disableThreadPostedEvents) || - (currentThread && currentThread->threadPostedEventsDisabled()) || - (currentThread && object->d->ownThread == currentThread)) { - sol->listMutex->unlock(); - object->tqt_emit( c->member(), o ); - sol->listMutex->lock(); - } - else { - if (object->d->ownThread && !object->d->ownThread->finished()) { -#ifdef QT_DEBUG - tqDebug("TQObject::activate_signal: Emitting cross-thread signal from object %p (member %d receiver %p) (2)\n", this, c->member(), object); -#endif // QT_DEBUG - TQApplication::postEvent(object, new TQMetaCallEvent(c->member(), this, deepCopyTQUObjectArray(o), TQMetaCallEvent::MetaCallEmit)); - } - } -#endif // !defined(TQT_THREAD_SUPPORT) - } - else { -#if !defined(TQT_THREAD_SUPPORT) - object->tqt_invoke( c->member(), o ); -#else // defined(TQT_THREAD_SUPPORT) - if ((d->disableThreadPostedEvents) || - (object->d->disableThreadPostedEvents) || - (currentThread && currentThread->threadPostedEventsDisabled()) || - (currentThread && object->d->ownThread == currentThread)) { - sol->listMutex->unlock(); - object->tqt_invoke( c->member(), o ); - sol->listMutex->lock(); - } - else { - if (object->d->ownThread && !object->d->ownThread->finished()) { -#ifdef QT_DEBUG - tqDebug("TQObject::activate_signal: Invoking cross-thread method from object %p (member %d receiver %p) (2)\n", this, c->member(), object); -#endif // QT_DEBUG - TQApplication::postEvent(object, new TQMetaCallEvent(c->member(), this, deepCopyTQUObjectArray(o), TQMetaCallEvent::MetaCallInvoke)); - } - } -#endif // !defined(TQT_THREAD_SUPPORT) - } - if (sol ) { - sol->currentSender = oldSender; - if ( sol->deref() ) { -#ifdef TQT_THREAD_SUPPORT - sol->listMutex->unlock(); -#endif // TQT_THREAD_SUPPORT - delete sol; - sol = NULL; - } - } -#ifdef TQT_THREAD_SUPPORT - if (sol) sol->listMutex->unlock(); -#endif // TQT_THREAD_SUPPORT - } } } -- cgit v1.2.1