OpenAtomFoundation / xupercore

The kernel of xuperchain.
Apache License 2.0
61 stars 47 forks source link

ledger: reduce the memory allocation of the Merkle tree generation algorithm #229

Closed icexin closed 3 years ago

icexin commented 3 years ago

Description

What is the purpose of the change?

减少merkle算法的内存分配

一个区块里面的交易多了之后,生成默克尔树就比较耗时,这个提交减少了内存分配的次数,内存分配主要在sha256的生成上,每个double sha256都要进行2次分配。

这个PR使用了arena分配算法,一次性申请够所有的内存,再在每次计算sha256的时候单独分配, 另外使用了零分配的sha256函数来辅助计算sha256

修复前:

BenchmarkNormalMerkle-160            100          11672176 ns/op         2067500 B/op      30017 allocs/op

修复后:

BenchmarkNormalMerkle-160            100          10394688 ns/op         1835604 B/op          5 allocs/op

对于一个10000个交易的区块,内存分配次数从30017降到了5

Type of change

Please delete options that are not relevant.

codecov-commenter commented 3 years ago

Codecov Report

Merging #229 (2812d64) into master (9042b75) will increase coverage by 0.18%. The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #229      +/-   ##
==========================================
+ Coverage   47.39%   47.58%   +0.18%     
==========================================
  Files         133      133              
  Lines       11413    11429      +16     
==========================================
+ Hits         5409     5438      +29     
+ Misses       4869     4856      -13     
  Partials     1135     1135              
Impacted Files Coverage Δ
bcs/ledger/xledger/ledger/ledger_hash.go 44.00% <85.71%> (+7.30%) :arrow_up:
bcs/consensus/tdpos/tdpos.go 40.96% <0.00%> (-0.81%) :arrow_down:
kernel/consensus/base/driver/chained-bft/smr.go 50.46% <0.00%> (+0.92%) :arrow_up:
bcs/ledger/xledger/ledger/ledger.go 56.82% <0.00%> (+0.97%) :arrow_up:
bcs/network/p2pv1/conn.go 61.29% <0.00%> (+6.45%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 9042b75...2812d64. Read the comment docs.