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

swarm: Dedup addresses to dial #2322

Merged
merged 3 commits into from Jun 2, 2023
Merged

swarm: Dedup addresses to dial #2322

merged 3 commits into from Jun 2, 2023

Conversation

MarcoPolo
Copy link
Contributor

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.

MarcoPolo and others added 2 commits June 2, 2023 12:01
Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>
@MarcoPolo MarcoPolo changed the title Dedup addresses to dial swarm: Dedup addresses to dial Jun 2, 2023
@MarcoPolo MarcoPolo enabled auto-merge (squash) June 2, 2023 19:33
@MarcoPolo MarcoPolo disabled auto-merge June 2, 2023 19:33
Comment on lines +189 to +196

// 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 })
Copy link
Member

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)
	}
}

Copy link
Contributor Author

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

@MarcoPolo MarcoPolo merged commit e89814c into master Jun 2, 2023
19 checks passed
MarcoPolo added a commit that referenced this pull request Jun 2, 2023
* Dedup addresses to dial

Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>

* Move DedupAddrs test

* Typo

---------

Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>
MarcoPolo added a commit that referenced this pull request Jun 2, 2023
* swarm: Dedup addresses to dial (#2322)

* Dedup addresses to dial

Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>

* Move DedupAddrs test

* Typo

---------

Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>

* Release version v0.27.5

---------

Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants