diff options
author | Thorbjørn Martsum <tmartsum@gmail.com> | 2013-07-31 07:01:58 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-16 12:31:52 +0200 |
commit | 8b86443e36bc3102e97be4f40f381151d19fd1a5 (patch) | |
tree | e48093aa5b7b8deb62a7f85ad4e0e60365dd2181 /src/corelib/tools/qvector.h | |
parent | 9b093871a683650011f2c317441e3879df4688eb (diff) |
QVector - optimize removeLast (and takeLast + pop_back)
In our benchmarks this makes removeLast more than twice as fast.
(That is on my core I7 laptop with gcc on linux).
It changes the alloc test to be an assert rather than an if.
Change-Id: Id55195b9b7880e54a89be4dd9d6228d94aff23c7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qvector.h')
-rw-r--r-- | src/corelib/tools/qvector.h | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index de64d7a670..288404f3c2 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -234,6 +234,7 @@ private: friend class QRegion; // Optimization for QRegion::rects() void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default); + void reallocData(const int sz) { reallocData(sz, d->alloc); } void freeData(Data *d); void defaultConstruct(T *from, T *to); void copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom); @@ -563,18 +564,17 @@ void QVector<T>::append(const T &t) } template <typename T> -inline void QVector<T>::removeLast() +void QVector<T>::removeLast() { Q_ASSERT(!isEmpty()); + Q_ASSERT(d->alloc); - if (d->alloc) { - if (d->ref.isShared()) { - reallocData(d->size - 1, int(d->alloc)); - return; - } - if (QTypeInfo<T>::isComplex) - (d->data() + d->size - 1)->~T(); + if (!d->ref.isShared()) { --d->size; + if (QTypeInfo<T>::isComplex) + (d->data() + d->size)->~T(); + } else { + reallocData(d->size - 1); } } |