Reduce cpu cost of readyRead() when using high speed internet connection
Question
I tried to profiling my program, which is a file downloader. I use 5 QNetworkRequest
to enhance transfer speed, they all connect to the same readyRead()
slot.
I use QMap to buffer those byte arrays.
When downloading speed reaches, e.g 5 MiB/s, I found CPU costs rises up significantly, nearly 100%, readyRead()
was calling 30 times per seconds.
Is there any suggestion to this, e.g can I let QNetworkReply buffer its io device, reduce the amount of signals emitted?
UPDATA
I made some debug output in readyReady():
(reply
here was casted from sender())
qDebug() << "Got: " << reply->readAll().length() << " bytes";
And about 30 output per seconds , each contains 1500 bytes
Solution
I have one hint after quick look on your code. You're using QByteArray::append
member (downloadBuffers
) which realocates internal buffer for appended data. If you're able to predict how much data you will store in each download buffer then use QByteArray::reserve
with exact amount of data. If you can't you could simply reserve i.e. 1mb and reserve memory progresively 1mb each time your next append will exceed current QByteArray::capacity
. You can for example reserve downbufferSize
for each buffer. It's a slight optimization, but should give you some time