Remove use of QByteDataBuffer in QNetworkReplyHttpImpl

It's temporarily storing QByteArrays before we copy them directly to
QIODevice's internal buffer. We can save the extra work by just push
them directly into the buffer.

The signal compression is no longer useful performance-wise, but is
kept as it will throttle the amount of readyRead emissions the users has
to handle.

Reorder some of the operations as a result to make it more natural.

Change-Id: Ifc0481d56fec42545e95970125d883a5c68d647a
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
bb10
Mårten Nordheim 2019-08-14 12:00:13 +02:00
parent d97009a9f1
commit 6a2112c28c
2 changed files with 20 additions and 42 deletions

View File

@ -1052,58 +1052,38 @@ void QNetworkReplyHttpImplPrivate::replyDownloadData(QByteArray d)
if (!q->isOpen())
return;
int pendingSignals = (int)pendingDownloadDataEmissions->fetchAndAddAcquire(-1) - 1;
if (cacheEnabled && isCachingAllowed() && !cacheSaveDevice)
initCacheSaveDevice();
// This is going to look a little strange. When downloading data while a
// HTTP redirect is happening (and enabled), we write the redirect
// response to the cache. However, we do not append it to our internal
// buffer as that will contain the response data only for the final
// response
if (cacheSaveDevice)
cacheSaveDevice->write(d);
if (!isHttpRedirectResponse()) {
buffer.append(d);
bytesDownloaded += d.size();
}
bytesBuffered += d.size();
int pendingSignals = pendingDownloadDataEmissions->fetchAndSubAcquire(1) - 1;
if (pendingSignals > 0) {
// Some more signal emissions to this slot are pending.
// Instead of writing the downstream data, we wait
// and do it in the next call we get
// (signal comppression)
pendingDownloadData.append(d);
return;
}
pendingDownloadData.append(d);
d.clear();
// We need to usa a copy for calling writeDownstreamData as we could
// possibly recurse into this this function when we call
// appendDownstreamDataSignalEmissions because the user might call
// processEvents() or spin an event loop when this occur.
QByteDataBuffer pendingDownloadDataCopy = pendingDownloadData;
pendingDownloadData.clear();
if (cacheEnabled && isCachingAllowed() && !cacheSaveDevice) {
initCacheSaveDevice();
}
qint64 bytesWritten = 0;
for (int i = 0; i < pendingDownloadDataCopy.bufferCount(); i++) {
QByteArray const &item = pendingDownloadDataCopy[i];
// This is going to look a little strange. When downloading data while a
// HTTP redirect is happening (and enabled), we write the redirect
// response to the cache. However, we do not append it to our internal
// buffer as that will contain the response data only for the final
// response
if (cacheSaveDevice)
cacheSaveDevice->write(item.constData(), item.size());
if (!isHttpRedirectResponse())
buffer.append(item);
bytesWritten += item.size();
}
bytesBuffered += bytesWritten;
pendingDownloadDataCopy.clear();
QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
if (preMigrationDownloaded != Q_INT64_C(-1))
totalSize = totalSize.toLongLong() + preMigrationDownloaded;
if (isHttpRedirectResponse())
return;
bytesDownloaded += bytesWritten;
QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
if (preMigrationDownloaded != Q_INT64_C(-1))
totalSize = totalSize.toLongLong() + preMigrationDownloaded;
emit q->readyRead();
// emit readyRead before downloadProgress incase this will cause events to be

View File

@ -63,7 +63,6 @@
#include <QtNetwork/QNetworkCacheMetaData>
#include <private/qhttpnetworkrequest_p.h>
#include <private/qbytedata_p.h>
#include <private/qnetworkreply_p.h>
#include <QtNetwork/QNetworkProxy>
#include <QtNetwork/QNetworkSession>
@ -248,7 +247,6 @@ public:
quint64 resumeOffset;
qint64 preMigrationDownloaded;
QByteDataBuffer pendingDownloadData; // For signal compression
qint64 bytesDownloaded;
qint64 bytesBuffered;