Skip to content

fix to_req() to not request pad-space for v2-only torrents#8231

Merged
arvidn merged 1 commit intoRC_2_0from
v2-to-req
Mar 28, 2026
Merged

fix to_req() to not request pad-space for v2-only torrents#8231
arvidn merged 1 commit intoRC_2_0from
v2-to-req

Conversation

@arvidn
Copy link
Copy Markdown
Owner

@arvidn arvidn commented Mar 27, 2026

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts how libtorrent computes per-block request lengths for v2-only torrents so peers don’t request bytes that fall into pad-space at file boundaries.

Changes:

  • Add torrent_info::piece_size_for_req() to select the appropriate piece size for request-length calculations (v1: piece_size(), v2-only: piece_size2()).
  • Update torrent::to_req() and peer_connection’s outgoing request sizing to use piece_size_for_req().
  • Add a ChangeLog entry describing the v2-only request size optimization.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/torrent.cpp Use piece_size_for_req() when converting piece_blockpeer_request.
src/peer_connection.cpp Use piece_size_for_req() when computing outgoing request block sizes.
include/libtorrent/torrent_info.hpp Introduce piece_size_for_req() helper API.
ChangeLog Document the behavior change for v2-only request sizing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/peer_connection.cpp Outdated
Comment thread src/peer_connection.cpp Outdated
Comment thread include/libtorrent/torrent_info.hpp
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/torrent.cpp
Comment thread include/libtorrent/torrent_info.hpp Outdated
Comment thread src/http_seed_connection.cpp Outdated
Comment thread src/web_peer_connection.cpp Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/web_peer_connection.cpp Outdated
Comment thread src/peer_connection.cpp Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/peer_connection.cpp Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 354 to +399
@@ -397,6 +391,12 @@ void web_peer_connection::write_request(peer_request const& r)
std::cerr << this << " REQ: p: " << pr.piece << " " << pr.start << std::endl;
#endif
size -= pr.length;
cur_start += pr.length;
if (cur_start >= info.piece_size_for_req(cur_piece))
{
cur_start = 0;
++cur_piece;
}
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the request-splitting loop, piece_size_for_req() is called multiple times per iteration (to compute pr.length and to check for piece boundary). For v2 torrents this ends up calling file_storage::piece_size2(), which does an upper_bound() over the file list (log N) each time. Consider caching the current piece's piece_size_for_req in a local variable and only recomputing it when cur_piece changes, to avoid extra work when pipelining many blocks.

Copilot uses AI. Check for mistakes.
Comment on lines +508 to +516
// returns the piece size appropriate for computing request lengths.
// for v2 torrents, pieces at the end of files may be shorter than
// the main piece size. This is the case for hybrid torrents as well.
int piece_size_for_req(piece_index_t index) const
{
return v2()
? m_files.piece_size2(index)
: m_files.piece_size(index);
}
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR introduces torrent_info::piece_size_for_req() as a new public helper with v2-specific semantics. There are already unit tests for file_storage::piece_size2(), but I couldn't find any tests that exercise piece_size_for_req() directly (including hybrid torrents) to ensure it keeps request sizing correct at file boundaries. Please add coverage to lock in the intended behavior (v2()/hybrid -> piece_size2, v1-only -> piece_size).

Copilot uses AI. Check for mistakes.
Comment thread src/torrent.cpp
Comment on lines 1522 to 1527
peer_request torrent::to_req(piece_block const& p) const
{
int const block_offset = p.block_index * block_size();
int const block = std::min(torrent_file().piece_size(
p.piece_index) - block_offset, block_size());
int const piece_sz = torrent_file().piece_size_for_req(p.piece_index);
int const block = std::min(piece_sz - block_offset, block_size());
TORRENT_ASSERT(block > 0);
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torrent::to_req() now uses piece_size_for_req() to size the last block at v2 file boundaries. There don't appear to be any tests that assert the generated peer_request lengths for v2-only/hybrid torrents (i.e. that requests never extend into pad-space). Adding a focused unit/integration test for this would help prevent regressions since this affects core wire-protocol behavior.

Copilot uses AI. Check for mistakes.
@arvidn arvidn merged commit aa59c7c into RC_2_0 Mar 28, 2026
62 of 64 checks passed
@arvidn arvidn deleted the v2-to-req branch March 28, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants