Skip to content

perf(torrent): declare v1 interest only in peers with pieces we want - #566

Merged
linroid merged 2 commits into
perf/torrent-platform-digestsfrom
perf/torrent-v1-interest
Oct 16, 2026
Merged

linroid merged 2 commits into
perf/torrent-platform-digestsfrom
perf/torrent-v1-interest

Conversation

@linroid

@linroid linroid commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Second of three stacked PRs for v1 torrent throughput, based on #565 (review item 17).

A v1 worker sent INTERESTED to every peer right after the handshake and never sent NOT_INTERESTED, even while seeding. Leechers' rate-based chokers then gave Ketch unchoke slots it never used, and two seeds stayed connected forever.

  • Interest follows the peer's pieces. The scheduler counts, for each peer, the wanted pieces it has that we have not verified, and keeps the count current on bitfield, HAVE, verify, revoke and selection changes. A worker starts not interested and sends INTERESTED or NOT_INTERESTED as that count crosses zero. It only claims pieces while interested.
  • Seeds close seed connections. A swarm that holds every piece closes connections to peers that hold every piece too, and never redials them. "Every piece" is deliberate: a swarm that is only complete for its selection keeps its seeds, because a later selection change may want their pieces.

Download throughput is unchanged; measured once per row on this PR, every row is within the run-to-run range of the digests PR. The 16 KiB row varies most, because the old per-claim scan allocates on every attempt.

Tests

  • TorrentSwarmInterestTest.interestFollowsThePiecesThePeerHasAndWeStillWant: no INTERESTED to a peer with nothing we want; INTERESTED once it announces a wanted piece; NOT_INTERESTED once that piece is stored; INTERESTED again for the next one.
  • TorrentSwarmInterestTest.seedIsNeverInterestedAndClosesOtherSeeds: a seeding swarm never declares interest, still serves a leecher, and hangs up on another seed.
  • Tests that read the swarm's unconditional INTERESTED as the end of its opening messages now read up to the bitfield, or have their peer hold a wanted piece (TorrentSwarmDhtPortTest, TorrentSwarmPexEndpointTest, the Fast seeder test).
  • :library:torrent:jvmTest passes (1,288 tests). An earlier version closed seeds once the selection was complete, and four selection tests caught it (TorrentDownloadSourceSelectionTest, KotlinTorrentSessionSelectionTest, KotlinRuntimePrivateTest).

How this was measured

V1ThroughputTest (opt-in, added in the first PR of the stack): a KotlinTorrentEngine with the default buffer limits (maxBufferedBytes 32 MiB) downloads a v1 torrent from 4 in-process LoopbackSeeders, 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.

KETCH_TORRENT_THROUGHPUT=1 KETCH_THROUGHPUT_LABEL="$(git rev-parse --short HEAD)" \
  ./gradlew :library:torrent:jvmTest --tests '*V1ThroughputTest'

A v1 worker sent INTERESTED to every peer and never NOT_INTERESTED, even
while seeding, so leechers' chokers gave us unchoke slots we never used.
The scheduler now counts, per peer, the wanted pieces it has that we have
not verified (bitfield, HAVE, verify, revoke and selection changes keep it
current); a worker starts not interested and sends INTERESTED or
NOT_INTERESTED as that count crosses zero, and claims pieces only while
interested. A swarm that has every piece closes connections to peers that
have every piece too, and never redials them.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T13:19:54.155466Z 54d8340 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 16, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@linroid
linroid added this pull request to stack #568 October 16, 2026 13:24
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 949 files  + 5   1 977 suites  +5   23m 22s ⏱️ + 1m 20s
13 095 tests + 6  12 848 ✅ + 6  247 💤 ±0  0 ❌ ±0 
15 764 runs  +10  15 517 ✅ +10  247 💤 ±0  0 ❌ ±0 

Results for commit cf0fc4f. ± Comparison against base commit dd6ee58.

@linroid
linroid merged commit 72c85df into main Oct 16, 2026
14 checks passed
@linroid
linroid deleted the perf/torrent-v1-interest branch October 16, 2026 14:38
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.

1 participant