QIODevice: remove unneeded resize(1) in QIODevice::readLine()

AFAIU, the resize(1) was merely there to have result.size() return 1
in the initial iteration of the do loop. Since resize(1) is always
followed by another unconditional resize() in the do-while loop,
remove it and use "1 + readBytes" instead of result.size() in the
follow-up resize() call inside the do loop.

Saves one resize() call and the corresponding executable size and
runtime overhead.

Pick-to: 6.7 6.5
Change-Id: I507acab2a6b7d55217cd454ecbbaf1df0e1d18e6
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit d9579f0043a04e08b3954eb6bb58637fc25fba9a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Rym Bouabid 2024-08-28 17:36:51 +02:00 committed by Qt Cherry-pick Bot
parent ea1da56f84
commit e99987329c

View File

@ -1456,12 +1456,10 @@ QByteArray QIODevice::readLine(qint64 maxSize)
// Size is unknown, read incrementally.
maxSize = QByteArray::maxSize() - 1;
// The first iteration needs to leave an extra byte for the terminating null
result.resize(1);
qint64 readResult;
do {
result.resize(qsizetype(qMin(maxSize, qint64(result.size() + d->buffer.chunkSize()))));
// +1 since d->readLine() actually _writes_ a terminating NUL (### why does it?)
result.resize(qsizetype(qMin(maxSize, 1 + readBytes + d->buffer.chunkSize())));
readResult = d->readLine(result.data() + readBytes, result.size() - readBytes);
if (readResult > 0 || readBytes == 0)
readBytes += readResult;