@@ -26,12 +26,10 @@ import (
26
26
27
27
"github.com/ethereum/go-ethereum/common"
28
28
"github.com/ethereum/go-ethereum/common/hexutil"
29
- "github.com/ethereum/go-ethereum/core/rawdb"
30
29
"github.com/ethereum/go-ethereum/core/types"
31
30
"github.com/ethereum/go-ethereum/crypto"
32
31
"github.com/ethereum/go-ethereum/rlp"
33
32
"github.com/ethereum/go-ethereum/trie"
34
- "github.com/ethereum/go-ethereum/triedb"
35
33
)
36
34
37
35
func TestDeriveSha (t * testing.T ) {
@@ -40,7 +38,7 @@ func TestDeriveSha(t *testing.T) {
40
38
t .Fatal (err )
41
39
}
42
40
for len (txs ) < 1000 {
43
- exp := types .DeriveSha (txs , trie .NewEmpty ( triedb . NewDatabase ( rawdb . NewMemoryDatabase (), nil ) ))
41
+ exp := types .DeriveSha (txs , trie .NewListHasher ( ))
44
42
got := types .DeriveSha (txs , trie .NewStackTrie (nil ))
45
43
if ! bytes .Equal (got [:], exp [:]) {
46
44
t .Fatalf ("%d txs: got %x exp %x" , len (txs ), got , exp )
@@ -76,38 +74,53 @@ func TestEIP2718DeriveSha(t *testing.T) {
76
74
}
77
75
}
78
76
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
79
86
func BenchmarkDeriveSha200 (b * testing.B ) {
80
87
txs , err := genTxs (200 )
81
88
if err != nil {
82
89
b .Fatal (err )
83
90
}
84
- var exp common. Hash
85
- var got common. Hash
91
+ want := types . DeriveSha ( txs , trie . NewListHasher ())
92
+
86
93
b .Run ("std_trie" , func (b * testing.B ) {
87
94
b .ReportAllocs ()
95
+ var have common.Hash
88
96
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 )
90
101
}
91
102
})
92
103
104
+ st := trie .NewStackTrie (nil )
93
105
b .Run ("stack_trie" , func (b * testing.B ) {
94
- b .ResetTimer ()
95
106
b .ReportAllocs ()
107
+ var have common.Hash
96
108
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 )
98
114
}
99
115
})
100
- if got != exp {
101
- b .Errorf ("got %x exp %x" , got , exp )
102
- }
103
116
}
104
117
105
118
func TestFuzzDeriveSha (t * testing.T ) {
106
119
// increase this for longer runs -- it's set to quite low for travis
107
120
rndSeed := mrand .Int ()
108
121
for i := 0 ; i < 10 ; i ++ {
109
122
seed := rndSeed + i
110
- exp := types .DeriveSha (newDummy (i ), trie .NewEmpty ( triedb . NewDatabase ( rawdb . NewMemoryDatabase (), nil ) ))
123
+ exp := types .DeriveSha (newDummy (i ), trie .NewListHasher ( ))
111
124
got := types .DeriveSha (newDummy (i ), trie .NewStackTrie (nil ))
112
125
if ! bytes .Equal (got [:], exp [:]) {
113
126
printList (t , newDummy (seed ))
@@ -135,7 +148,7 @@ func TestDerivableList(t *testing.T) {
135
148
},
136
149
}
137
150
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 ( ))
139
152
got := types .DeriveSha (flatList (tc ), trie .NewStackTrie (nil ))
140
153
if ! bytes .Equal (got [:], exp [:]) {
141
154
t .Fatalf ("case %d: got %x exp %x" , i , got , exp )
0 commit comments