QList: iterate forward in count()/contains()

After much head-scratching, we found no reason for the backwards iteration.
Indeed, forward iteration should be slightly faster than backwards, because
it operates in the direction in which cache-lines are filled, usually.

This is in preparation of using std algorithms instead of hand-written
loops. It avoids having to use std::reverse_iterator.

Change-Id: Ib62cf0a6f2a33d186cb174b23b0d6bb2891b6c63
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2014-10-01 10:10:29 +02:00
parent b1482795ee
commit 5e01d3f642

View File

@ -921,9 +921,9 @@ Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const
template <typename T>
Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const
{
Node *b = reinterpret_cast<Node *>(p.begin());
Node *i = reinterpret_cast<Node *>(p.end());
while (i-- != b)
Node *e = reinterpret_cast<Node *>(p.end());
Node *i = reinterpret_cast<Node *>(p.begin());
for (; i != e; ++i)
if (i->t() == t)
return true;
return false;
@ -933,9 +933,9 @@ template <typename T>
Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const
{
int c = 0;
Node *b = reinterpret_cast<Node *>(p.begin());
Node *i = reinterpret_cast<Node *>(p.end());
while (i-- != b)
Node *e = reinterpret_cast<Node *>(p.end());
Node *i = reinterpret_cast<Node *>(p.begin());
for (; i != e; ++i)
if (i->t() == t)
++c;
return c;