diff --git a/core/types/bloom9.go b/core/types/bloom9.go index 2c9d9763af..962ba46d47 100644 --- a/core/types/bloom9.go +++ b/core/types/bloom9.go @@ -140,10 +140,11 @@ func Bloom9(data []byte) []byte { // bloomValues returns the bytes (index-value pairs) to set for the given data func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byte) { - sha := crypto.NewKeccakState() + sha := hasherPool.Get().(crypto.KeccakState) sha.Reset() sha.Write(data) sha.Read(hashbuf) + hasherPool.Put(sha) // The actual bits to flip v1 := byte(1 << (hashbuf[1] & 0x7)) v2 := byte(1 << (hashbuf[3] & 0x7)) diff --git a/core/types/hashing.go b/core/types/hashing.go index 2e6fb2bf31..3cc22d50d1 100644 --- a/core/types/hashing.go +++ b/core/types/hashing.go @@ -27,6 +27,11 @@ import ( "github.com/ethereum/go-ethereum/rlp" ) +// hasherPool holds LegacyKeccak256 hashers for rlpHash. +var hasherPool = sync.Pool{ + New: func() interface{} { return crypto.NewKeccakState() }, +} + // encodeBufferPool holds temporary encoder buffers for DeriveSha and TX encoding. var encodeBufferPool = sync.Pool{ New: func() interface{} { return new(bytes.Buffer) }, @@ -50,7 +55,8 @@ func getPooledBuffer(size uint64) ([]byte, *bytes.Buffer, error) { // rlpHash encodes x and hashes the encoded bytes. func rlpHash(x interface{}) (h common.Hash) { - sha := crypto.NewKeccakState() + sha := hasherPool.Get().(crypto.KeccakState) + defer hasherPool.Put(sha) sha.Reset() rlp.Encode(sha, x) sha.Read(h[:]) @@ -60,7 +66,8 @@ func rlpHash(x interface{}) (h common.Hash) { // prefixedRlpHash writes the prefix into the hasher before rlp-encoding x. // It's used for typed transactions. func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) { - sha := crypto.NewKeccakState() + sha := hasherPool.Get().(crypto.KeccakState) + defer hasherPool.Put(sha) sha.Reset() sha.Write([]byte{prefix}) rlp.Encode(sha, x)