Skip to content

Optimize merkle root and streaming hash computation#1044

Open
Sahil-4555 wants to merge 1 commit into
anacrolix:masterfrom
Sahil-4555:optimize/merkle-hashing
Open

Optimize merkle root and streaming hash computation#1044
Sahil-4555 wants to merge 1 commit into
anacrolix:masterfrom
Sahil-4555:optimize/merkle-hashing

Conversation

@Sahil-4555
Copy link
Copy Markdown

This change improves the merkle package by simplifying how Merkle roots are built and by making the streaming hash path handle block hashing more directly. The root calculation now reduces hashes in place instead of building extra intermediate slices, and the hash writer now works with fixed block buffering instead of the older incremental hashing flow. I also added focused tests and benchmarks around the root and hash paths so the main logic is easier to verify and measure.

goos: linux
goarch: amd64
pkg: github.com/anacrolix/torrent/merkle
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
                │ old_bench.txt │            new_bench.txt            │
                │    sec/op     │   sec/op     vs base                │
HashWriteAndSum     787.3µ ± 3%   791.1µ ± 3%        ~ (p=0.280 n=10)
Root                565.8µ ± 2%   506.9µ ± 4%  -10.42% (p=0.000 n=10)
RootWithPadHash     285.3µ ± 4%   243.4µ ± 5%  -14.68% (p=0.000 n=10)
geomean             502.8µ        460.4µ        -8.42%

                │ old_bench.txt │            new_bench.txt             │
                │     B/op      │     B/op      vs base                │
HashWriteAndSum    3.297Ki ± 0%   1.500Ki ± 0%  -54.50% (p=0.000 n=10)
Root              127.25Ki ± 0%   32.00Ki ± 0%  -74.85% (p=0.000 n=10)
RootWithPadHash    89.94Ki ± 0%   16.00Ki ± 0%  -82.21% (p=0.000 n=10)
geomean            33.54Ki        9.158Ki       -72.70%

                │ old_bench.txt │           new_bench.txt            │
                │   allocs/op   │ allocs/op   vs base                │
HashWriteAndSum     45.000 ± 0%   7.000 ± 0%  -84.44% (p=0.000 n=10)
Root              1068.000 ± 0%   1.000 ± 0%  -99.91% (p=0.000 n=10)
RootWithPadHash    548.000 ± 0%   1.000 ± 0%  -99.82% (p=0.000 n=10)
geomean              297.5        1.913       -99.36%

@anacrolix anacrolix self-assigned this Apr 14, 2026
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