From 3222db0937aaf1ae5681bc124406ec37f550bc7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Thu, 10 Jan 2013 19:42:59 +0100 Subject: QVector - removeLast optimize In case somebody uses QVector as a stack, it is not fair to have takeLast, removeLast and pop_back to do way too much work. This is still very slow compared to std::vector::pop_back (mostly due implicit sharing), however it is more than a factor faster than before. Change-Id: I636872675e80c8ca0c8ebc94b04f587a2dcd6d8d Reviewed-by: Thiago Macieira --- src/corelib/tools/qvector.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/corelib/tools/qvector.h') diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index e2c28e4060..a06fb7b4eb 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -139,7 +139,7 @@ public: void remove(int i); void remove(int i, int n); inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(d->begin()); } - inline void removeLast() { Q_ASSERT(!isEmpty()); erase(d->end() - 1); } + inline void removeLast(); inline T takeFirst() { Q_ASSERT(!isEmpty()); T r = first(); removeFirst(); return r; } inline T takeLast() { Q_ASSERT(!isEmpty()); T r = last(); removeLast(); return r; } @@ -557,6 +557,22 @@ void QVector::append(const T &t) ++d->size; } +template +inline void QVector::removeLast() +{ + Q_ASSERT(!isEmpty()); + + if (d->alloc) { + if (d->ref.isShared()) { + reallocData(d->size - 1, int(d->alloc)); + return; + } + if (QTypeInfo::isComplex) + (d->data() + d->size - 1)->~T(); + --d->size; + } +} + template typename QVector::iterator QVector::insert(iterator before, size_type n, const T &t) { @@ -606,6 +622,7 @@ typename QVector::iterator QVector::erase(iterator abegin, iterator aend) // FIXME we could do a proper realloc, which copy constructs only needed data. // FIXME we ara about to delete data maybe it is good time to shrink? + // FIXME the shrink is also an issue in removeLast, that is just a copy + reduce of this. if (d->alloc) { detach(); abegin = d->begin() + itemsUntouched; -- cgit v1.2.3