Skip to content

Commit

Permalink
QIODevice: remove unneeded resize(1) in QIODevice::readLine()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Marc Mutz <[email protected]>
(cherry picked from commit d9579f0)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
  • Loading branch information
Rym Bouabid authored and Qt Cherry-pick Bot committed Sep 18, 2024
1 parent ea1da56 commit e999873
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/corelib/io/qiodevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e999873

Please sign in to comment.