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

core: fix allocation in network.DedupAddrs #2352

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
24 changes: 11 additions & 13 deletions core/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
package network

import (
"bytes"
"context"
"io"
"sort"
"time"

"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -197,22 +195,22 @@ type AddrDelay struct {
// DialRanker provides a schedule of dialing the provided addresses
type DialRanker func([]ma.Multiaddr) []AddrDelay

// DedupAddrs deduplicates addresses in place, leave only unique addresses.
// DedupAddrs deduplicates addresses in place, leaving 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 })
idx := 1
for i := 1; i < len(addrs); i++ {
if !addrs[i-1].Equal(addrs[i]) {
var idx int
for i := 0; i < len(addrs); i++ {
found := false
for j := 0; j < idx; j++ {
if addrs[i].Equal(addrs[j]) {
found = true
break
}
}
if !found {
addrs[idx] = addrs[i]
idx++
}
}
for i := idx; i < len(addrs); i++ {
addrs[i] = nil
}
return addrs[:idx]
}