diff options
author | Ritt Konstantin <ritt.ks@gmail.com> | 2011-07-13 16:14:38 (GMT) |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2011-08-12 16:39:52 (GMT) |
commit | b209fe3b1a51f64541067917e96de99f14ad65f3 (patch) | |
tree | 7da61ae8b8ca391643cac43c3a77e2c23b52db9f /src/corelib/tools | |
parent | bda5fd63ae9c1aa16492fad26dcabeffb8bd8db4 (diff) | |
download | Qt-b209fe3b1a51f64541067917e96de99f14ad65f3.zip Qt-b209fe3b1a51f64541067917e96de99f14ad65f3.tar.gz Qt-b209fe3b1a51f64541067917e96de99f14ad65f3.tar.bz2 |
optimize QList::removeAll()
a) don't detach until an occurrence found
b) don't memmove every time an occurrence found
c) truncate quickly )
well, numbers are better than words:
before:
RESULT : tst_QList::removeAll_primitive():
2,617,902 CPU ticks per iteration (total: 261,790,171, iterations: 100)
RESULT : tst_QList::removeAll_movable():
2,547,540 CPU ticks per iteration (total: 254,753,960, iterations: 100)
RESULT : tst_QList::removeAll_complex():
16,852,099 CPU ticks per iteration (total: 1,685,209,906, iterations: 100)
after:
RESULT : tst_QList::removeAll_primitive():
73,520 CPU ticks per iteration (total: 73,520,442, iterations: 1000)
RESULT : tst_QList::removeAll_movable():
90,422 CPU ticks per iteration (total: 90,422,464, iterations: 1000)
RESULT : tst_QList::removeAll_complex():
9,667,073 CPU ticks per iteration (total: 9,667,072,670, iterations: 1000)
Merge-request: 1285
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qlist.h | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 4eb05d6..e104f65 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -769,18 +769,26 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::clear() template <typename T> Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t) { - detachShared(); + int index = indexOf(_t); + if (index == -1) + return 0; + const T t = _t; - int removedCount=0, i=0; - Node *n; - while (i < p.size()) - if ((n = reinterpret_cast<Node *>(p.at(i)))->t() == t) { - node_destruct(n); - p.remove(i); - ++removedCount; - } else { - ++i; - } + detach(); + + Node *i = reinterpret_cast<Node *>(p.at(index)); + Node *e = reinterpret_cast<Node *>(p.end()); + Node *n = i; + node_destruct(i); + while (++i != e) { + if (i->t() == t) + node_destruct(i); + else + *n++ = *i; + } + + int removedCount = e - n; + d->end -= removedCount; return removedCount; } |