mpetazzoni / ttorrent

BitTorrent Java library with tracker and download client
http://mpetazzoni.github.com/ttorrent/
Apache License 2.0
1.38k stars 502 forks source link

Speed up downloading: do not calculate hash of blank pieces #255

Closed valdisxp1 closed 3 years ago

valdisxp1 commented 3 years ago

When downloading FairPieceStorageFactory.createStorage could not tell the difference between a partial download and a completely blank piece it just created. Then it would compute SHA-1 hash in both cases.

If we have a newly created blank piece we assume that the torrent does not have a blank piece and download that piece without looking at the hash. I.e. do not put it in availablePieces.

This should be significant speed up when downloading. I did the CommunicationManagerTest#download_many_times test 50 times and without the delay, i.e:

    for (int i = 0; i < 50; i++) {
      downloadAndStop(torrent, 250 * 1000, createClient());
    }

In jProfiler I see that java.security.MessageDigest.digest (calculation SHA-1) before optimizations took 2,295 ms (24 %). And after it takes 1,209 ms (13 %). So I expect >10% speed improvement for downloads.

Dead-off commented 3 years ago

thank you, good optimization!

valdisxp1 commented 3 years ago

Could you tag this pull request with hacktoberfest-accepted? Long story short: it costs you nothing, I get a free t-shirt. :smile: https://hacktoberfest.digitalocean.com/hacktoberfest-update

Dead-off commented 3 years ago

Done