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.7 6.6 6.5
Change-Id: I071df68d51ba1dbd8b2eb5f94eb078a33223505f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ivan Solovev 2024-02-01 11:50:25 +01:00
parent dd514160ce
commit a1bfac287e
1 changed files with 3 additions and 2 deletions

View File

@ -1074,13 +1074,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) {
@ -1092,6 +1092,7 @@ QDataStream &QDataStream::readBytes(char *&s, qint64 &l)
return *this;
}
allocated += blockSize;
step *= 2;
} while (allocated < len);
s = curBuf;