diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-11-04 11:12:05 +0100 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2020-11-09 17:36:04 +0100 |
commit | 2768270573c2707add47aebbfbbadaecaf828eec (patch) | |
tree | 92ac070bc26e3c343a3f3c51f7ff2676552053ae | |
parent | f43828687581c2d89549225d1cdc7518bde43b65 (diff) |
Do not shrink on remove()
This is in line with Qt 5 behavior, how std::vector works and also
how QString and QByteArray behave. If you need the shrink allocated
storage, call squeeze() manually.
Change-Id: I16cadd4f2a89bb2ec5de02fe69186f5da321cd06
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/doc/src/qt6-changes.qdoc | 7 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 21 | ||||
-rw-r--r-- | src/corelib/tools/qlist.qdoc | 20 |
3 files changed, 13 insertions, 35 deletions
diff --git a/src/corelib/doc/src/qt6-changes.qdoc b/src/corelib/doc/src/qt6-changes.qdoc index 4dd7488e1b..b92144a699 100644 --- a/src/corelib/doc/src/qt6-changes.qdoc +++ b/src/corelib/doc/src/qt6-changes.qdoc @@ -161,10 +161,9 @@ \section3 Stability of References There are several changes made to the QVector/QList implementation. The - QVector related ones are: insertion at the beginning is optimized (similarly - to QList in Qt 5) and element removal can reallocate in order to remove the - unused capacity. The QList related one is: memory layout for the elements is - simplified. + QVector related one is: insertion at the beginning is optimized (similarly + to QList in Qt 5). The QList related one is: memory layout for the elements + is simplified. \important These changes impact the stability of references. In Qt 6, you should consider any size or capacity modifying method to invalidate all diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 6fe3522f6f..f85d4c3610 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -639,22 +639,11 @@ inline void QList<T>::remove(qsizetype i, qsizetype n) if (n == 0) return; - const auto newSize = size() - n; - if (d->needsDetach() || - ((d->flags() & Data::CapacityReserved) == 0 - && newSize < d->allocatedCapacity()/2)) { - // allocate memory - DataPointer detached(Data::allocate(d->detachCapacity(newSize))); - const_iterator where = constBegin() + i; - if (newSize) { - detached->copyAppend(constBegin(), where); - detached->copyAppend(where + n, constEnd()); - } - d.swap(detached); - } else { - // we're detached and we can just move data around - d->erase(d->begin() + i, d->begin() + i + n); - } + if (d->needsDetach()) + d.detach(); + + d->erase(d->begin() + i, d->begin() + i + n); +} template <typename T> inline void QList<T>::removeFirst() diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc index acbc2f8d0e..33046f8a2f 100644 --- a/src/corelib/tools/qlist.qdoc +++ b/src/corelib/tools/qlist.qdoc @@ -501,11 +501,6 @@ may have used more memory than the normal QList growth strategy would have allocated—or you may have used less. - \note Calling reserve() changes the growth strategy of QList to the one that - avoids unnecessary reallocations. This may give worse performance when \a - size is underestimated. To restore automatic growth strategy, call - squeeze(). - \warning reserve() reserves memory but does not change the size of the list. Accessing data beyond the end of the list is undefined behavior. @@ -799,19 +794,14 @@ Removes \a n elements from the list, starting at index position \a i. //! [shrinking-erase] - Element removal may cause the list to shrink the capacity to reduce the - allocated memory size. The automatic shrinking only happens when the new - size is below some threshold (e.g. half of the capacity). This means that, - for large lists, removing a couple of elements often won't cause any side - effects. To make sure shrinking does not happen at all, use reserve() - beforehand to give a hint to the QList that the capacity should be - preserved. + Element removal will preserve the list's capacity and not reduce the amount of + allocated memory. To shed extra capacity and free as much memory as possible, + call squeeze(). //! [shrinking-erase] //! [iterator-invalidation-erase] - \note When QList is not \l{implicitly shared} and the capacity is reserved - with a call to reserve(), this function only invalidates iterators at or - after the specified position. + \note When QList is not \l{implicitly shared}, this function only + invalidates iterators at or after the specified position. //! [iterator-invalidation-erase] \sa insert(), replace(), fill() |