From e99987329cbfd8d81ce7884d793fb4874892a4b9 Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Wed, 28 Aug 2024 17:36:51 +0200 Subject: [PATCH] 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 Reviewed-by: Marc Mutz (cherry picked from commit d9579f0043a04e08b3954eb6bb58637fc25fba9a) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qiodevice.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index 45af9ff476d..7a02675b981 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -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;