Skip to content

Commit

Permalink
use lru cache to replace map
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed Dec 9, 2024
1 parent 61a8ca1 commit c3669fd
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sync"
"time"

"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover/v5wire"
Expand Down Expand Up @@ -63,11 +64,9 @@ type codecV5 interface {
// UDPv5 is the implementation of protocol version 5.
type UDPv5 struct {
// static fields
conn UDPConn
tab *Table
nodeMu sync.Mutex
// addr -> node cache which cached the nodes we have sent packets to or received packets from recently.
cachedAddrNode map[string]*enode.Node
conn UDPConn
tab *Table
cachedAddrNode *lru.Cache[string, *enode.Node]
netrestrict *netutil.Netlist
priv *ecdsa.PrivateKey
localNode *enode.LocalNode
Expand Down Expand Up @@ -156,7 +155,7 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
t := &UDPv5{
// static fields
conn: newMeteredConn(conn),
cachedAddrNode: make(map[string]*enode.Node),
cachedAddrNode: lru.NewCache[string, *enode.Node](1024),
localNode: ln,
db: ln.Database(),
netrestrict: cfg.NetRestrict,
Expand Down Expand Up @@ -983,19 +982,14 @@ func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes {
}

func (t *UDPv5) putCache(addr string, node *enode.Node) {
t.nodeMu.Lock()
defer t.nodeMu.Unlock()
if n, ok := t.cachedAddrNode[addr]; ok {
if n, ok := t.cachedAddrNode.Get(addr); ok {
t.log.Debug("Update cached node", "old", n.ID(), "new", node.ID())
}
t.cachedAddrNode[addr] = node
t.cachedAddrNode.Add(addr, node)
}

func (t *UDPv5) GetCachedNode(addr string) (*enode.Node, bool) {
t.nodeMu.Lock()
defer t.nodeMu.Unlock()
n, ok := t.cachedAddrNode[addr]
return n, ok
return t.cachedAddrNode.Get(addr)
}

func (t *UDPv5) Table() *Table {
Expand Down

0 comments on commit c3669fd

Please sign in to comment.