-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
swarm: Dedup addresses to dial #2322
Conversation
Co-authored-by: Aayush Rajasekaran <[email protected]>
|
||
// DedupAddrs deduplicates addresses in place, leave only unique addresses. | ||
// It doesn't allocate. | ||
func DedupAddrs(addrs []ma.Multiaddr) []ma.Multiaddr { | ||
if len(addrs) == 0 { | ||
return addrs | ||
} | ||
sort.Slice(addrs, func(i, j int) bool { return bytes.Compare(addrs[i].Bytes(), addrs[j].Bytes()) < 0 }) |
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.
Apologies for the drive by comment. Feel free to ignore.
Sort.Slice will allocate unfortunately :(
golang/go#17332
func BenchmarkDedupAddrs(b *testing.B) {
b.ReportAllocs()
tcpAddr := ma.StringCast("/ip4/127.0.0.1/tcp/1234")
quicAddr := ma.StringCast("/ip4/127.0.0.1/udp/1234/quic-v1")
wsAddr := ma.StringCast("/ip4/127.0.0.1/tcp/1234/ws")
addrs := []ma.Multiaddr{tcpAddr, quicAddr, tcpAddr, quicAddr, wsAddr}
for i := 0; i < b.N; i++ {
addrs = DedupAddrs(addrs)
}
}
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.
Unfortunate. Given this is pre-existing code, I'll leave it. But I'll open an issue
* Dedup addresses to dial Co-authored-by: Aayush Rajasekaran <[email protected]> * Move DedupAddrs test * Typo --------- Co-authored-by: Aayush Rajasekaran <[email protected]>
* swarm: Dedup addresses to dial (#2322) * Dedup addresses to dial Co-authored-by: Aayush Rajasekaran <[email protected]> * Move DedupAddrs test * Typo --------- Co-authored-by: Aayush Rajasekaran <[email protected]> * Release version v0.27.5 --------- Co-authored-by: Aayush Rajasekaran <[email protected]>
I moved the DedupAddrs function to
core/network
since that seemed like the most relevant place for it to be so that basic_host and swarm can use it. Really this should probably be part of the multiaddr package. Although I would say we make that change later.This change makes
swarm.addrsForDial
return only unique addresses. This is assumed by they dialWorker. I also added some existence checks to avoid any potential panics in case the key was missing from the pending map.Most of this diff is a test or moving code around. The real change is the one line in swarm_dial.go.