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

peerstore: remove sync.Pool for expiringAddrs #3093

Open
wants to merge 2 commits into
base: sukun/peerstore-bms
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 2 additions & 26 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ func ttlIsConnected(ttl time.Duration) bool {
return ttl >= peerstore.ConnectedAddrTTL
}

var expiringAddrPool = sync.Pool{New: func() any { return &expiringAddr{} }}

func getExpiringAddrs() *expiringAddr {
a := expiringAddrPool.Get().(*expiringAddr)
a.heapIndex = -1
return a
}

func putExpiringAddrs(ea *expiringAddr) {
if ea == nil {
return
}
*ea = expiringAddr{}
expiringAddrPool.Put(ea)
}

type peerRecordState struct {
Envelope *record.Envelope
Seq uint64
Expand Down Expand Up @@ -280,7 +264,6 @@ func (mab *memoryAddrBook) gc() {
if !ok {
return
}
putExpiringAddrs(ea)
mab.maybeDeleteSignedPeerRecordUnlocked(ea.Peer)
}
}
Expand Down Expand Up @@ -382,8 +365,7 @@ func (mab *memoryAddrBook) addAddrsUnlocked(p peer.ID, addrs []ma.Multiaddr, ttl
a, found := mab.addrs.FindAddr(p, addr)
if !found {
// not found, announce it.
entry := getExpiringAddrs()
*entry = expiringAddr{Addr: addr, Expiry: exp, TTL: ttl, Peer: p}
entry := &expiringAddr{Addr: addr, Expiry: exp, TTL: ttl, Peer: p}
mab.addrs.Insert(entry)
mab.subManager.BroadcastAddr(p, addr)
} else {
Expand Down Expand Up @@ -433,7 +415,6 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
if ttl > 0 {
if a.IsConnected() && !ttlIsConnected(ttl) && mab.addrs.NumUnconnectedAddrs() >= mab.maxUnconnectedAddrs {
mab.addrs.Delete(a)
putExpiringAddrs(a)
} else {
a.Addr = addr
a.Expiry = exp
Expand All @@ -443,15 +424,13 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
}
} else {
mab.addrs.Delete(a)
putExpiringAddrs(a)
}
} else {
if ttl > 0 {
if !ttlIsConnected(ttl) && mab.addrs.NumUnconnectedAddrs() >= mab.maxUnconnectedAddrs {
continue
}
entry := getExpiringAddrs()
*entry = expiringAddr{Addr: addr, Expiry: exp, TTL: ttl, Peer: p}
entry := &expiringAddr{Addr: addr, Expiry: exp, TTL: ttl, Peer: p}
mab.addrs.Insert(entry)
mab.subManager.BroadcastAddr(p, addr)
}
Expand All @@ -472,12 +451,10 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
if oldTTL == a.TTL {
if newTTL == 0 {
mab.addrs.Delete(a)
putExpiringAddrs(a)
} else {
// We are over limit, drop these addresses.
if ttlIsConnected(oldTTL) && !ttlIsConnected(newTTL) && mab.addrs.NumUnconnectedAddrs() >= mab.maxUnconnectedAddrs {
mab.addrs.Delete(a)
putExpiringAddrs(a)
} else {
a.TTL = newTTL
a.Expiry = exp
Expand Down Expand Up @@ -541,7 +518,6 @@ func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {
delete(mab.signedPeerRecords, p)
for _, a := range mab.addrs.Addrs[p] {
mab.addrs.Delete(a)
putExpiringAddrs(a)
}
}

Expand Down
17 changes: 13 additions & 4 deletions p2p/host/peerstore/test/benchmarks_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ func BenchmarkPeerstore(b *testing.B, factory PeerstoreFactory, variant string)
ps, cleanup := factory()
defer cleanup()
b.ResetTimer()
itersPerBM := 10
for i := 0; i < b.N; i++ {
pp := peers[i%N]
ps.AddAddrs(pp.ID, pp.Addr, pstore.RecentlyConnectedAddrTTL)
ps.Addrs(pp.ID)
ps.ClearAddrs(pp.ID)
for j := 0; j < itersPerBM; j++ {
pp := peers[(i+j)%N]
ps.AddAddrs(pp.ID, pp.Addr, pstore.RecentlyConnectedAddrTTL)
}
for j := 0; j < itersPerBM; j++ {
pp := peers[(i+j)%N]
ps.Addrs(pp.ID)
}
for j := 0; j < itersPerBM; j++ {
pp := peers[(i+j)%N]
ps.ClearAddrs(pp.ID)
}
}
})

Expand Down
Loading