mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-15 12:36:48 +00:00
trie/bintrie: use a sync.Pool when hashing binary tree nodes (#33989)
Binary tree hashing is quite slow, owing to many factors. One of them is the GC pressure that is the consequence of allocating many hashers, as a binary tree has 4x the size of an MPT. This PR introduces an optimization that already exists for the MPT: keep a pool of hashers, in order to reduce the amount of allocations.
This commit is contained in:
parent
95b9a2ed77
commit
1c9ddee16f
4 changed files with 49 additions and 8 deletions
39
trie/bintrie/hasher.go
Normal file
39
trie/bintrie/hasher.go
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
// Copyright 2026 go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package bintrie
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"hash"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var sha256Pool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return sha256.New()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSha256() hash.Hash {
|
||||||
|
h := sha256Pool.Get().(hash.Hash)
|
||||||
|
h.Reset()
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func returnSha256(h hash.Hash) {
|
||||||
|
sha256Pool.Put(h)
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package bintrie
|
package bintrie
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
|
@ -125,7 +124,8 @@ func (bt *InternalNode) Hash() common.Hash {
|
||||||
return bt.hash
|
return bt.hash
|
||||||
}
|
}
|
||||||
|
|
||||||
h := sha256.New()
|
h := newSha256()
|
||||||
|
defer returnSha256(h)
|
||||||
if bt.left != nil {
|
if bt.left != nil {
|
||||||
h.Write(bt.left.Hash().Bytes())
|
h.Write(bt.left.Hash().Bytes())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package bintrie
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
|
@ -51,7 +50,8 @@ func GetBinaryTreeKey(addr common.Address, key []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBinaryTreeKey(addr common.Address, offset []byte, overflow bool) []byte {
|
func getBinaryTreeKey(addr common.Address, offset []byte, overflow bool) []byte {
|
||||||
hasher := sha256.New()
|
hasher := newSha256()
|
||||||
|
defer returnSha256(hasher)
|
||||||
hasher.Write(zeroHash[:12])
|
hasher.Write(zeroHash[:12])
|
||||||
hasher.Write(addr[:])
|
hasher.Write(addr[:])
|
||||||
var buf [32]byte
|
var buf [32]byte
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package bintrie
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -114,14 +113,17 @@ func (bt *StemNode) Hash() common.Hash {
|
||||||
}
|
}
|
||||||
|
|
||||||
var data [StemNodeWidth]common.Hash
|
var data [StemNodeWidth]common.Hash
|
||||||
|
h := newSha256()
|
||||||
|
defer returnSha256(h)
|
||||||
for i, v := range bt.Values {
|
for i, v := range bt.Values {
|
||||||
if v != nil {
|
if v != nil {
|
||||||
h := sha256.Sum256(v)
|
h.Reset()
|
||||||
data[i] = common.BytesToHash(h[:])
|
h.Write(v)
|
||||||
|
h.Sum(data[i][:0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
h.Reset()
|
||||||
|
|
||||||
h := sha256.New()
|
|
||||||
for level := 1; level <= 8; level++ {
|
for level := 1; level <= 8; level++ {
|
||||||
for i := range StemNodeWidth / (1 << level) {
|
for i := range StemNodeWidth / (1 << level) {
|
||||||
h.Reset()
|
h.Reset()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue