This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Memory usage #22
Merged
Merged
Memory usage #22
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
|
@@ -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) | ||
for _, ea := range mgr.addrs[p] { | ||
amap[string(ea.Addr.Bytes())] = ea | ||
} | ||
|
||
subs := mgr.addrSubs[p] | ||
|
@@ -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) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
@@ -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) | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preallocate.