Skip to content

Commit

Permalink
libtorrent: Optimize chunk selection based on priority (#16)
Browse files Browse the repository at this point in the history
* libtorrent: Optimize chunk selector

Only search the bitfield for the priority type we're looking for.

* libtorrent: Fix download delegator for priority searches

We need to adjust the download delegator slightly to search the chunk selector by priority type.
  • Loading branch information
stickz authored Jul 10, 2024
1 parent 1439327 commit 6598f49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
25 changes: 8 additions & 17 deletions libtorrent/src/download/chunk_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ ChunkSelector::update_priorities() {
}

uint32_t
ChunkSelector::find(PeerChunks* pc, __UNUSED bool highPriority) {
ChunkSelector::find(PeerChunks* pc, bool highPriority) {
// This needs to be re-enabled.
if (m_position == invalid_chunk)
return invalid_chunk;
Expand Down Expand Up @@ -128,24 +128,15 @@ ChunkSelector::find(PeerChunks* pc, __UNUSED bool highPriority) {

queue->clear();

(search_linear(pc->bitfield(), queue, m_data->high_priority(), m_position, size()) &&
search_linear(pc->bitfield(), queue, m_data->high_priority(), 0, m_position));

if (queue->prepare_pop()) {
// Set that the peer has high priority pieces cached.

} else {
// Set that the peer has normal priority pieces cached.

// Urgh...
queue->clear();

if (highPriority)
(search_linear(pc->bitfield(), queue, m_data->high_priority(), m_position, size()) &&
search_linear(pc->bitfield(), queue, m_data->high_priority(), 0, m_position));
else
(search_linear(pc->bitfield(), queue, m_data->normal_priority(), m_position, size()) &&
search_linear(pc->bitfield(), queue, m_data->normal_priority(), 0, m_position));

if (!queue->prepare_pop())
return invalid_chunk;
}

if (!queue->prepare_pop())
return invalid_chunk;

uint32_t pos = queue->pop();

Expand Down
9 changes: 6 additions & 3 deletions libtorrent/src/download/delegator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ Delegator::delegate(PeerChunks* peerChunks, uint32_t affinity, uint32_t maxPiece
delegate_from_blocklist(new_transfers, maxPieces, itr, peerInfo);
}
// Create new high priority pieces.
delegate_new_chunks(new_transfers, maxPieces, peerChunks, true);
delegate_new_chunks(new_transfers, maxPieces, peerChunks, true);
if (new_transfers.size() >= maxPieces)
return new_transfers;

// Create new normal priority pieces.
delegate_new_chunks(new_transfers, maxPieces, peerChunks, false);
if (new_transfers.size() >= maxPieces)
return new_transfers;
}
if (new_transfers.size() >= maxPieces)
return new_transfers;

// Find existing high priority pieces.
for (BlockList* itr : m_transfers) {
Expand Down

0 comments on commit 6598f49

Please sign in to comment.