summaryrefslogtreecommitdiffstats
path: root/src/network/access/qhttpnetworkconnection.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/qhttpnetworkconnection.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/qhttpnetworkconnection.cpp')
-rw-r--r--src/network/access/qhttpnetworkconnection.cpp61
1 files changed, 33 insertions, 28 deletions
diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp
index f1da244..afcdf17 100644
--- a/src/network/access/qhttpnetworkconnection.cpp
+++ b/src/network/access/qhttpnetworkconnection.cpp
@@ -175,26 +175,43 @@ bool QHttpNetworkConnectionPrivate::isSocketReading(QAbstractSocket *socket) con
return (i != -1 && (channels[i].state & ReadingState));
}
+void QHttpNetworkConnectionPrivate::appendUncompressedData(QHttpNetworkReply &reply, QByteArray &qba)
+{
+ reply.d_func()->responseData.append(qba);
+
+ // clear the original! helps with implicit sharing and
+ // avoiding memcpy when the user is reading the data
+ qba.clear();
+}
-void QHttpNetworkConnectionPrivate::appendUncompressedData(QHttpNetworkReply &reply, const QByteArray &fragment)
+void QHttpNetworkConnectionPrivate::appendUncompressedData(QHttpNetworkReply &reply, QByteDataBuffer &data)
{
- char *dst = reply.d_func()->responseData.reserve(fragment.size());
- qMemCopy(dst, fragment.constData(), fragment.size());
+ reply.d_func()->responseData.append(data);
+
+ // clear the original! helps with implicit sharing and
+ // avoiding memcpy when the user is reading the data
+ data.clear();
}
-void QHttpNetworkConnectionPrivate::appendCompressedData(QHttpNetworkReply &reply, const QByteArray &fragment)
+void QHttpNetworkConnectionPrivate::appendCompressedData(QHttpNetworkReply &reply, QByteDataBuffer &data)
{
- reply.d_func()->compressedData.append(fragment);
+ // Work in progress: Later we will directly use a list of QByteArray or a QRingBuffer
+ // instead of one QByteArray.
+ for(int i = 0; i < data.bufferCount(); i++) {
+ QByteArray &byteData = data[i];
+ reply.d_func()->compressedData.append(byteData.constData(), byteData.size());
+ }
+ data.clear();
}
qint64 QHttpNetworkConnectionPrivate::uncompressedBytesAvailable(const QHttpNetworkReply &reply) const
{
- return reply.d_func()->responseData.size();
+ return reply.d_func()->responseData.byteAmount();
}
qint64 QHttpNetworkConnectionPrivate::uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const
{
- return reply.d_func()->responseData.nextDataBlockSize();
+ return reply.d_func()->responseData.sizeNextBlock();
}
qint64 QHttpNetworkConnectionPrivate::compressedBytesAvailable(const QHttpNetworkReply &reply) const
@@ -202,21 +219,6 @@ qint64 QHttpNetworkConnectionPrivate::compressedBytesAvailable(const QHttpNetwor
return reply.d_func()->compressedData.size();
}
-qint64 QHttpNetworkConnectionPrivate::read(QHttpNetworkReply &reply, QByteArray &data, qint64 maxSize)
-{
- QRingBuffer *rb = &reply.d_func()->responseData;
- if (maxSize == -1 || maxSize >= rb->size()) {
- // read the whole data
- data = rb->readAll();
- rb->clear();
- } else {
- // read only the requested length
- data.resize(maxSize);
- rb->read(data.data(), maxSize);
- }
- return data.size();
-}
-
void QHttpNetworkConnectionPrivate::eraseData(QHttpNetworkReply *reply)
{
reply->d_func()->compressedData.clear();
@@ -556,6 +558,8 @@ bool QHttpNetworkConnectionPrivate::expand(QAbstractSocket *socket, QHttpNetwork
reply->d_func()->totalProgress += inflated.size();
appendUncompressedData(*reply, inflated);
if (shouldEmitSignals(reply)) {
+ // important: At the point of this readyRead(), inflated must be cleared,
+ // else implicit sharing will trigger memcpy when the user is reading data!
emit reply->readyRead();
// make sure that the reply is valid
if (channels[i].reply != reply)
@@ -672,18 +676,19 @@ void QHttpNetworkConnectionPrivate::receiveReply(QAbstractSocket *socket, QHttpN
{
// use the traditional slower reading (for compressed encoding, chunked encoding,
// no content-length etc)
- QBuffer fragment;
- fragment.open(QIODevice::WriteOnly);
- bytes = reply->d_func()->readBody(socket, &fragment);
+ QByteDataBuffer byteDatas;
+ bytes = reply->d_func()->readBody(socket, &byteDatas);
if (bytes) {
if (reply->d_func()->autoDecompress)
- appendCompressedData(*reply, fragment.data());
+ appendCompressedData(*reply, byteDatas);
else
- appendUncompressedData(*reply, fragment.data());
+ appendUncompressedData(*reply, byteDatas);
if (!reply->d_func()->autoDecompress) {
- reply->d_func()->totalProgress += fragment.size();
+ reply->d_func()->totalProgress += bytes;
if (shouldEmitSignals(reply)) {
+ // important: At the point of this readyRead(), the byteDatas list must be empty,
+ // else implicit sharing will trigger memcpy when the user is reading data!
emit reply->readyRead();
// make sure that the reply is valid
if (channels[i].reply != reply)