core, eth, accounts, trie: use slices.Concat

This commit is contained in:
Hteev Oli 2024-10-29 12:34:17 +08:00 committed by GitHub
parent bce420b99f
commit 4397c712ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 17 additions and 12 deletions

View file

@ -18,6 +18,7 @@ package accounts
import (
"reflect"
"slices"
"sort"
"sync"
@ -255,7 +256,7 @@ func merge(slice []Wallet, wallets ...Wallet) []Wallet {
slice = append(slice, wallet)
continue
}
slice = append(slice[:n], append([]Wallet{wallet}, slice[n:]...)...)
slice = slices.Concat(slice[:n], []Wallet{wallet}, slice[n:])
}
return slice
}

View file

@ -21,6 +21,7 @@ import (
"context"
"errors"
"math"
"slices"
"sort"
"sync"
"sync/atomic"
@ -417,7 +418,7 @@ func (m *Matcher) distributor(dist chan *request, session *MatcherSession) {
// New retrieval request arrived to be distributed to some fetcher process
queue := requests[req.bit]
index := sort.Search(len(queue), func(i int) bool { return queue[i] >= req.section })
requests[req.bit] = append(queue[:index], append([]uint64{req.section}, queue[index:]...)...)
requests[req.bit] = slices.Concat(queue[:index], []uint64{req.section}, queue[index:])
// If it's a new bit and we have waiting fetchers, allocate to them
if len(queue) == 0 {
@ -485,7 +486,7 @@ func (m *Matcher) distributor(dist chan *request, session *MatcherSession) {
queue := requests[result.Bit]
for _, section := range missing {
index := sort.Search(len(queue), func(i int) bool { return queue[i] >= section })
queue = append(queue[:index], append([]uint64{section}, queue[index:]...)...)
queue = slices.Concat(queue[:index], []uint64{section}, queue[index:])
}
requests[result.Bit] = queue

View file

@ -135,7 +135,7 @@ func newFilter(config *params.ChainConfig, genesis *types.Block, headfn func() (
// Calculate the all the valid fork hash and fork next combos
var (
forksByBlock, forksByTime = gatherForks(config, genesis.Time())
forks = append(append([]uint64{}, forksByBlock...), forksByTime...)
forks = slices.Concat(forksByBlock, forksByTime)
sums = make([][4]byte, len(forks)+1) // 0th is the genesis
)
hash := crc32.ChecksumIEEE(genesis.Hash().Bytes())

View file

@ -20,6 +20,7 @@ package rawdb
import (
"bytes"
"encoding/binary"
"slices"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
@ -171,7 +172,7 @@ func headerKeyPrefix(number uint64) []byte {
// headerKey = headerPrefix + num (uint64 big endian) + hash
func headerKey(number uint64, hash common.Hash) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return slices.Concat(headerPrefix, encodeBlockNumber(number), hash.Bytes())
}
// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
@ -181,7 +182,7 @@ func headerTDKey(number uint64, hash common.Hash) []byte {
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
func headerHashKey(number uint64) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
return slices.Concat(headerPrefix, encodeBlockNumber(number), headerHashSuffix)
}
// headerNumberKey = headerNumberPrefix + hash
@ -191,12 +192,12 @@ func headerNumberKey(hash common.Hash) []byte {
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
func blockBodyKey(number uint64, hash common.Hash) []byte {
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return slices.Concat(blockBodyPrefix, encodeBlockNumber(number), hash.Bytes())
}
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
func blockReceiptsKey(number uint64, hash common.Hash) []byte {
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return slices.Concat(blockReceiptsPrefix, encodeBlockNumber(number), hash.Bytes())
}
// txLookupKey = txLookupPrefix + hash
@ -225,7 +226,7 @@ func storageSnapshotsKey(accountHash common.Hash) []byte {
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
key := slices.Concat(bloomBitsPrefix, make([]byte, 10), hash.Bytes())
binary.BigEndian.PutUint16(key[1:], uint16(bit))
binary.BigEndian.PutUint64(key[3:], section)

View file

@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"
"sync"
"sync/atomic"
"time"
@ -872,7 +873,7 @@ func (d *Downloader) processSnapSyncContent() error {
}
}
} else { // results already piled up, consume before handling pivot move
results = append(append([]*fetchResult{oldPivot}, oldTail...), results...)
results = slices.Concat([]*fetchResult{oldPivot}, oldTail, results)
}
// Split around the pivot block and process the two sides via snap/full sync
if !d.committed.Load() {

View file

@ -19,6 +19,7 @@ package trie
import (
"errors"
"fmt"
"slices"
"sync"
"github.com/ethereum/go-ethereum/common"
@ -553,7 +554,7 @@ func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
}
children = []childNode{{
node: node.Val,
path: append(append([]byte(nil), req.path...), key...),
path: slices.Concat(req.path, key),
}}
// Mark all internal nodes between shortNode and its **in disk**
// child as invalid. This is essential in the case of path mode
@ -595,7 +596,7 @@ func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
if node.Children[i] != nil {
children = append(children, childNode{
node: node.Children[i],
path: append(append([]byte(nil), req.path...), byte(i)),
path: slices.Concat(req.path, []byte{i}),
})
}
}