From 8eef75f0b52519f15078c29dfde5354ca7cccbdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Tue, 1 Sep 2020 11:10:40 +0200 Subject: [PATCH] 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 Reviewed-by: Lars Knoll --- src/corelib/text/qbytearray.cpp | 4 ++-- src/corelib/text/qstring.cpp | 4 ++-- src/corelib/tools/qlist.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 293b8f72fe..27986baeae 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -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); } diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index bf37f2e675..b7344353ec 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -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 diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index e94c108f0c..a704de54ea 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -616,7 +616,7 @@ QList::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::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));