celestiaorg / smt

A Go library that implements a Sparse Merkle tree for a key-value map.
https://godoc.org/github.com/celestiaorg/smt
MIT License
138 stars 53 forks source link

Improve treeHasher performance #42

Closed cuonglm closed 3 years ago

cuonglm commented 3 years ago

By doing two things:

That helps improves the sppeed, and less allocations for Update/Delete operations:

name                       old time/op    new time/op    delta
SparseMerkleTree_Update-8    11.9µs ± 3%    11.2µs ± 3%   -5.89%  (p=0.008 n=5+5)
SparseMerkleTree_Delete-8    9.66µs ± 2%    5.40µs ± 2%  -44.12%  (p=0.008 n=5+5)

name                       old alloc/op   new alloc/op   delta
SparseMerkleTree_Update-8    17.7kB ± 0%    16.1kB ± 1%   -9.42%  (p=0.016 n=4+5)
SparseMerkleTree_Delete-8    16.3kB ± 0%    13.9kB ± 0%  -14.82%  (p=0.008 n=5+5)

name                       old allocs/op  new allocs/op  delta
SparseMerkleTree_Update-8       117 ± 0%        63 ± 0%  -46.15%  (p=0.029 n=4+4)
SparseMerkleTree_Delete-8      94.4 ± 1%      28.6 ± 2%  -69.70%  (p=0.008 n=5+5)
cuonglm commented 3 years ago

cc @odeke-em

codecov-commenter commented 3 years ago

Codecov Report

Merging #42 (2066960) into master (072abf0) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #42   +/-   ##
=======================================
  Coverage   86.05%   86.05%           
=======================================
  Files           6        6           
  Lines         466      466           
=======================================
  Hits          401      401           
  Misses         37       37           
  Partials       28       28           
Impacted Files Coverage Δ
treehasher.go 100.00% <100.00%> (ø)

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 072abf0...2066960. Read the comment docs.

cuonglm commented 3 years ago

LGTM, thank you @cuonglm! Let's also see if we can shave off by reusing the hashers

Yeah, I think it can, but want to keep the PR as small as possible.

odeke-em commented 3 years ago

LGTM, thank you @cuonglm! Let's also see if we can shave off by reusing the hashers

Yeah, I think it can, but want to keep the PR as small as possible.

Absolutely, I meant to write/add "later on"