From 5dc13fe05e775d1621a5bfaa9e6a53c7ab82efee Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Fri, 19 Feb 2016 16:52:29 +0200 Subject: [PATCH] QIODevice: allow zero-copy in read() Try to prevent the data from being copied, if we have a chunk with the same size in the read buffer. Task-number: QTBUG-19169 Change-Id: I2a9a5c88855988888b56d0ca69ec4e50b8e6ef98 Reviewed-by: Allan Sandfeld Jensen --- src/corelib/io/qiodevice.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index fd204b00de..ee004b4293 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -1148,13 +1148,24 @@ QByteArray QIODevice::read(qint64 maxSize) Q_D(QIODevice); QByteArray result; - CHECK_MAXLEN(read, result); - #if defined QIODEVICE_DEBUG printf("%p QIODevice::read(%lld), d->pos = %lld, d->buffer.size() = %lld\n", this, maxSize, d->pos, d->buffer.size()); #endif + // Try to prevent the data from being copied, if we have a chunk + // with the same size in the read buffer. + if (maxSize == d->buffer.nextDataBlockSize() && !d->transactionStarted + && (d->openMode & (QIODevice::ReadOnly | QIODevice::Text)) == QIODevice::ReadOnly) { + result = d->buffer.read(); + if (!d->isSequential()) + d->pos += maxSize; + if (d->buffer.isEmpty()) + readData(nullptr, 0); + return result; + } + + CHECK_MAXLEN(read, result); if (maxSize >= MaxByteArraySize) { checkWarnMessage(this, "read", "maxSize argument exceeds QByteArray size limit"); maxSize = MaxByteArraySize - 1;