mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
better performance the usage of maps
This commit is contained in:
parent
1591d165c4
commit
f93b81898e
7 changed files with 30 additions and 17 deletions
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -33,6 +32,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrDeleteRangeInterrupted = errors.New("safe delete range operation interrupted")
|
var ErrDeleteRangeInterrupted = errors.New("safe delete range operation interrupted")
|
||||||
|
|
@ -552,7 +552,9 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
|
||||||
|
|
||||||
if unaccounted.size > 0 {
|
if unaccounted.size > 0 {
|
||||||
log.Error("Database contains unaccounted data", "size", unaccounted.size, "count", unaccounted.count)
|
log.Error("Database contains unaccounted data", "size", unaccounted.size, "count", unaccounted.count)
|
||||||
for _, e := range slices.SortedFunc(maps.Values(unaccountedKeys), bytes.Compare) {
|
keys := maps.Values(unaccountedKeys)
|
||||||
|
slices.SortFunc(keys, bytes.Compare)
|
||||||
|
for _, e := range keys {
|
||||||
log.Error(fmt.Sprintf(" example key: %x", e))
|
log.Error(fmt.Sprintf(" example key: %x", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package snapshot
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -31,6 +30,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
bloomfilter "github.com/holiman/bloomfilter/v2"
|
bloomfilter "github.com/holiman/bloomfilter/v2"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -431,7 +431,8 @@ func (dl *diffLayer) AccountList() []common.Hash {
|
||||||
dl.lock.Lock()
|
dl.lock.Lock()
|
||||||
defer dl.lock.Unlock()
|
defer dl.lock.Unlock()
|
||||||
|
|
||||||
dl.accountList = slices.SortedFunc(maps.Keys(dl.accountData), common.Hash.Cmp)
|
dl.accountList = maps.Keys(dl.accountData)
|
||||||
|
slices.SortFunc(dl.accountList, common.Hash.Cmp)
|
||||||
dl.memory += uint64(len(dl.accountList) * common.HashLength)
|
dl.memory += uint64(len(dl.accountList) * common.HashLength)
|
||||||
return dl.accountList
|
return dl.accountList
|
||||||
}
|
}
|
||||||
|
|
@ -463,7 +464,8 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) []common.Hash {
|
||||||
dl.lock.Lock()
|
dl.lock.Lock()
|
||||||
defer dl.lock.Unlock()
|
defer dl.lock.Unlock()
|
||||||
|
|
||||||
storageList := slices.SortedFunc(maps.Keys(dl.storageData[accountHash]), common.Hash.Cmp)
|
storageList := maps.Keys(dl.storageData[accountHash])
|
||||||
|
slices.SortFunc(storageList, common.Hash.Cmp)
|
||||||
dl.storageList[accountHash] = storageList
|
dl.storageList[accountHash] = storageList
|
||||||
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
|
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
|
||||||
return storageList
|
return storageList
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,12 @@ package blobpool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
"maps"
|
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// evictHeap is a helper data structure to keep track of the cheapest bottleneck
|
// evictHeap is a helper data structure to keep track of the cheapest bottleneck
|
||||||
|
|
@ -54,7 +54,8 @@ func newPriceHeap(basefee *uint256.Int, blobfee *uint256.Int, index map[common.A
|
||||||
// Populate the heap in account sort order. Not really needed in practice,
|
// Populate the heap in account sort order. Not really needed in practice,
|
||||||
// but it makes the heap initialization deterministic and less annoying to
|
// but it makes the heap initialization deterministic and less annoying to
|
||||||
// test in unit tests.
|
// test in unit tests.
|
||||||
heap.addrs = slices.SortedFunc(maps.Keys(index), common.Address.Cmp)
|
heap.addrs = maps.Keys(index)
|
||||||
|
slices.SortFunc(heap.addrs, common.Address.Cmp)
|
||||||
for i, addr := range heap.addrs {
|
for i, addr := range heap.addrs {
|
||||||
heap.index[addr] = i
|
heap.index[addr] = i
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package legacypool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"maps"
|
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -42,6 +41,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/holiman/uint256"
|
"github.com/holiman/uint256"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -1724,7 +1724,7 @@ func (as *accountSet) addTx(tx *types.Transaction) {
|
||||||
// reuse. The returned slice should not be changed!
|
// reuse. The returned slice should not be changed!
|
||||||
func (as *accountSet) flatten() []common.Address {
|
func (as *accountSet) flatten() []common.Address {
|
||||||
if as.cache == nil {
|
if as.cache == nil {
|
||||||
as.cache = slices.Collect(maps.Keys(as.accounts))
|
as.cache = maps.Keys(as.accounts)
|
||||||
}
|
}
|
||||||
return as.cache
|
return as.cache
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
var special4, special6 Netlist
|
var special4, special6 Netlist
|
||||||
|
|
@ -323,7 +324,8 @@ func (s *DistinctNetSet) key(ip netip.Addr) netip.Prefix {
|
||||||
|
|
||||||
// String implements fmt.Stringer
|
// String implements fmt.Stringer
|
||||||
func (s DistinctNetSet) String() string {
|
func (s DistinctNetSet) String() string {
|
||||||
keys := slices.SortedFunc(maps.Keys(s.members), func(a, b netip.Prefix) int {
|
keys := maps.Keys(s.members)
|
||||||
|
slices.SortFunc(keys, func(a, b netip.Prefix) int {
|
||||||
return strings.Compare(a.String(), b.String())
|
return strings.Compare(a.String(), b.String())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -30,6 +29,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// State history records the state changes involved in executing a block. The
|
// State history records the state changes involved in executing a block. The
|
||||||
|
|
@ -250,11 +250,15 @@ type history struct {
|
||||||
// newHistory constructs the state history object with provided state change set.
|
// newHistory constructs the state history object with provided state change set.
|
||||||
func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map[common.Address][]byte, storages map[common.Address]map[common.Hash][]byte, rawStorageKey bool) *history {
|
func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map[common.Address][]byte, storages map[common.Address]map[common.Hash][]byte, rawStorageKey bool) *history {
|
||||||
var (
|
var (
|
||||||
accountList = slices.SortedFunc(maps.Keys(accounts), common.Address.Cmp)
|
accountList = maps.Keys(accounts)
|
||||||
storageList = make(map[common.Address][]common.Hash)
|
storageList = make(map[common.Address][]common.Hash)
|
||||||
)
|
)
|
||||||
|
slices.SortFunc(accountList, common.Address.Cmp)
|
||||||
|
|
||||||
for addr, slots := range storages {
|
for addr, slots := range storages {
|
||||||
storageList[addr] = slices.SortedFunc(maps.Keys(slots), common.Hash.Cmp)
|
addrs := maps.Keys(slots)
|
||||||
|
slices.SortFunc(addrs, common.Hash.Cmp)
|
||||||
|
storageList[addr] = addrs
|
||||||
}
|
}
|
||||||
version := historyVersion
|
version := historyVersion
|
||||||
if !rawStorageKey {
|
if !rawStorageKey {
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package pathdb
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"maps"
|
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
|
@ -28,6 +27,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// counter helps in tracking items and their corresponding sizes.
|
// counter helps in tracking items and their corresponding sizes.
|
||||||
|
|
@ -174,7 +174,8 @@ func (s *stateSet) accountList() []common.Hash {
|
||||||
s.listLock.Lock()
|
s.listLock.Lock()
|
||||||
defer s.listLock.Unlock()
|
defer s.listLock.Unlock()
|
||||||
|
|
||||||
list = slices.SortedFunc(maps.Keys(s.accountData), common.Hash.Cmp)
|
list = maps.Keys(s.accountData)
|
||||||
|
slices.SortFunc(list, common.Hash.Cmp)
|
||||||
s.accountListSorted = list
|
s.accountListSorted = list
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
@ -204,7 +205,8 @@ func (s *stateSet) storageList(accountHash common.Hash) []common.Hash {
|
||||||
s.listLock.Lock()
|
s.listLock.Lock()
|
||||||
defer s.listLock.Unlock()
|
defer s.listLock.Unlock()
|
||||||
|
|
||||||
list := slices.SortedFunc(maps.Keys(s.storageData[accountHash]), common.Hash.Cmp)
|
list := maps.Keys(s.storageData[accountHash])
|
||||||
|
slices.SortFunc(list, common.Hash.Cmp)
|
||||||
s.storageListSorted[accountHash] = list
|
s.storageListSorted[accountHash] = list
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue