Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primitives: Add core merkle tree root calcs. #2826

Merged
merged 2 commits into from
Dec 4, 2021

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Nov 20, 2021

This requires #2788.

This implements functions to calculate a Merkle tree root for a given set of leaves along with associated tests.

The functions are direct ports of the functions of the same names from blockchain/standalone.

Specifically, the following functions are updated and added along with their associated tests and benchmarks:

  • CalcMerkleRootInPlace (equiv to standalone.CalcMerkleRootInPlace)
  • CalcMerkleRoot (equiv to standalone.CalcMerkleRoot)

Note that this does not include the other functions in blockchain/standalone related to merkle root calculations from transaction trees since the primitives package does not yet implement a transaction type. Those will be implemented later after a type is introduced.

BenchmarkCalcMerkleRootInPlace
------------------------------
20_leaves      95551      12436 ns/op   0 B/op   0 allocs/op
1000_leaves     2047     591660 ns/op   0 B/op   0 allocs/op
2000_leaves     1018    1179846 ns/op   0 B/op   0 allocs/op
4000_leaves      507    2357246 ns/op   0 B/op   0 allocs/op
8000_leaves      254    4710341 ns/op   0 B/op   0 allocs/op
16000_leaves     127    9499098 ns/op   0 B/op   0 allocs/op
32000_leaves      62   18876558 ns/op   0 B/op   0 allocs/op

BenchmarkCalcMerkleRoot
-----------------------
20_leaves      92455      12840 ns/op       640 B/op   1 allocs/op
1000_leaves     1965     608127 ns/op     32769 B/op   1 allocs/op
2000_leaves      969    1261865 ns/op     65536 B/op   1 allocs/op
4000_leaves      493    2439323 ns/op    131073 B/op   1 allocs/op
8000_leaves      246    4850361 ns/op    262144 B/op   1 allocs/op
16000_leaves     122    9826700 ns/op    516101 B/op   1 allocs/op
32000_leaves      63   19420881 ns/op   1024008 B/op   1 allocs/op

This is work towards #2786.

@davecgh davecgh changed the title primitives: Add merkle root benchmarks. primitives: Add core merkle tree root calcs. Nov 20, 2021
Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

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

benchmarks
goos: linux
goarch: amd64
pkg: github.com/decred/dcrd/internal/staging/primitives
cpu: AMD Ryzen 9 3900XT 12-Core Processor           
BenchmarkCalcMerkleRootInPlace/20_leaves-24               140908              9020 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRootInPlace/1000_leaves-24               2755            409990 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRootInPlace/2000_leaves-24               1438            869987 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRootInPlace/4000_leaves-24                741           1642815 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRootInPlace/8000_leaves-24                338           3413965 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRootInPlace/16000_leaves-24               177           6479413 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRootInPlace/32000_leaves-24                91          13891543 ns/op               0 B/op          0 allocs/op
BenchmarkCalcMerkleRoot/20_leaves-24                       94971             15375 ns/op             640 B/op          1 allocs/op
BenchmarkCalcMerkleRoot/1000_leaves-24                      1644            718782 ns/op           32768 B/op          1 allocs/op
BenchmarkCalcMerkleRoot/2000_leaves-24                       776           1392608 ns/op           65538 B/op          1 allocs/op
BenchmarkCalcMerkleRoot/4000_leaves-24                       410           2634010 ns/op          131072 B/op          1 allocs/op
BenchmarkCalcMerkleRoot/8000_leaves-24                       261           5422578 ns/op          262144 B/op          1 allocs/op
BenchmarkCalcMerkleRoot/16000_leaves-24                      120           9022608 ns/op          516096 B/op          1 allocs/op
BenchmarkCalcMerkleRoot/32000_leaves-24                       62          17837654 ns/op         1024004 B/op          1 allocs/op

This implements functions to calculate a Merkle tree root for a given
set of leaves along with associated tests.

The functions are direct ports of the functions of the same names from
blockchain/standalone.

Note that this does not include the other functions in
blockchain/standalone related to merkle root calculations from
transaction trees since the primitives package does not yet implement
a transaction type.  Those will be implemented later after a transaction
type is introduced.
BenchmarkCalcMerkleRootInPlace
------------------------------
20_leaves      95551      12436 ns/op   0 B/op   0 allocs/op
1000_leaves     2047     591660 ns/op   0 B/op   0 allocs/op
2000_leaves     1018    1179846 ns/op   0 B/op   0 allocs/op
4000_leaves      507    2357246 ns/op   0 B/op   0 allocs/op
8000_leaves      254    4710341 ns/op   0 B/op   0 allocs/op
16000_leaves     127    9499098 ns/op   0 B/op   0 allocs/op
32000_leaves      62   18876558 ns/op   0 B/op   0 allocs/op

BenchmarkCalcMerkleRoot
-----------------------
20_leaves      92455      12840 ns/op       640 B/op   1 allocs/op
1000_leaves     1965     608127 ns/op     32769 B/op   1 allocs/op
2000_leaves      969    1261865 ns/op     65536 B/op   1 allocs/op
4000_leaves      493    2439323 ns/op    131073 B/op   1 allocs/op
8000_leaves      246    4850361 ns/op    262144 B/op   1 allocs/op
16000_leaves     122    9826700 ns/op    516101 B/op   1 allocs/op
32000_leaves      63   19420881 ns/op   1024008 B/op   1 allocs/op
@davecgh davecgh merged commit a4526b4 into decred:master Dec 4, 2021
@davecgh davecgh deleted the primitives_merkle branch December 4, 2021 23:09
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.

None yet

4 participants