QThreadPool: optimize enqueueTask() for common case
the most-common case is: queue is empty or filled with tasks of the same priority; so the runnable would be put at the end of queue. both checks are cheap for us. also avoid detach()'ing by using const iterators Change-Id: Iab2255f852211f9accc8d717f778671661210ef3 Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
This commit is contained in:
parent
95bd974163
commit
cbaf52b099
@ -47,15 +47,6 @@
|
|||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
inline bool operator<(int priority, const QPair<QRunnable *, int> &p)
|
|
||||||
{
|
|
||||||
return p.second < priority;
|
|
||||||
}
|
|
||||||
inline bool operator<(const QPair<QRunnable *, int> &p, int priority)
|
|
||||||
{
|
|
||||||
return priority < p.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
Q_GLOBAL_STATIC(QThreadPool, theInstance)
|
Q_GLOBAL_STATIC(QThreadPool, theInstance)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -209,15 +200,22 @@ bool QThreadPoolPrivate::tryStart(QRunnable *task)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool operator<(int priority, const QPair<QRunnable *, int> &p)
|
||||||
|
{ return p.second < priority; }
|
||||||
|
inline bool operator<(const QPair<QRunnable *, int> &p, int priority)
|
||||||
|
{ return priority < p.second; }
|
||||||
|
|
||||||
void QThreadPoolPrivate::enqueueTask(QRunnable *runnable, int priority)
|
void QThreadPoolPrivate::enqueueTask(QRunnable *runnable, int priority)
|
||||||
{
|
{
|
||||||
if (runnable->autoDelete())
|
if (runnable->autoDelete())
|
||||||
++runnable->ref;
|
++runnable->ref;
|
||||||
|
|
||||||
// put it on the queue
|
// put it on the queue
|
||||||
QList<QPair<QRunnable *, int> >::iterator at =
|
QList<QPair<QRunnable *, int> >::const_iterator begin = queue.constBegin();
|
||||||
qUpperBound(queue.begin(), queue.end(), priority);
|
QList<QPair<QRunnable *, int> >::const_iterator it = queue.constEnd();
|
||||||
queue.insert(at, qMakePair(runnable, priority));
|
if (it != begin && priority < (*(it - 1)).second)
|
||||||
|
it = qUpperBound(begin, --it, priority);
|
||||||
|
queue.insert(it - begin, qMakePair(runnable, priority));
|
||||||
runnableReady.wakeOne();
|
runnableReady.wakeOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user