Repository navigation
perf(torrent): hash with the platform's SHA-1 and SHA-256 - #565
Conversation
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.
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. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
There was a problem hiding this comment.
💡 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".
…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.
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.
ChecksumAlgorithm.newDigest()now returns the platform's digest where there is one (platformDigest, expect/actual):java.security.MessageDigeston 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.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 indocs/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.
getInstanceplus a 64-byte Merkle node costs 60–100 ns, less than the pure hash itself, so digests are not cached.Results
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
DigestVectorsTestruns 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): 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.