revert the usage of hashBuf

This commit is contained in:
maskpp 2025-05-21 20:49:47 +08:00
parent b29998a3af
commit 8a14428af3

View file

@ -74,17 +74,9 @@ func NewKeccakState() KeccakState {
return sha3.NewLegacyKeccak256().(KeccakState) return sha3.NewLegacyKeccak256().(KeccakState)
} }
type keccakState struct {
KeccakState
hashBuf common.Hash
}
var hasherPool = sync.Pool{ var hasherPool = sync.Pool{
New: func() any { New: func() any {
return &keccakState{ return sha3.NewLegacyKeccak256().(KeccakState)
hashBuf: common.Hash{},
KeccakState: sha3.NewLegacyKeccak256().(KeccakState),
}
}, },
} }
@ -110,26 +102,26 @@ func Keccak256RLP(x interface{}) []byte {
// Keccak256Hash calculates and returns the Keccak256 hash of the input data, // Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// converting it to an internal Hash data structure. // converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) common.Hash { func Keccak256Hash(data ...[]byte) (h common.Hash) {
d := hasherPool.Get().(*keccakState) d := hasherPool.Get().(KeccakState)
d.Reset() d.Reset()
for _, b := range data { for _, b := range data {
d.Write(b) d.Write(b)
} }
d.Read(d.hashBuf[:]) d.Read(h[:])
hasherPool.Put(d) hasherPool.Put(d)
return d.hashBuf return h
} }
// Keccak256RLPHash calculates and returns the Keccak256 hash of the input interface, // Keccak256RLPHash calculates and returns the Keccak256 hash of the input interface,
// converting it to an internal Hash data structure. // converting it to an internal Hash data structure.
func Keccak256RLPHash(x interface{}) common.Hash { func Keccak256RLPHash(x interface{}) (h common.Hash) {
d := hasherPool.Get().(*keccakState) d := hasherPool.Get().(KeccakState)
d.Reset() d.Reset()
rlp.Encode(d, x) rlp.Encode(d, x)
d.Read(d.hashBuf[:]) d.Read(h[:])
hasherPool.Put(d) hasherPool.Put(d)
return d.hashBuf return h
} }
// Keccak512 calculates and returns the Keccak512 hash of the input data. // Keccak512 calculates and returns the Keccak512 hash of the input data.