Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add download sequentially #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/download/chunk_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ ChunkSelector::update_priorities() {

m_sharedQueue.clear();

if (m_position == invalid_chunk)
m_position = random() % size();
if (m_position == invalid_chunk) {
if (m_sequential) {
m_position = 0;
} else {
m_position = random() % size();
}
}

advance_position();
}
Expand All @@ -99,12 +104,14 @@ ChunkSelector::find(PeerChunks* pc, [[maybe_unused]] bool highPriority) {
// set.
rak::partial_queue* queue = pc->is_seeder() ? &m_sharedQueue : pc->download_cache();

// Randomize position on average every 16 chunks to prevent
// inefficient distribution with a slow seed and fast peers
// all arriving at the same position.
if ((random() & 63) == 0) {
m_position = random() % size();
queue->clear();
if (!m_sequential) {
// Randomize position on average every 16 chunks to prevent
// inefficient distribution with a slow seed and fast peers
// all arriving at the same position.
if ((random() & 63) == 0) {
m_position = random() % size();
queue->clear();
}
}

if (queue->is_enabled()) {
Expand Down
11 changes: 11 additions & 0 deletions src/download/chunk_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class ChunkSelector {
void initialize(ChunkStatistics* cs);
void cleanup();

// Sequential chunk selection
bool is_sequential_enabled() {
return m_sequential;
};
void set_sequential_enabled(bool enabled) {
m_sequential = enabled;
};


// Call this once you've modified the bitfield or priorities to
// update cached information. This must be called once before using
// find.
Expand Down Expand Up @@ -118,6 +127,8 @@ class ChunkSelector {
rak::partial_queue m_sharedQueue;

uint32_t m_position;

bool m_sequential = false;
};

}
Expand Down
11 changes: 11 additions & 0 deletions src/torrent/download.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ Download::set_pex_enabled(bool enabled) {
m_ptr->info()->unset_flags(DownloadInfo::flag_pex_enabled);
}

bool
Download::is_sequential_enabled() {
return m_ptr->main()->chunk_selector()->is_sequential_enabled();
}

void
Download::set_sequential_enabled(bool enabled) {
m_ptr->main()->chunk_selector()->set_sequential_enabled(enabled);
}


Object*
Download::bencode() {
return m_ptr->bencode();
Expand Down
4 changes: 4 additions & 0 deletions src/torrent/download.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class LIBTORRENT_EXPORT Download {

void set_pex_enabled(bool enabled);

// Sequential download
bool is_sequential_enabled();
void set_sequential_enabled(bool enabled);

Object* bencode();
const Object* bencode() const;

Expand Down