From e812358ba8570823e4d600561d45361c2edaaa3b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 11 Feb 2015 15:14:34 +0100 Subject: Animation classes: micro-optimize loops. Don't use index-based iteration, but use iterators assigning end() to a variable. Change-Id: Ia87532349855fa3baa748b62224fc8adf3395dc0 Reviewed-by: Marc Mutz --- src/corelib/animation/qabstractanimation.cpp | 11 ++-- src/corelib/animation/qparallelanimationgroup.cpp | 61 ++++++++++------------ .../animation/qsequentialanimationgroup.cpp | 7 +-- 3 files changed, 40 insertions(+), 39 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/animation/qabstractanimation.cpp b/src/corelib/animation/qabstractanimation.cpp index 7599c18cf2..7f2f01072f 100644 --- a/src/corelib/animation/qabstractanimation.cpp +++ b/src/corelib/animation/qabstractanimation.cpp @@ -154,6 +154,9 @@ QT_BEGIN_NAMESPACE +typedef QList::ConstIterator TimerListConstIt; +typedef QList::ConstIterator AnimationListConstIt; + /*! \class QAbstractAnimationTimer \inmodule QtCore @@ -497,8 +500,8 @@ void QUnifiedTimer::resumeAnimationTimer(QAbstractAnimationTimer *timer) int QUnifiedTimer::closestPausedAnimationTimerTimeToFinish() { int closestTimeToFinish = INT_MAX; - for (int i = 0; i < pausedAnimationTimers.size(); ++i) { - int timeToFinish = pausedAnimationTimers.at(i)->pauseDuration; + for (TimerListConstIt it = pausedAnimationTimers.constBegin(), cend = pausedAnimationTimers.constEnd(); it != cend; ++it) { + const int timeToFinish = (*it)->pauseDuration; if (timeToFinish < closestTimeToFinish) closestTimeToFinish = timeToFinish; } @@ -726,8 +729,8 @@ void QAnimationTimer::unregisterRunningAnimation(QAbstractAnimation *animation) int QAnimationTimer::closestPauseAnimationTimeToFinish() { int closestTimeToFinish = INT_MAX; - for (int i = 0; i < runningPauseAnimations.size(); ++i) { - QAbstractAnimation *animation = runningPauseAnimations.at(i); + for (AnimationListConstIt it = runningPauseAnimations.constBegin(), cend = runningPauseAnimations.constEnd(); it != cend; ++it) { + const QAbstractAnimation *animation = *it; int timeToFinish; if (animation->direction() == QAbstractAnimation::Forward) diff --git a/src/corelib/animation/qparallelanimationgroup.cpp b/src/corelib/animation/qparallelanimationgroup.cpp index be8d928694..d5a3ee751b 100644 --- a/src/corelib/animation/qparallelanimationgroup.cpp +++ b/src/corelib/animation/qparallelanimationgroup.cpp @@ -70,6 +70,10 @@ QT_BEGIN_NAMESPACE +typedef QList::ConstIterator AnimationListConstIt; +typedef QHash::Iterator AnimationTimeHashIt; +typedef QHash::ConstIterator AnimationTimeHashConstIt; + /*! Constructs a QParallelAnimationGroup. \a parent is passed to QObject's constructor. @@ -103,9 +107,8 @@ int QParallelAnimationGroup::duration() const Q_D(const QParallelAnimationGroup); int ret = 0; - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); - const int currentDuration = animation->totalDuration(); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + const int currentDuration = (*it)->totalDuration(); if (currentDuration == -1) return -1; // Undetermined length @@ -128,16 +131,16 @@ void QParallelAnimationGroup::updateCurrentTime(int currentTime) // simulate completion of the loop int dura = duration(); if (dura > 0) { - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + QAbstractAnimation *animation = (*it); if (animation->state() != QAbstractAnimation::Stopped) - d->animations.at(i)->setCurrentTime(dura); // will stop + animation->setCurrentTime(dura); // will stop } } } else if (d->currentLoop < d->lastLoop) { // simulate completion of the loop seeking backwards - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + QAbstractAnimation *animation = *it; //we need to make sure the animation is in the right state //and then rewind it d->applyGroupState(animation); @@ -151,8 +154,8 @@ void QParallelAnimationGroup::updateCurrentTime(int currentTime) __LINE__, d->currentTime, d->currentLoop, d->lastLoop, timeFwd, d->lastCurrentTime, state()); #endif // finally move into the actual time of the current loop - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + QAbstractAnimation *animation = *it; const int dura = animation->totalDuration(); //if the loopcount is bigger we should always start all animations if (d->currentLoop > d->lastLoop @@ -183,19 +186,20 @@ void QParallelAnimationGroup::updateState(QAbstractAnimation::State newState, switch (newState) { case Stopped: - for (int i = 0; i < d->animations.size(); ++i) - d->animations.at(i)->stop(); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) + (*it)->stop(); d->disconnectUncontrolledAnimations(); break; case Paused: - for (int i = 0; i < d->animations.size(); ++i) - if (d->animations.at(i)->state() == Running) - d->animations.at(i)->pause(); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + if ((*it)->state() == Running) + (*it)->pause(); + } break; case Running: d->connectUncontrolledAnimations(); - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + QAbstractAnimation *animation = *it; if (oldState == Stopped) animation->stop(); animation->setDirection(d->direction); @@ -215,14 +219,12 @@ void QParallelAnimationGroupPrivate::_q_uncontrolledAnimationFinished() int uncontrolledRunningCount = 0; if (animation->duration() == -1 || animation->loopCount() < 0) { - QHash::iterator it = uncontrolledFinishTime.begin(); - while (it != uncontrolledFinishTime.end()) { + for (AnimationTimeHashIt it = uncontrolledFinishTime.begin(), cend = uncontrolledFinishTime.end(); it != cend; ++it) { if (it.key() == animation) { *it = animation->currentTime(); } if (it.value() == -1) ++uncontrolledRunningCount; - ++it; } } @@ -230,8 +232,8 @@ void QParallelAnimationGroupPrivate::_q_uncontrolledAnimationFinished() return; int maxDuration = 0; - for (int i = 0; i < animations.size(); ++i) - maxDuration = qMax(maxDuration, animations.at(i)->totalDuration()); + for (AnimationListConstIt it = animations.constBegin(), cend = animations.constEnd(); it != cend; ++it) + maxDuration = qMax(maxDuration, (*it)->totalDuration()); if (currentTime >= maxDuration) q->stop(); @@ -239,19 +241,16 @@ void QParallelAnimationGroupPrivate::_q_uncontrolledAnimationFinished() void QParallelAnimationGroupPrivate::disconnectUncontrolledAnimations() { - QHash::iterator it = uncontrolledFinishTime.begin(); - while (it != uncontrolledFinishTime.end()) { + for (AnimationTimeHashConstIt it = uncontrolledFinishTime.constBegin(), cend = uncontrolledFinishTime.constEnd(); it != cend; ++it) disconnectUncontrolledAnimation(it.key()); - ++it; - } uncontrolledFinishTime.clear(); } void QParallelAnimationGroupPrivate::connectUncontrolledAnimations() { - for (int i = 0; i < animations.size(); ++i) { - QAbstractAnimation *animation = animations.at(i); + for (AnimationListConstIt it = animations.constBegin(), cend = animations.constEnd(); it != cend; ++it) { + QAbstractAnimation *animation = *it; if (animation->duration() == -1 || animation->loopCount() < 0) { uncontrolledFinishTime[animation] = -1; connectUncontrolledAnimation(animation); @@ -309,10 +308,8 @@ void QParallelAnimationGroup::updateDirection(QAbstractAnimation::Direction dire Q_D(QParallelAnimationGroup); //we need to update the direction of the current animation if (state() != Stopped) { - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); - animation->setDirection(direction); - } + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) + (*it)->setDirection(direction); } else { if (direction == Forward) { d->lastLoop = 0; diff --git a/src/corelib/animation/qsequentialanimationgroup.cpp b/src/corelib/animation/qsequentialanimationgroup.cpp index 059d62bdca..5a0982019f 100644 --- a/src/corelib/animation/qsequentialanimationgroup.cpp +++ b/src/corelib/animation/qsequentialanimationgroup.cpp @@ -81,6 +81,8 @@ QT_BEGIN_NAMESPACE +typedef QList::ConstIterator AnimationListConstIt; + bool QSequentialAnimationGroupPrivate::atEnd() const { // we try to detect if we're at the end of the group @@ -310,9 +312,8 @@ int QSequentialAnimationGroup::duration() const Q_D(const QSequentialAnimationGroup); int ret = 0; - for (int i = 0; i < d->animations.size(); ++i) { - QAbstractAnimation *animation = d->animations.at(i); - const int currentDuration = animation->totalDuration(); + for (AnimationListConstIt it = d->animations.constBegin(), cend = d->animations.constEnd(); it != cend; ++it) { + const int currentDuration = (*it)->totalDuration(); if (currentDuration == -1) return -1; // Undetermined length -- cgit v1.2.3