make the piece-picker less two-phase initialized

This commit is contained in:
arvidn 2017-10-25 22:31:49 +02:00 committed by Arvid Norberg
parent ab4f140328
commit 50bef25786
5 changed files with 18 additions and 17 deletions

View File

@ -176,7 +176,7 @@ namespace libtorrent {
std::uint16_t outstanding_hash_check:1;
};
piece_picker();
piece_picker(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces);
void get_availability(aux::vector<int, piece_index_t>& avail) const;
int get_availability(piece_index_t piece) const;
@ -215,7 +215,7 @@ namespace libtorrent {
piece_index_t reverse_cursor() const { return m_reverse_cursor; }
// sets all pieces to dont-have
void init(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces);
void resize(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces);
int num_pieces() const { return int(m_piece_map.size()); }
bool have_piece(piece_index_t index) const;

View File

@ -68,7 +68,8 @@ namespace libtorrent {
constexpr prio_index_t piece_picker::piece_pos::we_have_index;
piece_picker::piece_picker()
piece_picker::piece_picker(int const blocks_per_piece
, int const blocks_in_last_piece, int const total_num_pieces)
: m_priority_boundaries(1, m_pieces.end_index())
{
#ifdef TORRENT_PICKER_LOG
@ -77,15 +78,18 @@ namespace libtorrent {
#if TORRENT_USE_INVARIANT_CHECKS
check_invariant();
#endif
resize(blocks_per_piece, blocks_in_last_piece, total_num_pieces);
}
void piece_picker::init(int const blocks_per_piece, int const blocks_in_last_piece, int const total_num_pieces)
void piece_picker::resize(int const blocks_per_piece
, int const blocks_in_last_piece, int const total_num_pieces)
{
TORRENT_ASSERT(blocks_per_piece > 0);
TORRENT_ASSERT(total_num_pieces > 0);
#ifdef TORRENT_PICKER_LOG
std::cerr << "[" << this << "] " << "piece_picker::init()" << std::endl;
std::cerr << "[" << this << "] " << "piece_picker::resize()" << std::endl;
#endif
// allocate the piece_map to cover all pieces
// and make them invalid (as if we don't have a single piece)

View File

@ -1223,15 +1223,15 @@ namespace libtorrent {
|| settings().get_int(settings_pack::suggest_mode)
== settings_pack::suggest_read_cache);
std::unique_ptr<piece_picker> pp(new piece_picker());
int const blocks_per_piece
= (m_torrent_file->piece_length() + block_size() - 1) / block_size();
int const blocks_in_last_piece
= ((m_torrent_file->total_size() % m_torrent_file->piece_length())
+ block_size() - 1) / block_size();
// TODO: 3 the init function should be merged with the constructor
pp->init(blocks_per_piece, blocks_in_last_piece, m_torrent_file->num_pieces());
std::unique_ptr<piece_picker> pp(new piece_picker(blocks_per_piece
, blocks_in_last_piece
, m_torrent_file->num_pieces()));
m_picker = std::move(pp);
@ -2208,7 +2208,7 @@ namespace libtorrent {
int const blocks_per_piece = (m_torrent_file->piece_length() + block_size() - 1) / block_size();
int const blocks_in_last_piece = ((m_torrent_file->total_size() % m_torrent_file->piece_length())
+ block_size() - 1) / block_size();
m_picker->init(blocks_per_piece, blocks_in_last_piece, m_torrent_file->num_pieces());
m_picker->resize(blocks_per_piece, blocks_in_last_piece, m_torrent_file->num_pieces());
m_file_progress.clear();
m_file_progress.init(picker(), m_torrent_file->files());

View File

@ -56,8 +56,7 @@ TORRENT_TEST(init)
for (piece_index_t idx(0); idx < fs.end_piece(); ++idx)
{
piece_picker picker;
picker.init(4, fs.total_size() % 4, fs.num_pieces());
piece_picker picker(4, fs.total_size() % 4, fs.num_pieces());
picker.we_have(idx);
aux::file_progress fp;
@ -88,8 +87,7 @@ TORRENT_TEST(init2)
for (piece_index_t idx(0); idx < fs.end_piece(); ++idx)
{
piece_picker picker;
picker.init(4, fs.total_size() % 4, fs.num_pieces());
piece_picker picker(4, fs.total_size() % 4, fs.num_pieces());
picker.we_have(idx);
aux::vector<std::int64_t, file_index_t> vec;

View File

@ -114,8 +114,7 @@ std::shared_ptr<piece_picker> setup_picker(
const int num_pieces = int(strlen(availability));
TORRENT_ASSERT(int(strlen(have_str)) == num_pieces);
std::shared_ptr<piece_picker> p = std::make_shared<piece_picker>();
p->init(blocks_per_piece, blocks_per_piece, num_pieces);
std::shared_ptr<piece_picker> p = std::make_shared<piece_picker>(blocks_per_piece, blocks_per_piece, num_pieces);
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
{
@ -603,7 +602,7 @@ TORRENT_TEST(dec_refcount_split_seed)
TEST_CHECK(avail[piece_index_t(4)] != 0);
}
TORRENT_TEST(init)
TORRENT_TEST(resize)
{
// make sure init preserves priorities
auto p = setup_picker("1111111", " ", "1111111", "");
@ -623,7 +622,7 @@ TORRENT_TEST(init)
TEST_CHECK(p->num_have_filtered() == 1);
TEST_CHECK(p->num_have() == 1);
p->init(blocks_per_piece, blocks_per_piece, blocks_per_piece * 7);
p->resize(blocks_per_piece, blocks_per_piece, blocks_per_piece * 7);
TEST_CHECK(p->piece_priority(piece_index_t(0)) == 0);
TEST_CHECK(p->num_filtered() == 1);
TEST_CHECK(p->num_have_filtered() == 0);