core/types: restore hasherpool in types

This commit is contained in:
MariusVanDerWijden 2025-05-08 15:06:30 +02:00 committed by Gary Rong
parent c84f999c9e
commit debc601050
2 changed files with 11 additions and 3 deletions

View file

@ -140,10 +140,11 @@ func Bloom9(data []byte) []byte {
// bloomValues returns the bytes (index-value pairs) to set for the given data // 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) { func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byte) {
sha := crypto.NewKeccakState() sha := hasherPool.Get().(crypto.KeccakState)
sha.Reset() sha.Reset()
sha.Write(data) sha.Write(data)
sha.Read(hashbuf) sha.Read(hashbuf)
hasherPool.Put(sha)
// The actual bits to flip // The actual bits to flip
v1 := byte(1 << (hashbuf[1] & 0x7)) v1 := byte(1 << (hashbuf[1] & 0x7))
v2 := byte(1 << (hashbuf[3] & 0x7)) v2 := byte(1 << (hashbuf[3] & 0x7))

View file

@ -27,6 +27,11 @@ import (
"github.com/ethereum/go-ethereum/rlp" "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. // encodeBufferPool holds temporary encoder buffers for DeriveSha and TX encoding.
var encodeBufferPool = sync.Pool{ var encodeBufferPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) }, 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. // rlpHash encodes x and hashes the encoded bytes.
func rlpHash(x interface{}) (h common.Hash) { func rlpHash(x interface{}) (h common.Hash) {
sha := crypto.NewKeccakState() sha := hasherPool.Get().(crypto.KeccakState)
defer hasherPool.Put(sha)
sha.Reset() sha.Reset()
rlp.Encode(sha, x) rlp.Encode(sha, x)
sha.Read(h[:]) 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. // prefixedRlpHash writes the prefix into the hasher before rlp-encoding x.
// It's used for typed transactions. // It's used for typed transactions.
func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) { func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) {
sha := crypto.NewKeccakState() sha := hasherPool.Get().(crypto.KeccakState)
defer hasherPool.Put(sha)
sha.Reset() sha.Reset()
sha.Write([]byte{prefix}) sha.Write([]byte{prefix})
rlp.Encode(sha, x) rlp.Encode(sha, x)