Repository navigation
perf(torrent): share v1 pieces between peers and pipeline requests by rate - #567
Merged
linroid merged 3 commits intoOct 16, 2026
Merged
Conversation
… rate Each v1 peer claimed a whole piece and reserved all of it from the engine-wide transfer budget, so the default 32 MiB let about eight 4 MiB pieces, or one 16 MiB piece, be in flight across the engine. Requests stopped at 16 blocks of that one piece, the queue drained at every piece end, and a choke threw the partial piece away. Peers now request 16 KiB blocks of open pieces: one buffer per piece being assembled, opened rarest first when a peer has nothing left to fetch in the open ones, and shared by every peer that has the piece. The budget bounds pieces in assembly rather than peers transferring, and store.commit still takes a whole, hashed piece, so the commit, sidecar and selection rules are unchanged. - Each peer keeps about 3 s of its measured rate outstanding (4 to 500 requests, within its reqq or 250), with a slow start from 16, across piece ends; we advertise reqq 500. - A choke, reject, cancel or disconnect frees only the blocks still owed. Partial pieces give their buffers up after 10 s idle when another piece needs one, after 60 s anyway, and at once when no connected peer has them. - A failed hash check blames a peer only when it sent every block; otherwise one peer fetches the piece alone next. - Once every wanted piece is open, idle peers duplicate outstanding blocks and the first copy cancels the other. - HAVEs come from an append-only log, the pending count no longer copies the request set, peers read completion without the store lock, and a peer waiting for a buffer is not dropped as useless.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ec2b7889a1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Every torrent's peers and pieces, and uploads, share one transfer budget. Counting only the swarm's own peers let several swarms admit peers until no piece fit anywhere, and peers waiting for a buffer are no longer dropped, so every download could stall. A peer is admitted again only while all of the budget but the swarm's own pieces, which its peers share and finish, leaves room for one more piece.
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Third of three stacked PRs for v1 torrent throughput, based on #566 (and #565) (review items 8 and 9).
The limits
maxBufferedBytes, 32 MiB). That left room for about eight 4 MiB pieces in flight across the whole engine, or a single 16 MiB one. A slow peer could hold its buffer for up to 90 s.Design: shared in-memory pieces, not write-through
Writing blocks through to disk and hashing incrementally was the other option. Against
TorrentPieceStoreit would mean:verifyExisting;changeSelectionanddropIfVanishedmove spans between the file and its sidecar;That is a storage redesign, so this PR keeps
store.commit(index, bytes)(a whole, hashed piece) and changes how pieces are shared.reqq(250 when it names none). It starts at 16 with a slow start of one more request per block received until the first rate sample. Requests run across piece ends and go out in batches (PeerWire.sendAll). We advertisereqq500; the upload queue became aLinkedHashSet.close()returns everything when the swarm stops.CorruptPieceException, as before). Otherwise one peer fetches the piece alone next, so a repeat offender ends up identified.PeerProtocolState.pendingCountreplaces the copyingrequests.size. Workers read completion lock-free (completedNow()), so a commit holding the store lock no longer stalls every peer. A peer is admitted while everything the engine-wide budget holds except this swarm's own pieces (other torrents' peers and pieces, uploads) leaves room for one more piece; the swarm's own open pieces, which its peers share and finish, no longer keep peers out. A peer waiting for a buffer is not dropped as useless. Web seeds keep their whole-piece runs.Results
Median MB/s (3 runs), CPU s/GiB and GiB allocated per GiB downloaded:
The 64 MiB rows with a round trip include the slow start. With 512 MiB at 50 ms (this PR only, 3 runs), the rates are: 16 KiB 77.4, 256 KiB 406.1, 4 MiB 379.0, 16 MiB 129.4 MB/s.
Limits left
maxBufferedBytes, or write-through, would remove this; that is a memory-policy decision for a separate change.lstatper component), plus socket syscalls per message. A profile shows the download path itself no longer dominates. The review's validated-handle cache is the fix.Tests
New regression tests, each checked against the previous code:
TorrentSwarmPipelineTest.requestsRunAcrossPieceEnds: 6 requests over at least 3 two-block pieces before any replyTorrentSwarmPipelineTest.peersShareOnePiecesBuffer: a budget of one 32-block piece, and both peers are asked for blocks of it before either answersTorrentSwarmPipelineTest.chokeKeepsTheBlocksAlreadyReceived: after a non-Fast choke at half a piece, the other peer is asked only for the missing 4 blocksTorrentSwarmPipelineTest.fastPeerThatChokesAfterServingLeavesTheRestToOthers: a Fast peer serves its 16 requests after choking and never unchokes; a seed found later finishes well within the 90 s deadlineTorrentSwarmPipelineTest.stoppedSwarmReturnsEveryBuffer: a stopped swarm returns its partial pieces' buffersTorrentPieceSchedulerTestwas rewritten for the block API (17 tests). It covers:starved,finishandclosefreeing it;poll;TorrentSwarmPipelineTest.peerWaitsWhileOtherTorrentsLeaveNoRoomForAPiecechecks that admission (added after review; it fails on the swarm-only rule).:library:torrent:jvmTestpasses in full (1,305 tests). v1 and crypto tests also pass on the Android host, and the module compiles for iOS.How this was measured
V1ThroughputTest(opt-in, added in the first PR of the stack): aKotlinTorrentEnginewith the default buffer limits (maxBufferedBytes32 MiB) downloads a v1 torrent from 4 in-processLoopbackSeeders, memory-backed seeds on plain threads that answer each request after an optional delay, which the engine sees as the round trip. Uploads, DHT, uTP, encryption, local discovery and port mapping are off; the seeders' threads are subtracted from CPU time and allocation; one warm-up download runs first. 256 MiB payload without a round trip, 64 MiB with 50 ms, 3 runs per row (median and range). CPU is process CPU, JIT and GC included.Apple M4 Pro (14 cores), 48 GiB, macOS 27, APFS SSD, OpenJDK 21.0.11 (Homebrew), otherwise idle.