From 44dc38ebc9fa407565ec1699bdbb1f97ffd13f1c Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 1 Feb 2024 11:50:25 +0100 Subject: [PATCH] QDataStream::readBytes(): make the growth of the buffer geometric The algorithm tries to allocate the memory using 1 Mb blocks. When the input data is large, this results in a lot of reallocations, which is slow and inefficient. This patch modifies the algorithm in such way that the allocation size increases at each step, resulting in geometric growth of the buffer. Pick-to: 6.6 6.5 Change-Id: I071df68d51ba1dbd8b2eb5f94eb078a33223505f Reviewed-by: Thiago Macieira (cherry picked from commit a1bfac287ee5d3719646d68dc91dc8e8e4cec04e) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/serialization/qdatastream.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp index 442f5f1e53d..27a2ee9fdd6 100644 --- a/src/corelib/serialization/qdatastream.cpp +++ b/src/corelib/serialization/qdatastream.cpp @@ -1073,13 +1073,13 @@ QDataStream &QDataStream::readBytes(char *&s, qint64 &l) return *this; } - constexpr qsizetype Step = 1024 * 1024; + qsizetype step = 1024 * 1024; qsizetype allocated = 0; char *prevBuf = nullptr; char *curBuf = nullptr; do { - qsizetype blockSize = qMin(Step, len - allocated); + qsizetype blockSize = qMin(step, len - allocated); prevBuf = curBuf; curBuf = new char[allocated + blockSize + 1]; if (prevBuf) { @@ -1091,6 +1091,7 @@ QDataStream &QDataStream::readBytes(char *&s, qint64 &l) return *this; } allocated += blockSize; + step *= 2; } while (allocated < len); s = curBuf;