transaction: don't allocate new buffer to calculate hash
We can write directly to hash.Hash. name old time/op new time/op delta DecodeBinary-8 2.89µs ± 3% 2.82µs ± 5% ~ (p=0.052 n=10+10) DecodeJSON-8 13.0µs ± 1% 12.8µs ± 1% -1.54% (p=0.002 n=10+8) DecodeFromBytes-8 2.37µs ± 1% 2.25µs ± 5% -5.25% (p=0.000 n=9+10) name old alloc/op new alloc/op delta DecodeBinary-8 1.75kB ± 0% 1.53kB ± 0% -12.79% (p=0.000 n=10+10) DecodeJSON-8 3.49kB ± 0% 3.26kB ± 0% -6.42% (p=0.000 n=10+10) DecodeFromBytes-8 1.37kB ± 0% 1.14kB ± 0% -16.37% (p=0.000 n=10+10) name old allocs/op new allocs/op delta DecodeBinary-8 26.0 ± 0% 23.0 ± 0% -11.54% (p=0.000 n=10+10) DecodeJSON-8 58.0 ± 0% 55.0 ± 0% -5.17% (p=0.000 n=10+10) DecodeFromBytes-8 18.0 ± 0% 15.0 ± 0% -16.67% (p=0.000 n=10+10)
This commit is contained in:
parent
6d10cdc2f6
commit
892c9785ad
1 changed files with 7 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -234,13 +235,14 @@ func (t *Transaction) EncodeHashableFields() ([]byte, error) {
|
||||||
|
|
||||||
// createHash creates the hash of the transaction.
|
// createHash creates the hash of the transaction.
|
||||||
func (t *Transaction) createHash() error {
|
func (t *Transaction) createHash() error {
|
||||||
buf := io.NewBufBinWriter()
|
shaHash := sha256.New()
|
||||||
t.encodeHashableFields(buf.BinWriter)
|
bw := io.NewBinWriterFromIO(shaHash)
|
||||||
if buf.Err != nil {
|
t.encodeHashableFields(bw)
|
||||||
return buf.Err
|
if bw.Err != nil {
|
||||||
|
return bw.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.hash = hash.Sha256(buf.Bytes())
|
shaHash.Sum(t.hash[:0])
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue