From 6b884d2aa129e6f8d552cbaa9dbfa034eb0aca3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 28 Aug 2019 12:46:39 +0200 Subject: [PATCH] QByteArray: Don't realloc when resizing to a smaller size The same change was done for QVector and QString for 5.6 while QByteArray was not done/forgotten. For the sake of consistency we should converge on one type of behavior. The change mirrors the one for QString: 50ab7c16d470dbb0984bc2b48c5f49d94106edd0 [ChangeLog][QtCore][QByteArray] resize() will no longer shrink the capacity. That means resize(0) now reliably preserves capacity(). Change-Id: I4fcfa92f75fa472af0eaab567e1cdd62e6d336b0 Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index c50e087c103..444980e9c01 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1775,12 +1775,7 @@ void QByteArray::resize(int size) return; } - if (size == 0 && !d->capacityReserved) { - Data *x = Data::allocate(0); - if (!d->ref.deref()) - Data::deallocate(d); - d = x; - } else if (d->size == 0 && d->ref.isStatic()) { + if (d->size == 0 && d->ref.isStatic()) { // // Optimize the idiom: // QByteArray a; @@ -1795,9 +1790,7 @@ void QByteArray::resize(int size) x->data()[size] = '\0'; d = x; } else { - if (d->ref.isShared() || uint(size) + 1u > d->alloc - || (!d->capacityReserved && size < d->size - && uint(size) + 1u < uint(d->alloc >> 1))) + if (d->ref.isShared() || uint(size) + 1u > d->alloc) reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow); if (d->alloc) { d->size = size;