QList/QByteArray/QString: Base GrowsBackwards heuristic on old size

If you grow from 10 to 100 characters then even if the point of
insertion was the end then it will get the GrowsBackwards option on
realloc. By basing it on the oldSize the intention of the position to
insert at is better clarified.

Change-Id: Ia73f4902e8356d94709556de5704cbfa0e1a3a56
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Mårten Nordheim 2020-09-01 11:10:40 +02:00
parent 69d239ef00
commit 8eef75f0b5
3 changed files with 6 additions and 6 deletions

View File

@ -1962,7 +1962,7 @@ QByteArray &QByteArray::insert(qsizetype i, const char *str, qsizetype len)
// ### optimize me
if (d->needsDetach() || newSize > capacity() || shouldGrow) {
auto flags = d->detachFlags() | Data::GrowsForward;
if (i <= newSize / 4) // using QList's policy
if (i <= oldSize / 4) // using QList's policy
flags |= Data::GrowsBackwards;
reallocGrowData(newSize + 1, flags);
}
@ -2010,7 +2010,7 @@ QByteArray &QByteArray::insert(qsizetype i, qsizetype count, char ch)
// ### optimize me
if (d->needsDetach() || newSize > capacity() || shouldGrow) {
auto flags = d->detachFlags() | Data::GrowsForward;
if (i <= newSize / 4) // using QList's policy
if (i <= oldSize / 4) // using QList's policy
flags |= Data::GrowsBackwards;
reallocGrowData(newSize + 1, flags);
}

View File

@ -2690,7 +2690,7 @@ QString& QString::insert(qsizetype i, const QChar *unicode, qsizetype size)
const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin() + qMin(i, oldSize), size);
auto flags = d->detachFlags() | Data::GrowsForward;
if (i <= newSize / 4)
if (i <= oldSize / 4)
flags |= Data::GrowsBackwards;
// ### optimize me
@ -2724,7 +2724,7 @@ QString& QString::insert(qsizetype i, QChar ch)
const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin() + qMin(i, oldSize), 1);
auto flags = d->detachFlags() | Data::GrowsForward;
if (i <= newSize / 4)
if (i <= oldSize / 4)
flags |= Data::GrowsBackwards;
// ### optimize me

View File

@ -616,7 +616,7 @@ QList<T>::insert(qsizetype i, qsizetype n, parameter_type t)
const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin() + i, n);
if (d->needsDetach() || newSize > d->allocatedCapacity() || shouldGrow) {
typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward;
if (size_t(i) <= newSize / 4)
if (i <= d.size / 4)
flags |= Data::GrowsBackwards;
DataPointer detached(DataPointer::allocateGrow(d, newSize, flags));
@ -648,7 +648,7 @@ QList<T>::emplace(qsizetype i, Args&&... args)
const size_t newSize = size() + 1;
if (d->needsDetach() || newSize > d->allocatedCapacity() || shouldGrow) {
typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward;
if (size_t(i) <= newSize / 4)
if (i <= d.size / 4)
flags |= Data::GrowsBackwards;
DataPointer detached(DataPointer::allocateGrow(d, newSize, flags));