Skip to content

perf(torrent): hash with the platform's SHA-1 and SHA-256 - #565

Merged
linroid merged 2 commits into
mainfrom
perf/torrent-platform-digests
Oct 16, 2026
Merged

linroid merged 2 commits into
mainfrom
perf/torrent-platform-digests

Conversation

@linroid

@linroid linroid commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

First of three stacked PRs that remove the design limits on v1 torrent throughput (items 8–10 and 17 of the October torrent review). Next: #566 (v1 interest), then #567 (block-level pieces and adaptive pipelines).

This PR makes hashing fast and adds the measurement the stack is judged by.

  • Platform digests in core. ChecksumAlgorithm.newDigest() now returns the platform's digest where there is one (platformDigest, expect/actual): java.security.MessageDigest on the JVM and Android (Conscrypt on Android), CommonCrypto on iOS for SHA-1, SHA-256 and SHA-512. MD5 stays pure Kotlin on iOS, because CommonCrypto deprecates it. JS and WasmWasi keep the pure Kotlin classes, which stay public and are now also the tests' oracle. Checksum verification (fileDigest) and the updater get the speedup too.
  • Torrent uses them. Pieces, Merkle trees, proofs, payload verification and MSE keys hash through newSha1() / newSha256() (crypto/Digests.kt) instead of the pure classes.
  • V1ThroughputTest (opt-in, KETCH_TORRENT_THROUGHPUT=1) measures v1 download throughput and CPU against in-process seeders with an emulated round trip. It is documented in docs/development/torrent-verification.md.

On this machine the JDK hashes SHA-1 and SHA-256 at about 3.3 GB/s; the review measured the pure Kotlin ones at about 455 and 370 MB/s. getInstance plus a 64-byte Merkle node costs 60–100 ns, less than the pure hash itself, so digests are not cached.

Results

Piece RTT main MB/s this PR MB/s main CPU s/GiB this PR CPU s/GiB
16 KiB 0 69.4 (61.8–71.5) 65.3 (57.5–67.9) 34.09 34.39
256 KiB 0 491.6 (466.6–513.8) 569.0 (518.0–575.8) 5.94 4.60
4 MiB 0 572.1 (514.3–638.8) 961.1 (888.7–974.8) 6.49 4.69
16 MiB 0 135.9 (135.8–139.5) 170.4 (170.1–170.5) 8.84 7.44
16 KiB 50 ms 1.2 1.2 81.90 83.09
256 KiB 50 ms 18.2 (18.1–18.4) 18.1 (18.0–18.2) 12.54 10.09
4 MiB 50 ms 18.9 (18.7–19.1) 19.0 (19.0–19.2) 11.26 10.45
16 MiB 50 ms 4.8 4.8 17.39 23.88

Where hashing is the bottleneck (4 MiB pieces, no round trip) throughput rises 68% and CPU per GiB falls 28%. The 50 ms rows are bound by the request pipeline, which the third PR fixes. At under 5 MB/s their CPU per GiB is dominated by fixed per-second work, so those CPU figures are noisy.

Tests

DigestVectorsTest runs every known-answer vector, whole and split at every byte, through both the platform and the pure Kotlin digests, and checks the two against each other on random inputs and splits. It passes on the JVM, on the Android host and on the iOS simulator (CommonCrypto). :library:core:jvmTest (575 tests) and :library:torrent:jvmTest (1,286 tests) pass, and core compiles for JS and WasmWasi.

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'

ChecksumAlgorithm.newDigest() now gives the platform's digest where there
is one: java.security.MessageDigest on the JVM and Android, CommonCrypto on
iOS (all but MD5, which CommonCrypto deprecates). JS and WasmWasi keep the
pure Kotlin classes, which stay public and serve as the tests' oracle.
Torrent hashes pieces, Merkle trees and MSE keys through newSha1() and
newSha256() instead of the pure classes.

Adds V1ThroughputTest, an opt-in loopback measurement of v1 download
throughput and CPU against in-process seeders.
@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:21:02.105013Z 4b1f34a 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4b1f34a2f1

ℹ️ 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".

@linroid
linroid added this pull request to stack #568 October 16, 2026 13:24
…sage

The warm-up always served 64 MiB, past a smaller payload. Seeder CPU and
allocation were read after the download, when threads that had ended no
longer reported theirs and live ones counted work from before it; each
seeder thread now stores its totals as it ends, and the seeders' usage is
taken over the same interval as the process's.
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 944 files  ±0   1 972 suites  ±0   22m 2s ⏱️ - 2m 54s
13 089 tests +4  12 842 ✅ +4  247 💤 ±0  0 ❌ ±0 
15 754 runs  +4  15 507 ✅ +4  247 💤 ±0  0 ❌ ±0 

Results for commit dd6ee58. ± Comparison against base commit 7b490c7.

@linroid
linroid merged commit 4d90ad2 into main Oct 16, 2026
14 checks passed
@linroid
linroid deleted the perf/torrent-platform-digests 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