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

blockchain/standalone: Add merkle root calc funcs. #1809

Merged
merged 1 commit into from
Aug 6, 2019

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Aug 1, 2019

This requires PR #1808.

This adds functions to calculate a merkle root to the blockchain/standalone module. Rather than copying the existing functions from blockchain, it introduces new optimized functions that have simpler code and are also generally easier to use.

The new functions simply return the calculated root instead of all of the intermediate hashes which no known callers were actually using anyway.

It also adds a basic usage example, benchmarks, updates the documentation and includes comprehensive tests.

As can be seen by the benchmarks below, the new code isn't significantly faster in terms of execution speed since it is still dominated by the actual hashing, however, it does significantly reduce the number of allocations which reduces pressure on the GC, particularly during bursty situations such as IBD. It also scales much better to larger numbers of transactions.

The following is a before and after comparison of calculating merkle roots for various numbers of leaves:

benchmark                       old ns/op    new ns/op   delta
---------------------------------------------------------------
BenchmarkCalcMerkleRoot/20      25637        24287       -5.27%
BenchmarkCalcMerkleRoot/1000    1252749      1209130     -3.48%
BenchmarkCalcMerkleRoot/2000    2601754      2343198     -9.94%
BenchmarkCalcMerkleRoot/4000    4873969      4629169     -5.02%
BenchmarkCalcMerkleRoot/8000    9831116      9270095     -5.71%
BenchmarkCalcMerkleRoot/16000   19172545     19067585    -0.55%
BenchmarkCalcMerkleRoot/32000   37980323     35746500    -5.88%

benchmark                       old allocs   new allocs  delta
----------------------------------------------------------------
BenchmarkCalcMerkleRoot/20      106          64          -39.62%
BenchmarkCalcMerkleRoot/1000    5006         3004        -39.99%
BenchmarkCalcMerkleRoot/2000    10006        6004        -40.00%
BenchmarkCalcMerkleRoot/4000    20006        12004       -40.00%
BenchmarkCalcMerkleRoot/8000    40006        24004       -40.00%
BenchmarkCalcMerkleRoot/16000   80006        48004       -40.00%
BenchmarkCalcMerkleRoot/32000   160006       96004       -40.00%

benchmark                       old bytes    new bytes   delta
----------------------------------------------------------------
BenchmarkCalcMerkleRoot/20      6896         4432        -35.73%
BenchmarkCalcMerkleRoot/1000    320688       208272      -35.05%
BenchmarkCalcMerkleRoot/2000    641072       416272      -35.07%
BenchmarkCalcMerkleRoot/4000    1281840      832272      -35.07%
BenchmarkCalcMerkleRoot/8000    2563376      1664272     -35.07%
BenchmarkCalcMerkleRoot/16000   5126448      3328272     -35.08%
BenchmarkCalcMerkleRoot/32000   10252592     6656273     -35.08%

@davecgh davecgh added this to the 1.5.0 milestone Aug 1, 2019
@davecgh
Copy link
Member Author

davecgh commented Aug 1, 2019

$ go test -coverprofile=cov.out && go tool cover -func=cov.out
PASS
coverage: 100.0% of statements
ok      github.com/decred/dcrd/blockchain/standalone    0.385s
github.com/decred/dcrd/blockchain/standalone/error.go:36:       String                  100.0%
github.com/decred/dcrd/blockchain/standalone/error.go:52:       Error                   100.0%
github.com/decred/dcrd/blockchain/standalone/error.go:57:       ruleError               100.0%
github.com/decred/dcrd/blockchain/standalone/error.go:63:       IsErrorCode             100.0%
github.com/decred/dcrd/blockchain/standalone/merkle.go:29:      CalcMerkleRootInPlace   100.0%
github.com/decred/dcrd/blockchain/standalone/merkle.go:91:      CalcMerkleRoot          100.0%
github.com/decred/dcrd/blockchain/standalone/merkle.go:112:     CalcTxTreeMerkleRoot    100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:27:         HashToBig               100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:62:         CompactToBig            100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:94:         BigToCompact            100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:142:        CalcWork                100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:158:        checkProofOfWorkRange   100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:177:        CheckProofOfWorkRange   100.0%
github.com/decred/dcrd/blockchain/standalone/pow.go:185:        CheckProofOfWork        100.0%
total:                                                          (statements)            100.0%

Copy link
Member

@matheusd matheusd left a comment

Choose a reason for hiding this comment

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

Verified the testdata comes from the commented source blocks.

This adds functions to calculate a merkle root to the
blockchain/standalone module.  Rather than copying the existing
functions from blockchain, it introduces new optimized functions that
have simpler code and are also generally easier to use.  The new
functions simply return the calculated root instead of all of the
intermediate hashes which no known callers were actually using anyway.

It also adds a basic usage example, benchmarks, updates the
documentation and includes comprehensive tests.

As can be seen by the benchmarks below, the new code isn't significantly
faster in terms of execution speed since it is still dominated by the
actual hashing, however, it does significantly reduce the number of
allocations which reduces pressure on the GC, particularly during bursty
situations such as IBD.  It also scales much better to larger numbers of
transactions.

The following is a before and after comparison of calculating merkle
roots for various numbers of leaves:

benchmark                       old ns/op    new ns/op   delta
---------------------------------------------------------------
BenchmarkCalcMerkleRoot/20      25637        24287       -5.27%
BenchmarkCalcMerkleRoot/1000    1252749      1209130     -3.48%
BenchmarkCalcMerkleRoot/2000    2601754      2343198     -9.94%
BenchmarkCalcMerkleRoot/4000    4873969      4629169     -5.02%
BenchmarkCalcMerkleRoot/8000    9831116      9270095     -5.71%
BenchmarkCalcMerkleRoot/16000   19172545     19067585    -0.55%
BenchmarkCalcMerkleRoot/32000   37980323     35746500    -5.88%

benchmark                       old allocs   new allocs  delta
----------------------------------------------------------------
BenchmarkCalcMerkleRoot/20      106          64          -39.62%
BenchmarkCalcMerkleRoot/1000    5006         3004        -39.99%
BenchmarkCalcMerkleRoot/2000    10006        6004        -40.00%
BenchmarkCalcMerkleRoot/4000    20006        12004       -40.00%
BenchmarkCalcMerkleRoot/8000    40006        24004       -40.00%
BenchmarkCalcMerkleRoot/16000   80006        48004       -40.00%
BenchmarkCalcMerkleRoot/32000   160006       96004       -40.00%

benchmark                       old bytes    new bytes   delta
----------------------------------------------------------------
BenchmarkCalcMerkleRoot/20      6896         4432        -35.73%
BenchmarkCalcMerkleRoot/1000    320688       208272      -35.05%
BenchmarkCalcMerkleRoot/2000    641072       416272      -35.07%
BenchmarkCalcMerkleRoot/4000    1281840      832272      -35.07%
BenchmarkCalcMerkleRoot/8000    2563376      1664272     -35.07%
BenchmarkCalcMerkleRoot/16000   5126448      3328272     -35.08%
BenchmarkCalcMerkleRoot/32000   10252592     6656273     -35.08%
@davecgh davecgh force-pushed the standalone_add_merkle_funcs branch from 4b847ad to bd4e5c2 Compare August 6, 2019 15:41
@davecgh davecgh merged commit bd4e5c2 into decred:master Aug 6, 2019
@davecgh davecgh deleted the standalone_add_merkle_funcs branch August 6, 2019 15:55
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

5 participants