Fix performance regression when appending one vector to another

When appending a QVector to an existing vector the code would
unconditionally realloc the vector instead of first checking
whether we can do without. This gives a quadratic behaviour when
repeatedly appending a vector to another.

Change-Id: I2cd81253e6a8aec0bc4402e6fbda262435080966
Reviewed-by: Qt Doc Bot <qt_docbot@qt-project.org>
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2012-09-07 10:02:04 +02:00 committed by Qt by Nokia
parent 5b1bc864a9
commit 3a48320fd6

View File

@ -650,8 +650,12 @@ QVector<T> &QVector<T>::fill(const T &from, int asize)
template <typename T>
QVector<T> &QVector<T>::operator+=(const QVector &l)
{
int newSize = d->size + l.d->size;
reallocData(d->size, newSize);
uint newSize = d->size + l.d->size;
const bool isTooSmall = newSize > d->alloc;
if (!isDetached() || isTooSmall) {
QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
reallocData(d->size, isTooSmall ? newSize : d->alloc, opt);
}
if (d->alloc) {
T *w = d->begin() + newSize;