From 9f591b3b6ca4d3c0de234f04bb9a2deb8d784170 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 12 Oct 2015 22:47:13 +0200 Subject: [PATCH] QVector: prevent resize() from shedding capacity ...even if reserve() hasn't been called before. [ChangeLog][QtCore][QVector] resize() will no longer shrink the capacity. That means resize(0) now reliably preserves capacity(). Task-number: QTBUG-39293 Done-with: Robin Burchell Change-Id: Ie7e4e597126832990b6cfb83bba875c3963b143e Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Milian Wolff Reviewed-by: Thiago Macieira --- src/corelib/tools/qvector.cpp | 3 +++ src/corelib/tools/qvector.h | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index 0fcba3c0c4..f1cf23cc6a 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -384,6 +384,9 @@ initialized with a \l{default-constructed value}. If \a size is less than the current size, elements are removed from the end. + Since Qt 5.6, resize() doesn't shrink the capacity anymore. + To shed excess capacity, use squeeze(). + \sa size() */ diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 890dbd317d..3ce33fb477 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -406,9 +406,6 @@ void QVector::resize(int asize) if (asize > oldAlloc) { // there is not enough space newAlloc = asize; opt = QArrayData::Grow; - } else if (!d->capacityReserved && asize < d->size && asize < (oldAlloc >> 1)) { // we want to shrink - newAlloc = asize; - opt = QArrayData::Grow; } else { newAlloc = oldAlloc; }