Skip to content

Commit

Permalink
QEventDispatcherWin32Private: Get rid of TimerInfo struct list
Browse files Browse the repository at this point in the history
It was redundant with the timer info dictionary, and was even used
where using the dictionary would have been more efficient.

Change-Id: Ia656bf9b56c61e23df9f8743d8f8efbf65d37574
Reviewed-by: Oswald Buddenhagen <[email protected]>
Reviewed-by: Alex Trotsenko <[email protected]>
  • Loading branch information
maksqwe committed Nov 26, 2020
1 parent 9a09087 commit cc1c40c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
48 changes: 20 additions & 28 deletions src/corelib/kernel/qeventdispatcher_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,6 @@ void QEventDispatcherWin32::registerTimer(int timerId, qint64 interval, Qt::Time

d->registerTimer(t);

d->timerVec.append(t); // store in timer vector
d->timerDict.insert(t->timerId, t); // store timers in dict
}

Expand All @@ -740,15 +739,11 @@ bool QEventDispatcherWin32::unregisterTimer(int timerId)
#endif

Q_D(QEventDispatcherWin32);
if (d->timerVec.isEmpty() || timerId <= 0)
return false;

WinTimerInfo *t = d->timerDict.value(timerId);
WinTimerInfo *t = d->timerDict.take(timerId);
if (!t)
return false;

d->timerDict.remove(t->timerId);
d->timerVec.removeAll(t);
d->unregisterTimer(t);
return true;
}
Expand All @@ -767,16 +762,18 @@ bool QEventDispatcherWin32::unregisterTimers(QObject *object)
#endif

Q_D(QEventDispatcherWin32);
if (d->timerVec.isEmpty())
if (d->timerDict.isEmpty())
return false;
WinTimerInfo *t;
for (int i=0; i<d->timerVec.size(); i++) {
t = d->timerVec.at(i);
if (t && t->obj == object) { // object found
d->timerDict.remove(t->timerId);
d->timerVec.removeAt(i);

auto it = d->timerDict.begin();
while (it != d->timerDict.end()) {
WinTimerInfo *t = it.value();
Q_ASSERT(t);
if (t->obj == object) {
it = d->timerDict.erase(it);
d->unregisterTimer(t);
--i;
} else {
++it;
}
}
return true;
Expand All @@ -794,8 +791,9 @@ QEventDispatcherWin32::registeredTimers(QObject *object) const

Q_D(const QEventDispatcherWin32);
QList<TimerInfo> list;
for (const WinTimerInfo *t : qAsConst(d->timerVec)) {
if (t && t->obj == object)
for (WinTimerInfo *t : qAsConst(d->timerDict)) {
Q_ASSERT(t);
if (t->obj == object)
list << TimerInfo(t->timerId, t->interval, t->timerType);
}
return list;
Expand All @@ -812,17 +810,12 @@ int QEventDispatcherWin32::remainingTime(int timerId)

Q_D(QEventDispatcherWin32);

if (d->timerVec.isEmpty())
return -1;

quint64 currentTime = qt_msectime();

for (const WinTimerInfo *t : qAsConst(d->timerVec)) {
if (t && t->timerId == timerId) {
// timer found, return time to wait

return t->timeout > currentTime ? t->timeout - currentTime : 0;
}
WinTimerInfo *t = d->timerDict.value(timerId);
if (t) {
// timer found, return time to wait
return t->timeout > currentTime ? t->timeout - currentTime : 0;
}

#ifndef QT_NO_DEBUG
Expand Down Expand Up @@ -866,9 +859,8 @@ void QEventDispatcherWin32::closingDown()
Q_ASSERT(d->active_fd.isEmpty());

// clean up any timers
for (int i = 0; i < d->timerVec.count(); ++i)
d->unregisterTimer(d->timerVec.at(i));
d->timerVec.clear();
for (WinTimerInfo *t : qAsConst(d->timerDict))
d->unregisterTimer(t);
d->timerDict.clear();

d->closingDown = true;
Expand Down
2 changes: 0 additions & 2 deletions src/corelib/kernel/qeventdispatcher_win_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ class QZeroTimerEvent : public QTimerEvent
{ t = QEvent::ZeroTimerEvent; }
};

typedef QList<WinTimerInfo*> WinTimerVec; // vector of TimerInfo structs
typedef QHash<int, WinTimerInfo*> WinTimerDict; // fast dict of timers

class Q_CORE_EXPORT QEventDispatcherWin32Private : public QAbstractEventDispatcherPrivate
Expand All @@ -162,7 +161,6 @@ class Q_CORE_EXPORT QEventDispatcherWin32Private : public QAbstractEventDispatch
QAtomicInt wakeUps;

// timers
WinTimerVec timerVec;
WinTimerDict timerDict;
void registerTimer(WinTimerInfo *t);
void unregisterTimer(WinTimerInfo *t);
Expand Down

0 comments on commit cc1c40c

Please sign in to comment.