diff --git a/trie/committer.go b/trie/committer.go index 1d7b6fc5f0..050bc39fda 100644 --- a/trie/committer.go +++ b/trie/committer.go @@ -47,8 +47,8 @@ func newCommitter(nodes *trienode.NodeSet, tracer *tracer, collectLeaf bool, par type wrapNode struct { node *trienode.Node path string - leafHash common.Hash // optional, the parent hash of the relative leaf - leafBlob []byte // optional, the blob of the relative leaf + leafHash common.Hash // optional, the parent hash of the related leaf + leafBlob []byte // optional, the blob of the related leaf } // Commit collapses a node down into a hash node. @@ -192,7 +192,7 @@ func (c *committer) store(path []byte, n node) (node, *wrapNode) { } // Collect the corresponding leaf node if it's required. We don't check // full node since it's impossible to store value in fullNode. The key - // length of leaves should be exactly same.. + // length of leaves should be exactly same. if c.collectLeaf { if sn, ok := n.(*shortNode); ok { if val, ok := sn.Val.(valueNode); ok { diff --git a/trie/trie_test.go b/trie/trie_test.go index 505b517bc5..038330e759 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -21,6 +21,7 @@ import ( "encoding/binary" "errors" "fmt" + "github.com/ethereum/go-ethereum/internal/testrand" "hash" "io" "math/rand" @@ -1206,3 +1207,70 @@ func FuzzTrie(f *testing.F) { } }) } + +// goos: darwin +// goarch: arm64 +// pkg: github.com/ethereum/go-ethereum/trie +// BenchmarkCommit/commit-100nodes-single +// BenchmarkCommit/commit-100nodes-single-8 13508 90490 ns/op 115151 B/op 1461 allocs/op +// BenchmarkCommit/commit-100nodes-parallel +// BenchmarkCommit/commit-100nodes-parallel-8 13897 87488 ns/op 115208 B/op 1462 allocs/op +// BenchmarkCommit/commit-200nodes-single +// BenchmarkCommit/commit-200nodes-single-8 5971 182548 ns/op 233411 B/op 2935 allocs/op +// BenchmarkCommit/commit-200nodes-parallel +// BenchmarkCommit/commit-200nodes-parallel-8 5972 194446 ns/op 235053 B/op 2967 allocs/op +// BenchmarkCommit/commit-500nodes-single +// BenchmarkCommit/commit-500nodes-single-8 2443 444320 ns/op 604342 B/op 7604 allocs/op +// BenchmarkCommit/commit-500nodes-parallel +// BenchmarkCommit/commit-500nodes-parallel-8 2930 360024 ns/op 606174 B/op 7638 allocs/op +// BenchmarkCommit/commit-1000nodes-single +// BenchmarkCommit/commit-1000nodes-single-8 1353 911020 ns/op 1174328 B/op 14818 allocs/op +// BenchmarkCommit/commit-1000nodes-parallel +// BenchmarkCommit/commit-1000nodes-parallel-8 2217 615980 ns/op 1175860 B/op 14849 allocs/op +// BenchmarkCommit/commit-2000nodes-single +// BenchmarkCommit/commit-2000nodes-single-8 684 1765588 ns/op 2305835 B/op 28858 allocs/op +// BenchmarkCommit/commit-2000nodes-parallel +// BenchmarkCommit/commit-2000nodes-parallel-8 1153 1025471 ns/op 2307711 B/op 28890 allocs/op +// BenchmarkCommit/commit-5000nodes-single +// BenchmarkCommit/commit-5000nodes-single-8 250 4762405 ns/op 6471036 B/op 74907 allocs/op +// BenchmarkCommit/commit-5000nodes-parallel +// BenchmarkCommit/commit-5000nodes-parallel-8 450 2725071 ns/op 6471357 B/op 74938 allocs/op +func BenchmarkCommit(b *testing.B) { + benchmarkCommit(b, 100) + benchmarkCommit(b, 200) + benchmarkCommit(b, 500) + benchmarkCommit(b, 1000) + benchmarkCommit(b, 2000) + benchmarkCommit(b, 5000) +} + +func benchmarkCommit(b *testing.B, n int) { + b.Run(fmt.Sprintf("commit-%vnodes-single", n), func(b *testing.B) { + testCommit(b, n, false) + }) + b.Run(fmt.Sprintf("commit-%vnodes-parallel", n), func(b *testing.B) { + testCommit(b, n, true) + }) +} + +func testCommit(b *testing.B, n int, parallel bool) { + tries := make([]*Trie, b.N) + for i := 0; i < b.N; i++ { + tries[i] = NewEmpty(nil) + for j := 0; j < n; j++ { + key := testrand.Bytes(32) + val := testrand.Bytes(32) + tries[i].Update(key, val) + } + tries[i].Hash() + if !parallel { + tries[i].mutate = 0 + } + } + b.ResetTimer() + b.ReportAllocs() + + for i := 0; i < len(tries); i++ { + tries[i].Commit(true) + } +}