summaryrefslogtreecommitdiffstats
path: root/src/network/access/qhttpnetworkreply.cpp
diff options
context:
space:
mode:
authorMarkus Goetz <Markus.Goetz@nokia.com>2009-07-15 10:40:53 (GMT)
committerMarkus Goetz <Markus.Goetz@nokia.com>2009-07-17 11:36:17 (GMT)
commit8ab072aff0527d3ef3e44cf1ceba7dca985a6f94 (patch)
tree6a7a77a5dcea90c00ebc66c735c19af731b042f3 /src/network/access/qhttpnetworkreply.cpp
parent430f93c3649aacea5d9ccab047f036027f0622ea (diff)
downloadQt-8ab072aff0527d3ef3e44cf1ceba7dca985a6f94.zip
Qt-8ab072aff0527d3ef3e44cf1ceba7dca985a6f94.tar.gz
Qt-8ab072aff0527d3ef3e44cf1ceba7dca985a6f94.tar.bz2
QNetworkAccessManager: HTTP download performance improvements
Better usage of move semantics with implicit sharing to avoid detaching (=malloc/memcpy). Also some other improvements. Download performance improvement is around 20% according to the httpDownloadPerformance autotest. Reviewed-by: Thiago Macieira
Diffstat (limited to 'src/network/access/qhttpnetworkreply.cpp')
-rw-r--r--src/network/access/qhttpnetworkreply.cpp78
1 files changed, 35 insertions, 43 deletions
diff --git a/src/network/access/qhttpnetworkreply.cpp b/src/network/access/qhttpnetworkreply.cpp
index 7a616aa..2fe0d78 100644
--- a/src/network/access/qhttpnetworkreply.cpp
+++ b/src/network/access/qhttpnetworkreply.cpp
@@ -176,15 +176,6 @@ qint64 QHttpNetworkReply::bytesAvailableNextBlock() const
return -1;
}
-QByteArray QHttpNetworkReply::read(qint64 maxSize)
-{
- Q_D(QHttpNetworkReply);
- QByteArray data;
- if (d->connection)
- d->connection->d_func()->read(*this, data, maxSize);
- return data;
-}
-
QByteArray QHttpNetworkReply::readAny()
{
Q_D(QHttpNetworkReply);
@@ -203,7 +194,7 @@ QHttpNetworkReplyPrivate::QHttpNetworkReplyPrivate(const QUrl &newUrl)
majorVersion(0), minorVersion(0), bodyLength(0), contentRead(0), totalProgress(0),
chunkedTransferEncoding(0),
currentChunkSize(0), currentChunkRead(0), connection(0), initInflate(false),
- autoDecompress(false), responseData(0), requestIsPrepared(false)
+ autoDecompress(false), responseData(), requestIsPrepared(false)
{
}
@@ -561,17 +552,19 @@ bool QHttpNetworkReplyPrivate::connectionCloseEnabled()
// note this function can only be used for non-chunked, non-compressed with
// known content length
-qint64 QHttpNetworkReplyPrivate::readBodyFast(QAbstractSocket *socket, QRingBuffer *rb)
-{
- quint64 toBeRead = qMin(socket->bytesAvailable(), bodyLength - contentRead);
- char* dst = rb->reserve(toBeRead);
- qint64 haveRead = socket->read(dst, toBeRead);
+qint64 QHttpNetworkReplyPrivate::readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb)
+{
+ qint64 toBeRead = qMin(socket->bytesAvailable(), bodyLength - contentRead);
+ QByteArray bd;
+ bd.resize(toBeRead);
+ qint64 haveRead = socket->read(bd.data(), bd.size());
if (haveRead == -1) {
- rb->chop(toBeRead);
+ bd.clear();
return 0; // ### error checking here;
}
+ bd.resize(haveRead);
- rb->chop(toBeRead - haveRead);
+ rb->append(bd);
if (contentRead + haveRead == bodyLength) {
state = AllDoneState;
@@ -583,7 +576,7 @@ qint64 QHttpNetworkReplyPrivate::readBodyFast(QAbstractSocket *socket, QRingBuff
}
-qint64 QHttpNetworkReplyPrivate::readBody(QAbstractSocket *socket, QIODevice *out)
+qint64 QHttpNetworkReplyPrivate::readBody(QAbstractSocket *socket, QByteDataBuffer *out)
{
qint64 bytes = 0;
if (isChunked()) {
@@ -601,33 +594,35 @@ qint64 QHttpNetworkReplyPrivate::readBody(QAbstractSocket *socket, QIODevice *ou
return bytes;
}
-qint64 QHttpNetworkReplyPrivate::readReplyBodyRaw(QIODevice *in, QIODevice *out, qint64 size)
+qint64 QHttpNetworkReplyPrivate::readReplyBodyRaw(QIODevice *in, QByteDataBuffer *out, qint64 size)
{
qint64 bytes = 0;
Q_ASSERT(in);
Q_ASSERT(out);
int toBeRead = qMin<qint64>(128*1024, qMin<qint64>(size, in->bytesAvailable()));
- QByteArray raw(toBeRead, 0);
- while (size > 0) {
- qint64 read = in->read(raw.data(), raw.size());
- if (read == 0)
- return bytes;
- // ### error checking here
- qint64 written = out->write(raw.data(), read);
- if (written == 0)
+ while (toBeRead > 0) {
+ QByteArray byteData;
+ byteData.resize(toBeRead);
+ qint64 haveRead = in->read(byteData.data(), byteData.size());
+ if (haveRead <= 0) {
+ // ### error checking here
+ byteData.clear();
return bytes;
- if (read != written)
- qDebug() << "### read" << read << "written" << written;
- bytes += read;
- size -= read;
- out->waitForBytesWritten(-1); // throttle
+ }
+
+ byteData.resize(haveRead);
+ out->append(byteData);
+ bytes += haveRead;
+ size -= haveRead;
+
+ toBeRead = qMin<qint64>(128*1024, qMin<qint64>(size, in->bytesAvailable()));
}
return bytes;
}
-qint64 QHttpNetworkReplyPrivate::readReplyBodyChunked(QIODevice *in, QIODevice *out)
+qint64 QHttpNetworkReplyPrivate::readReplyBodyChunked(QIODevice *in, QByteDataBuffer *out)
{
qint64 bytes = 0;
while (in->bytesAvailable()) { // while we can read from input
@@ -648,17 +643,14 @@ qint64 QHttpNetworkReplyPrivate::readReplyBodyChunked(QIODevice *in, QIODevice *
state = AllDoneState;
break;
}
- // otherwise, read data
- qint64 readSize = qMin(in->bytesAvailable(), currentChunkSize - currentChunkRead);
- QByteArray buffer(readSize, 0);
- qint64 read = in->read(buffer.data(), readSize);
- bytes += read;
- currentChunkRead += read;
- qint64 written = out->write(buffer);
- Q_UNUSED(written); // Avoid compile warning when building release
- Q_ASSERT(read == written);
+
+ // otherwise, try to read what is missing for this chunk
+ qint64 haveRead = readReplyBodyRaw (in, out, currentChunkSize - currentChunkRead);
+ currentChunkRead += haveRead;
+ bytes += haveRead;
+
// ### error checking here
- out->waitForBytesWritten(-1);
+
}
return bytes;
}