From 3a48320fd6335d21451f07cb68336404177e2d87 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 7 Sep 2012 10:02:04 +0200 Subject: 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 Reviewed-by: Simon Hausmann Reviewed-by: Thiago Macieira --- src/corelib/tools/qvector.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/corelib/tools/qvector.h') diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index f3ef9b97c3..e52fb9a1be 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -650,8 +650,12 @@ QVector &QVector::fill(const T &from, int asize) template QVector &QVector::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; -- cgit v1.2.3