Skip to content

Commit

Permalink
Preallocate maps and remove extra cleanup line.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arceliar committed Jan 17, 2018
1 parent 7f335f0 commit 45bb9eb
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions p2p/host/peerstore/addr_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ func (mgr *AddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
// so zero value can be used
mgr.init()

amap := make(map[string]expiringAddr)
for _, ea := range mgr.addrs[p] {
oldAddrs := mgr.addrs[p]
amap := make(map[string]expiringAddr, len(oldAddrs))
for _, ea := range oldAddrs {
amap[string(ea.Addr.Bytes())] = ea
}

Expand Down Expand Up @@ -154,8 +155,9 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
// so zero value can be used
mgr.init()

amap := make(map[string]expiringAddr)
for _, ea := range mgr.addrs[p] {
oldAddrs := mgr.addrs[p]
amap := make(map[string]expiringAddr, len(oldAddrs))
for _, ea := range oldAddrs {
amap[string(ea.Addr.Bytes())] = ea
}

Expand Down Expand Up @@ -238,7 +240,6 @@ func (mgr *AddrManager) Addrs(p peer.ID) []ma.Multiaddr {
}

// clean up the expired ones.
mgr.addrs[p] = cleaned
if len(cleaned) == 0 {
delete(mgr.addrs, p)
} else {
Expand Down

0 comments on commit 45bb9eb

Please sign in to comment.