Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: use LRU cache in txNoncer to avoid memory leaks #25603

Merged
merged 5 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions core/tx_noncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
lru "github.com/hashicorp/golang-lru"
)

// txNoncer is a tiny virtual state database to manage the executable nonces of
const nonceCacheLimit = 1024 * 50

// txNoncer is a LRU cache to manage the executable nonces of
// accounts in the pool, falling back to reading from a real state database if
// an account is unknown.
type txNoncer struct {
fallback *state.StateDB
nonces map[common.Address]uint64
nonces *lru.Cache
lock sync.Mutex
}

// newTxNoncer creates a new virtual state database to track the pool nonces.
// newTxNoncer creates a new LRU cache to track the pool nonces.
func newTxNoncer(statedb *state.StateDB) *txNoncer {
cache, _ := lru.New(nonceCacheLimit)
return &txNoncer{
fallback: statedb.Copy(),
nonces: make(map[common.Address]uint64),
nonces: cache,
}
}

Expand All @@ -45,43 +49,40 @@ func newTxNoncer(statedb *state.StateDB) *txNoncer {
func (txn *txNoncer) get(addr common.Address) uint64 {
// We use mutex for get operation is the underlying
// state will mutate db even for read access.
txn.lock.Lock()
defer txn.lock.Unlock()

if _, ok := txn.nonces[addr]; !ok {
txn.nonces[addr] = txn.fallback.GetNonce(addr)
if _, ok := txn.nonces.Get(addr); !ok {
txn.lock.Lock()
txn.nonces.Add(addr, txn.fallback.GetNonce(addr))
txn.lock.Unlock()
dbadoy marked this conversation as resolved.
Show resolved Hide resolved
}
return txn.nonces[addr]
nonce, _ := txn.nonces.Get(addr)
return nonce.(uint64)
}

// set inserts a new virtual nonce into the virtual state database to be returned
// set inserts a new virtual nonce into the LRU cache to be returned
// whenever the pool requests it instead of reaching into the real state database.
func (txn *txNoncer) set(addr common.Address, nonce uint64) {
txn.lock.Lock()
defer txn.lock.Unlock()

txn.nonces[addr] = nonce
txn.nonces.Add(addr, nonce)
}

// setIfLower updates a new virtual nonce into the virtual state database if the
// setIfLower updates a new virtual nonce into the LRU cache if the
// the new one is lower.
func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
txn.lock.Lock()
defer txn.lock.Unlock()

if _, ok := txn.nonces[addr]; !ok {
txn.nonces[addr] = txn.fallback.GetNonce(addr)
if _, ok := txn.nonces.Get(addr); !ok {
txn.lock.Lock()
dbadoy marked this conversation as resolved.
Show resolved Hide resolved
txn.nonces.Add(addr, txn.fallback.GetNonce(addr))
txn.lock.Unlock()
}
if txn.nonces[addr] <= nonce {

cachedNonce, _ := txn.nonces.Get(addr)
if cachedNonce.(uint64) <= nonce {
return
}
txn.nonces[addr] = nonce
txn.nonces.Add(addr, nonce)
}

// setAll sets the nonces for all accounts to the given map.
func (txn *txNoncer) setAll(all map[common.Address]uint64) {
txn.lock.Lock()
defer txn.lock.Unlock()

txn.nonces = all
for addr, nonce := range all {
txn.nonces.Add(addr, nonce)
}
}
145 changes: 145 additions & 0 deletions core/tx_noncer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package core

import (
"fmt"
"math/rand"
"runtime"
"sync"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
lru "github.com/hashicorp/golang-lru"
)

type txNoncerMap struct {
fallback *state.StateDB
nonces map[common.Address]uint64
lock sync.Mutex
}

func (txn *txNoncerMap) get(addr common.Address) uint64 {
txn.lock.Lock()
defer txn.lock.Unlock()

if _, ok := txn.nonces[addr]; !ok {
txn.nonces[addr] = txn.fallback.GetNonce(addr)
}
return txn.nonces[addr]
}

func (txn *txNoncerMap) set(addr common.Address, nonce uint64) {
txn.lock.Lock()
defer txn.lock.Unlock()

txn.nonces[addr] = nonce
}

type txNoncerLRU struct {
fallback *state.StateDB
nonces *lru.Cache
}

func newTxNoncerMap(statedb *state.StateDB) *txNoncerMap {
return &txNoncerMap{
fallback: statedb.Copy(),
nonces: make(map[common.Address]uint64),
}
}

func newTxNoncerLRU(statedb *state.StateDB) *txNoncerLRU {
// lru cache size 1024 * 50 allocated 10 ~ 20 MB
cache, _ := lru.New(1024 * 50)
return &txNoncerLRU{
fallback: statedb.Copy(),
nonces: cache,
}
}

func TestTxNoncerMap(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
txNoncer := newTxNoncerMap(statedb)

var m runtime.MemStats
runtime.GC()
for i := 0; i < 10000000; i++ {
var b [20]byte
rand.Read(b[:])
addr := common.Address(b)
txNoncer.set(addr, uint64(0))
}
runtime.ReadMemStats(&m)
fmt.Printf("Object memory: %.3f MB current\n", float64(m.Alloc)/1024/1024)
fmt.Printf("System memory: %.3f MB current\n", float64(m.Sys)/1024/1024)
fmt.Printf("Allocations: %.3f million\n", float64(m.Mallocs)/1000000)
}

func TestTxNoncerLRU(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
txNoncer := newTxNoncerLRU(statedb)

var m runtime.MemStats
runtime.GC()
for i := 0; i < 10000000; i++ {
var b [20]byte
rand.Read(b[:])
addr := common.Address(b)
txNoncer.nonces.Add(addr, uint64(1))
}
runtime.ReadMemStats(&m)
fmt.Printf("Object memory: %.3f MB current\n", float64(m.Alloc)/1024/1024)
fmt.Printf("System memory: %.3f MB current\n", float64(m.Sys)/1024/1024)
fmt.Printf("Allocations: %.3f million\n", float64(m.Mallocs)/1000000)
}

func BenchmarkMapStore(b *testing.B) {
dbadoy marked this conversation as resolved.
Show resolved Hide resolved
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
txNoncer := newTxNoncerMap(statedb)
for i := 0; i < b.N; i++ {
var byt [20]byte
rand.Read(byt[:])
addr := common.Address(byt)
txNoncer.set(addr, uint64(1))
}
}

func BenchmarkLRUStore(b *testing.B) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
txNoncer := newTxNoncerLRU(statedb)
for i := 0; i < b.N; i++ {
var byt [20]byte
rand.Read(byt[:])
addr := common.Address(byt)
txNoncer.nonces.Add(addr, uint64(1))
}
}

func BenchmarkMapLoad(b *testing.B) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
txNoncer := newTxNoncerMap(statedb)
var byt [20]byte
rand.Read(byt[:])
addr := common.Address(byt)
txNoncer.set(addr, uint64(1))

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = txNoncer.get(addr)
}
}

func BenchmarkLRULoad(b *testing.B) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
txNoncer := newTxNoncerLRU(statedb)
var byt [20]byte
rand.Read(byt[:])
addr := common.Address(byt)
txNoncer.nonces.Add(addr, uint64(1))

b.ResetTimer()
for i := 0; i < b.N; i++ {
v, _ := txNoncer.nonces.Get(addr)
_ = v.(uint64)
}
}
3 changes: 0 additions & 3 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,6 @@ func (pool *TxPool) SetGasPrice(price *big.Int) {
// Nonce returns the next nonce of an account, with all transactions executable
// by the pool already applied on top.
func (pool *TxPool) Nonce(addr common.Address) uint64 {
holiman marked this conversation as resolved.
Show resolved Hide resolved
pool.mu.RLock()
defer pool.mu.RUnlock()

return pool.pendingNonces.get(addr)
}

Expand Down