Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Memory usage #22

Merged
merged 2 commits into from
Jan 17, 2018
Merged
Changes from 1 commit
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
53 changes: 30 additions & 23 deletions addr_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (e *expiringAddr) ExpiredBy(t time.Time) bool {
return t.After(e.Expires)
}

type addrSet map[string]expiringAddr
type addrSlice []expiringAddr

// AddrManager manages addresses.
// The zero-value is ready to be used.
type AddrManager struct {
addrmu sync.Mutex // guards addrs
addrs map[peer.ID]addrSet
addrs map[peer.ID]addrSlice

addrSubs map[peer.ID][]*addrSub
}
Expand All @@ -67,7 +67,7 @@ type AddrManager struct {
// So we can use the zero value.
func (mgr *AddrManager) init() {
if mgr.addrs == nil {
mgr.addrs = make(map[peer.ID]addrSet)
mgr.addrs = make(map[peer.ID]addrSlice)
}
if mgr.addrSubs == nil {
mgr.addrSubs = make(map[peer.ID][]*addrSub)
Expand Down Expand Up @@ -108,10 +108,9 @@ func (mgr *AddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
// so zero value can be used
mgr.init()

amap, found := mgr.addrs[p]
if !found {
amap = make(addrSet)
mgr.addrs[p] = amap
amap := make(map[string]expiringAddr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preallocate.

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

subs := mgr.addrSubs[p]
Expand All @@ -134,6 +133,11 @@ func (mgr *AddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
}
}
}
newAddrs := make([]expiringAddr, 0, len(amap))
for _, ea := range amap {
newAddrs = append(newAddrs, ea)
}
mgr.addrs[p] = newAddrs
}

// SetAddr calls mgr.SetAddrs(p, addr, ttl)
Expand All @@ -150,10 +154,9 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
// so zero value can be used
mgr.init()

amap, found := mgr.addrs[p]
if !found {
amap = make(addrSet)
mgr.addrs[p] = amap
amap := make(map[string]expiringAddr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably preallocate.

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

subs := mgr.addrSubs[p]
Expand All @@ -177,6 +180,11 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
delete(amap, addrs)
}
}
newAddrs := make([]expiringAddr, 0, len(amap))
for _, ea := range amap {
newAddrs = append(newAddrs, ea)
}
mgr.addrs[p] = newAddrs
}

// UpdateAddrs updates the addresses associated with the given peer that have
Expand Down Expand Up @@ -221,21 +229,20 @@ func (mgr *AddrManager) Addrs(p peer.ID) []ma.Multiaddr {

now := time.Now()
good := make([]ma.Multiaddr, 0, len(maddrs))
var expired []string
for s, m := range maddrs {
if m.ExpiredBy(now) {
expired = append(expired, s)
} else {
cleaned := make([]expiringAddr, 0, len(maddrs))
for _, m := range maddrs {
if !m.ExpiredBy(now) {
cleaned = append(cleaned, m)
good = append(good, m.Addr)
}
}

// clean up the expired ones.
for _, s := range expired {
delete(maddrs, s)
}
if len(maddrs) == 0 {
mgr.addrs[p] = cleaned
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line probably shouldn't be here.

if len(cleaned) == 0 {
delete(mgr.addrs, p)
} else {
mgr.addrs[p] = cleaned
}
return good
}
Expand Down Expand Up @@ -295,9 +302,9 @@ func (mgr *AddrManager) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Mul

mgr.addrSubs[p] = append(mgr.addrSubs[p], sub)

baseaddrset := mgr.addrs[p]
initial := make([]ma.Multiaddr, 0, len(baseaddrset))
for _, a := range baseaddrset {
baseaddrslice := mgr.addrs[p]
initial := make([]ma.Multiaddr, 0, len(baseaddrslice))
for _, a := range baseaddrslice {
initial = append(initial, a.Addr)
}

Expand Down