From b3912fa1a5a4c68f412eb65ebf5fde437df71d03 Mon Sep 17 00:00:00 2001 From: Yat Ho Date: Sat, 14 Dec 2024 09:32:03 +0800 Subject: [PATCH] feat: download first and last piece first in sequential mode (#6893) * feat: download first and last piece first in sequential mode * test: fix tests --- libtransmission/peer-mgr-wishlist.cc | 21 ++++++++++++++++++- .../libtransmission/peer-mgr-wishlist-test.cc | 12 ++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/libtransmission/peer-mgr-wishlist.cc b/libtransmission/peer-mgr-wishlist.cc index 9398f9f2a..ed1df88c5 100644 --- a/libtransmission/peer-mgr-wishlist.cc +++ b/libtransmission/peer-mgr-wishlist.cc @@ -351,7 +351,26 @@ private: continue; } - auto const salt = is_sequential ? piece : salter(); + auto const salt = [&]() + { + if (!is_sequential) + { + return salter(); + } + + // Download first and last piece first + if (piece == 0U) + { + return 0U; + } + + if (piece == n_pieces - 1U) + { + return 1U; + } + + return piece + 1U; + }(); candidates_.emplace_back(piece, salt, &mediator_); } std::sort(std::begin(candidates_), std::end(candidates_)); diff --git a/tests/libtransmission/peer-mgr-wishlist-test.cc b/tests/libtransmission/peer-mgr-wishlist-test.cc index 1407a02c5..97789541d 100644 --- a/tests/libtransmission/peer-mgr-wishlist-test.cc +++ b/tests/libtransmission/peer-mgr-wishlist-test.cc @@ -1511,7 +1511,8 @@ TEST_F(PeerMgrWishlistTest, settingSequentialDownloadRebuildsWishlist) return wishlist.next(n_wanted, PeerHasAllPieces, ClientHasNoActiveRequests); }; - // we should get pieces in order when we ask for blocks + // we should get pieces in sequential order when we ask for blocks, + // except the last piece should follow immediately after the first piece // NB: when all other things are equal in the wishlist, pieces are // picked at random so this test -could- pass even if there's a bug. // So test several times to shake out any randomness @@ -1526,8 +1527,8 @@ TEST_F(PeerMgrWishlistTest, settingSequentialDownloadRebuildsWishlist) } EXPECT_EQ(150U, requested.count()); EXPECT_EQ(100U, requested.count(0, 100)); - EXPECT_EQ(50U, requested.count(100, 200)); - EXPECT_EQ(0U, requested.count(200, 300)); + EXPECT_EQ(0U, requested.count(100, 200)); + EXPECT_EQ(50U, requested.count(200, 300)); } // Same premise as previous test, but ask for more blocks. @@ -1540,7 +1541,8 @@ TEST_F(PeerMgrWishlistTest, settingSequentialDownloadRebuildsWishlist) requested.set_span(span.begin, span.end); } EXPECT_EQ(250U, requested.count()); - EXPECT_EQ(200U, requested.count(0, 200)); - EXPECT_EQ(50U, requested.count(200, 300)); + EXPECT_EQ(100U, requested.count(0, 100)); + EXPECT_EQ(50U, requested.count(100, 200)); + EXPECT_EQ(100U, requested.count(200, 300)); } }