From f90a80ab053d01bd1708355533d2ece5dbfb3ab7 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 5 Jul 2007 22:49:28 +0000 Subject: [PATCH] made piece picker less anal about speed categories, to improve performance --- src/piece_picker.cpp | 32 ++++++++++++++++++++++---------- test/test_bandwidth_limiter.cpp | 4 ++-- test/test_piece_picker.cpp | 11 ++++++----- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index a1afc6bc3..82d2ccbdb 100755 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -1071,21 +1071,31 @@ namespace libtorrent namespace { - bool exclusively_requested_from(piece_picker::downloading_piece const& p + // the first bool is true if this is the only peer that has requested and downloaded + // blocks from this piece. + // the second bool is true if this is the only active peer that is requesting + // and downloading blocks from this piece. Active means having a connection. + boost::tuple requested_from(piece_picker::downloading_piece const& p , int num_blocks_in_piece, void* peer) { + bool exclusive = true; + bool exclusive_active = true; for (int j = 0; j < num_blocks_in_piece; ++j) { piece_picker::block_info const& info = p.info[j]; if (info.state != piece_picker::block_info::state_none - && info.peer != peer - && info.peer != 0 - && static_cast(info.peer)->connection) + && info.peer != peer) { - return false; + exclusive = false; + if (info.peer == 0 + || static_cast(info.peer)->connection == 0) + { + exclusive_active = false; + return boost::make_tuple(exclusive, exclusive_active); + } } } - return true; + return boost::make_tuple(exclusive, exclusive_active); } } @@ -1120,8 +1130,10 @@ namespace libtorrent // is true if all the other pieces that are currently // requested from this piece are from the same // peer as 'peer'. - bool only_same_peer = exclusively_requested_from(*p - , num_blocks_in_piece, peer); + bool exclusive; + bool exclusive_active; + boost::tie(exclusive, exclusive_active) + = requested_from(*p, num_blocks_in_piece, peer); // this means that this partial piece has // been downloaded/requested partially from @@ -1130,7 +1142,7 @@ namespace libtorrent // blocks to the backup list. If the prioritized // blocks aren't enough, blocks from this list // will be picked. - if (prefer_whole_pieces && !only_same_peer) + if (prefer_whole_pieces && !exclusive) { if (int(backup_blocks.size()) >= num_blocks) continue; for (int j = 0; j < num_blocks_in_piece; ++j) @@ -1164,7 +1176,7 @@ namespace libtorrent // if the state of the piece is none (the // piece will in that case change state). if (p->state != none && p->state != speed - && !only_same_peer + && !exclusive_active && !ignore_speed_categories) { if (int(backup_blocks.size()) >= num_blocks) continue; diff --git a/test/test_bandwidth_limiter.cpp b/test/test_bandwidth_limiter.cpp index 370a5bf91..b268f225d 100644 --- a/test/test_bandwidth_limiter.cpp +++ b/test/test_bandwidth_limiter.cpp @@ -149,7 +149,7 @@ struct peer_connection bool m_prioritized; bool m_ignore_limits; bool m_abort; - stat m_stats; + libtorrent::stat m_stats; io_service& m_ios; std::string m_name; int m_refs; @@ -192,7 +192,7 @@ void run_test(io_service& ios, connections_t& v) tick.async_wait(boost::bind(&do_tick, _1, boost::ref(tick), boost::ref(v))); deadline_timer complete(ios); - complete.expires_from_now(seconds(stat::history * 2)); + complete.expires_from_now(seconds(libtorrent::stat::history * 2)); complete.async_wait(boost::bind(&do_stop, boost::ref(tick), boost::ref(v))); std::for_each(v.begin(), v.end() diff --git a/test/test_piece_picker.cpp b/test/test_piece_picker.cpp index bd46fd17c..6d3e174ab 100644 --- a/test/test_piece_picker.cpp +++ b/test/test_piece_picker.cpp @@ -188,9 +188,10 @@ int test_main() piece_block expected4[] = { - piece_block(2, 0), piece_block(2, 1) - , piece_block(2, 2), piece_block(2, 3) + piece_block(3, 0), piece_block(3, 1) + , piece_block(3, 2), piece_block(3, 3) }; + TEST_CHECK(std::equal(picked.begin() , picked.end(), expected4)); @@ -204,12 +205,12 @@ int test_main() piece_block expected5[] = { - piece_block(2, 0), piece_block(2, 1) - , piece_block(2, 2), piece_block(2, 3) - , piece_block(3, 0), piece_block(3, 1) + piece_block(3, 0), piece_block(3, 1) , piece_block(3, 2), piece_block(3, 3) , piece_block(5, 0), piece_block(5, 1) , piece_block(5, 2), piece_block(5, 3) + , piece_block(2, 0), piece_block(2, 1) + , piece_block(2, 2), piece_block(2, 3) }; TEST_CHECK(std::equal(picked.begin()