From 8b86443e36bc3102e97be4f40f381151d19fd1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Wed, 31 Jul 2013 07:01:58 +0200 Subject: [PATCH] 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 --- src/corelib/tools/qvector.h | 16 ++++++++-------- 1 file 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::append(const T &t) } template -inline void QVector::removeLast() +void QVector::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::isComplex) - (d->data() + d->size - 1)->~T(); + if (!d->ref.isShared()) { --d->size; + if (QTypeInfo::isComplex) + (d->data() + d->size)->~T(); + } else { + reallocData(d->size - 1); } }