File tree Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -94,14 +94,25 @@ func calcDatasetSize(epoch int) uint64 {
94
94
// reused between hash runs instead of requiring new ones to be created.
95
95
type hasher func (dest []byte , data []byte )
96
96
97
- // makeHasher creates a repetitive hasher, allowing the same hash data structures
98
- // to be reused between hash runs instead of requiring new ones to be created.
99
- // The returned function is not thread safe!
97
+ // makeHasher creates a repetitive hasher, allowing the same hash data structures to
98
+ // be reused between hash runs instead of requiring new ones to be created. The returned
99
+ // function is not thread safe!
100
100
func makeHasher (h hash.Hash ) hasher {
101
+ // sha3.state supports Read to get the sum, use it to avoid the overhead of Sum.
102
+ // Read alters the state but we reset the hash before every operation.
103
+ type readerHash interface {
104
+ hash.Hash
105
+ Read ([]byte ) (int , error )
106
+ }
107
+ rh , ok := h .(readerHash )
108
+ if ! ok {
109
+ panic ("can't find Read method on hash" )
110
+ }
111
+ outputLen := rh .Size ()
101
112
return func (dest []byte , data []byte ) {
102
- h . Write ( data )
103
- h . Sum ( dest [: 0 ] )
104
- h . Reset ( )
113
+ rh . Reset ( )
114
+ rh . Write ( data )
115
+ rh . Read ( dest [: outputLen ] )
105
116
}
106
117
}
107
118
You can’t perform that action at this time.
0 commit comments