Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Jul 22, 2021
1 parent 94e1505 commit 8975587
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
41 changes: 1 addition & 40 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"io"
"math/big"
mrand "math/rand"
"runtime"
"sort"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -88,7 +87,6 @@ const (
maxFutureBlocks = 256
maxTimeFutureBlocks = 30
badBlockLimit = 10
preLoadLimit = 64
maxBeyondBlocks = 2048

// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
Expand Down Expand Up @@ -1883,44 +1881,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
// Enable prefetching to pull in trie node paths while processing transactions
statedb.StartPrefetcher("chain")
activeState = statedb

accounts := make(map[common.Address]bool, block.Transactions().Len())
accountsSlice := make([]common.Address, 0, block.Transactions().Len())
for _, tx := range block.Transactions() {
from, err := types.Sender(signer, tx)
if err != nil {
break
}
accounts[from] = true
if tx.To() != nil {
accounts[*tx.To()] = true
}
}
for account, _ := range accounts {
accountsSlice = append(accountsSlice, account)
}
if len(accountsSlice) >= preLoadLimit {
objsChan := make(chan []*state.StateObject, runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
start := i * len(accountsSlice) / runtime.NumCPU()
end := (i + 1) * len(accountsSlice) / runtime.NumCPU()
if i+1 == runtime.NumCPU() {
end = len(accountsSlice)
}
go func(start, end int) {
objs := statedb.PreloadStateObject(accountsSlice[start:end])
objsChan <- objs
}(start, end)
}
for i := 0; i < runtime.NumCPU(); i++ {
objs := <-objsChan
if objs != nil {
for _, obj := range objs {
statedb.SetStateObject(obj)
}
}
}
}
statedb.TryPreload(block, signer)

//Process block using the parent state as reference point
substart := time.Now()
Expand Down
47 changes: 45 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ import (
"github.com/ethereum/go-ethereum/trie"
)

const defaultNumOfSlots = 100
const (
preLoadLimit = 64
defaultNumOfSlots = 100
)

type revision struct {
id int
Expand Down Expand Up @@ -516,7 +519,47 @@ func (s *StateDB) getStateObject(addr common.Address) *StateObject {
return nil
}

func (s *StateDB) PreloadStateObject(address []common.Address) []*StateObject {
func (s *StateDB) TryPreload(block *types.Block, signer types.Signer) {
accounts := make(map[common.Address]bool, block.Transactions().Len())
accountsSlice := make([]common.Address, 0, block.Transactions().Len())
for _, tx := range block.Transactions() {
from, err := types.Sender(signer, tx)
if err != nil {
break
}
accounts[from] = true
if tx.To() != nil {
accounts[*tx.To()] = true
}
}
for account, _ := range accounts {
accountsSlice = append(accountsSlice, account)
}
if len(accountsSlice) >= preLoadLimit && len(accountsSlice) > runtime.NumCPU() {
objsChan := make(chan []*StateObject, runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
start := i * len(accountsSlice) / runtime.NumCPU()
end := (i + 1) * len(accountsSlice) / runtime.NumCPU()
if i+1 == runtime.NumCPU() {
end = len(accountsSlice)
}
go func(start, end int) {
objs := s.preloadStateObject(accountsSlice[start:end])
objsChan <- objs
}(start, end)
}
for i := 0; i < runtime.NumCPU(); i++ {
objs := <-objsChan
if objs != nil {
for _, obj := range objs {
s.SetStateObject(obj)
}
}
}
}
}

func (s *StateDB) preloadStateObject(address []common.Address) []*StateObject {
// Prefer live objects if any is available
if s.snap == nil {
return nil
Expand Down
3 changes: 1 addition & 2 deletions eth/downloader/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import (
"context"
"sync"

"github.com/ethereum/go-ethereum/common/gopool"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common/gopool"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/rpc"
)
Expand Down
2 changes: 1 addition & 1 deletion p2p/rlpx/rlpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import (

"github.com/VictoriaMetrics/fastcache"
"github.com/golang/snappy"
"github.com/oxtoacart/bpool"
"golang.org/x/crypto/sha3"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/rlp"
"github.com/oxtoacart/bpool"
)

var snappyCache *fastcache.Cache
Expand Down

0 comments on commit 8975587

Please sign in to comment.