From be04a47a8b371a44b728f7b7a2be2f66749e562b Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 29 Jan 2024 14:36:10 +0100 Subject: [PATCH] QDataStream::readBytes(): only chunk when data is not all available If the underlying QDataStream's device already contains all data that we want to read, we can optimize the allocation algorithm, and try to allocate all needed memory at once. Use bytesAvailable() to determine if the underlying device can provide all requested data, and adjust the initial block size based on the result. If not all data is available, fall back to the geometric growth algorithm. Change-Id: I6384d2caa16c238c2dbb77b2ad761cbd8a44df6c Reviewed-by: Thiago Macieira --- src/corelib/serialization/qdatastream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp index 32f827c69e..fd550cf8bc 100644 --- a/src/corelib/serialization/qdatastream.cpp +++ b/src/corelib/serialization/qdatastream.cpp @@ -1074,7 +1074,7 @@ QDataStream &QDataStream::readBytes(char *&s, qint64 &l) return *this; } - qsizetype step = 1024 * 1024; + qsizetype step = (dev->bytesAvailable() >= len) ? len : 1024 * 1024; qsizetype allocated = 0; char *prevBuf = nullptr; char *curBuf = nullptr;