@@ -26,12 +26,10 @@ import (
2626
2727 "github.com/ethereum/go-ethereum/common"
2828 "github.com/ethereum/go-ethereum/common/hexutil"
29- "github.com/ethereum/go-ethereum/core/rawdb"
3029 "github.com/ethereum/go-ethereum/core/types"
3130 "github.com/ethereum/go-ethereum/crypto"
3231 "github.com/ethereum/go-ethereum/rlp"
3332 "github.com/ethereum/go-ethereum/trie"
34- "github.com/ethereum/go-ethereum/triedb"
3533)
3634
3735func TestDeriveSha (t * testing.T ) {
@@ -40,7 +38,7 @@ func TestDeriveSha(t *testing.T) {
4038 t .Fatal (err )
4139 }
4240 for len (txs ) < 1000 {
43- exp := types .DeriveSha (txs , trie .NewEmpty ( triedb . NewDatabase ( rawdb . NewMemoryDatabase (), nil ) ))
41+ exp := types .DeriveSha (txs , trie .NewListHasher ( ))
4442 got := types .DeriveSha (txs , trie .NewStackTrie (nil ))
4543 if ! bytes .Equal (got [:], exp [:]) {
4644 t .Fatalf ("%d txs: got %x exp %x" , len (txs ), got , exp )
@@ -76,38 +74,53 @@ func TestEIP2718DeriveSha(t *testing.T) {
7674 }
7775}
7876
77+ // goos: darwin
78+ // goarch: arm64
79+ // pkg: github.com/ethereum/go-ethereum/core/types
80+ // cpu: Apple M1 Pro
81+ // BenchmarkDeriveSha200
82+ // BenchmarkDeriveSha200/std_trie
83+ // BenchmarkDeriveSha200/std_trie-8 6754 174074 ns/op 80054 B/op 1926 allocs/op
84+ // BenchmarkDeriveSha200/stack_trie
85+ // BenchmarkDeriveSha200/stack_trie-8 7296 162675 ns/op 745 B/op 19 allocs/op
7986func BenchmarkDeriveSha200 (b * testing.B ) {
8087 txs , err := genTxs (200 )
8188 if err != nil {
8289 b .Fatal (err )
8390 }
84- var exp common. Hash
85- var got common. Hash
91+ want := types . DeriveSha ( txs , trie . NewListHasher ())
92+
8693 b .Run ("std_trie" , func (b * testing.B ) {
8794 b .ReportAllocs ()
95+ var have common.Hash
8896 for b .Loop () {
89- exp = types .DeriveSha (txs , trie .NewEmpty (triedb .NewDatabase (rawdb .NewMemoryDatabase (), nil )))
97+ have = types .DeriveSha (txs , trie .NewListHasher ())
98+ }
99+ if have != want {
100+ b .Errorf ("have %x want %x" , have , want )
90101 }
91102 })
92103
104+ st := trie .NewStackTrie (nil )
93105 b .Run ("stack_trie" , func (b * testing.B ) {
94- b .ResetTimer ()
95106 b .ReportAllocs ()
107+ var have common.Hash
96108 for b .Loop () {
97- got = types .DeriveSha (txs , trie .NewStackTrie (nil ))
109+ st .Reset ()
110+ have = types .DeriveSha (txs , st )
111+ }
112+ if have != want {
113+ b .Errorf ("have %x want %x" , have , want )
98114 }
99115 })
100- if got != exp {
101- b .Errorf ("got %x exp %x" , got , exp )
102- }
103116}
104117
105118func TestFuzzDeriveSha (t * testing.T ) {
106119 // increase this for longer runs -- it's set to quite low for travis
107120 rndSeed := mrand .Int ()
108121 for i := 0 ; i < 10 ; i ++ {
109122 seed := rndSeed + i
110- exp := types .DeriveSha (newDummy (i ), trie .NewEmpty ( triedb . NewDatabase ( rawdb . NewMemoryDatabase (), nil ) ))
123+ exp := types .DeriveSha (newDummy (i ), trie .NewListHasher ( ))
111124 got := types .DeriveSha (newDummy (i ), trie .NewStackTrie (nil ))
112125 if ! bytes .Equal (got [:], exp [:]) {
113126 printList (t , newDummy (seed ))
@@ -135,7 +148,7 @@ func TestDerivableList(t *testing.T) {
135148 },
136149 }
137150 for i , tc := range tcs [1 :] {
138- exp := types .DeriveSha (flatList (tc ), trie .NewEmpty ( triedb . NewDatabase ( rawdb . NewMemoryDatabase (), nil ) ))
151+ exp := types .DeriveSha (flatList (tc ), trie .NewListHasher ( ))
139152 got := types .DeriveSha (flatList (tc ), trie .NewStackTrie (nil ))
140153 if ! bytes .Equal (got [:], exp [:]) {
141154 t .Fatalf ("case %d: got %x exp %x" , i , got , exp )
0 commit comments