From a7885c9756d423042bd0670d82d78d8dffe9be54 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 16 Feb 2016 15:24:38 +0100 Subject: QVector: preserve capacity in clear() This is what std::vector implementations usually do, because it minimizes memory fragmentation and useless allocations since no user will call clear() unless she intends to append new data afterwards. Fix calls to resize(0) that show how existing code tried to work around the issue. Adjust test. Port from QVERIFY(==) to QCOMPARE as a drive-by. [ChangeLog][QtCore][QVector] clear() now preserves capacity. To shed capacity, call squeeze() or swap with a default-constructed QVector object, see the documentation for an example. Change-Id: I9cebe611a97e027a89e821e64408a4741b31f1f6 Reviewed-by: Lars Knoll --- src/corelib/kernel/qeventdispatcher_unix.cpp | 4 ++-- src/corelib/tools/qregexp.cpp | 2 +- src/corelib/tools/qvector.cpp | 15 +++++++++++++-- src/corelib/tools/qvector.h | 2 +- src/corelib/xml/qxmlstream_p.h | 6 ++---- 5 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp index 64b813bb13..9afbb84abf 100644 --- a/src/corelib/kernel/qeventdispatcher_unix.cpp +++ b/src/corelib/kernel/qeventdispatcher_unix.cpp @@ -286,7 +286,7 @@ void QEventDispatcherUNIXPrivate::markPendingSocketNotifiers() } } - pollfds.resize(0); + pollfds.clear(); } int QEventDispatcherUNIXPrivate::activateSocketNotifiers() @@ -480,8 +480,8 @@ bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags) if (!canWait || (include_timers && d->timerList.timerWait(wait_tm))) tm = &wait_tm; + d->pollfds.clear(); d->pollfds.reserve(1 + (include_notifiers ? d->socketNotifiers.size() : 0)); - d->pollfds.resize(0); if (include_notifiers) for (auto it = d->socketNotifiers.cbegin(); it != d->socketNotifiers.cend(); ++it) diff --git a/src/corelib/tools/qregexp.cpp b/src/corelib/tools/qregexp.cpp index fd67193302..f8f3347786 100644 --- a/src/corelib/tools/qregexp.cpp +++ b/src/corelib/tools/qregexp.cpp @@ -2335,7 +2335,7 @@ QRegExpCharClass::QRegExpCharClass() void QRegExpCharClass::clear() { c = 0; - r.resize(0); + r.clear(); n = false; } diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index b3247b8af5..ef1c9c17b0 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -502,8 +502,19 @@ /*! \fn void QVector::clear() - Removes all the elements from the vector and releases the memory used by - the vector. + Removes all the elements from the vector. + + \note Until Qt 5.6, this also released the memory used by + the vector. From Qt 5.7, the capacity is preserved. To shed + all capacity, swap with a default-constructed vector: + \code + QVector v ...; + QVector().swap(v); + Q_ASSERT(v.capacity() == 0); + \endcode + or call squeeze(). + + \sa squeeze() */ /*! \fn const T &QVector::at(int i) const diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index d60be78ade..13ae121450 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -419,7 +419,7 @@ void QVector::resize(int asize) } template inline void QVector::clear() -{ *this = QVector(); } +{ resize(0); } template inline const T &QVector::at(int i) const { Q_ASSERT_X(i >= 0 && i < d->size, "QVector::at", "index out of range"); diff --git a/src/corelib/xml/qxmlstream_p.h b/src/corelib/xml/qxmlstream_p.h index 4c6c206bf3..ab0bbba413 100644 --- a/src/corelib/xml/qxmlstream_p.h +++ b/src/corelib/xml/qxmlstream_p.h @@ -1020,10 +1020,8 @@ bool QXmlStreamReaderPrivate::parse() prefix.clear(); qualifiedName.clear(); namespaceUri.clear(); - if (publicNamespaceDeclarations.size()) - publicNamespaceDeclarations.clear(); - if (attributes.size()) - attributes.resize(0); + publicNamespaceDeclarations.clear(); + attributes.clear(); if (isEmptyElement) { setType(QXmlStreamReader::EndElement); Tag &tag = tagStack_pop(); -- cgit v1.2.3