Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions core/protocol/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ const (
// ConvertFromStrings is a convenience function that takes a slice of strings and
// converts it to a slice of protocol.ID.
func ConvertFromStrings(ids []string) (res []ID) {
res = make([]ID, 0, len(ids))
for _, id := range ids {
res = append(res, ID(id))
res = make([]ID, len(ids))
for i, id := range ids {
res[i] = ID(id)
}
return res
}

// ConvertToStrings is a convenience function that takes a slice of protocol.ID and
// converts it to a slice of strings.
func ConvertToStrings(ids []ID) (res []string) {
res = make([]string, 0, len(ids))
for _, id := range ids {
res = append(res, string(id))
res = make([]string, len(ids))
for i, id := range ids {
res[i] = string(id)
}
return res
}
2 changes: 1 addition & 1 deletion p2p/host/autonat/dialpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *dialPolicy) skipDial(addr ma.Multiaddr) bool {
// public addresses in the list.
func (d *dialPolicy) skipPeer(addrs []ma.Multiaddr) bool {
localAddrs := d.host.Addrs()
localHosts := make([]net.IP, 0)
localHosts := make([]net.IP, 0, len(localAddrs))
for _, lAddr := range localAddrs {
if _, err := lAddr.ValueForProtocol(ma.P_CIRCUIT); err != nil && manet.IsPublicAddr(lAddr) {
lIP, err := manet.ToIP(lAddr)
Expand Down
3 changes: 2 additions & 1 deletion p2p/host/autorelay/addrsplosion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
// best address for the relay. Instead we should rely on the addresses provided by the
// relay in response to the reservation request.
func cleanupAddressSet(addrs []ma.Multiaddr) []ma.Multiaddr {
var public, private []ma.Multiaddr
public := make([]ma.Multiaddr, 0, len(addrs))
private := make([]ma.Multiaddr, 0, len(addrs))

for _, a := range addrs {
if isRelayAddr(a) {
Expand Down
6 changes: 6 additions & 0 deletions p2p/host/basic/addrs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,12 @@ func areAddrsDifferent(prev, current []ma.Multiaddr) bool {
// diffAddrs diffs prev and current addrs and returns added, maintained, and removed addrs.
// Both prev and current are expected to be sorted using ma.Compare()
func diffAddrs(prev, current []ma.Multiaddr) (added, maintained, removed []ma.Multiaddr) {
added = make([]ma.Multiaddr, 0, len(current))
removed = make([]ma.Multiaddr, 0, len(prev))

minLen := min(len(current), len(prev))
maintained = make([]ma.Multiaddr, 0, minLen)

i, j := 0, 0
for i < len(prev) && j < len(current) {
cmp := prev[i].Compare(current[j])
Expand Down
3 changes: 2 additions & 1 deletion p2p/net/swarm/dial_ranker.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NoDelayDialRanker(addrs []ma.Multiaddr) []network.AddrDelay {
//
// We dial lowest ports first as they are more likely to be the listen port.
func DefaultDialRanker(addrs []ma.Multiaddr) []network.AddrDelay {
totalAddrs := len(addrs)
relay, addrs := filterAddrs(addrs, isRelayAddr)
pvt, addrs := filterAddrs(addrs, manet.IsPrivateAddr)
public, addrs := filterAddrs(addrs, func(a ma.Multiaddr) bool { return isProtocolAddr(a, ma.P_IP4) || isProtocolAddr(a, ma.P_IP6) })
Expand All @@ -89,7 +90,7 @@ func DefaultDialRanker(addrs []ma.Multiaddr) []network.AddrDelay {
relayOffset = RelayDelay
}

res := make([]network.AddrDelay, 0, len(addrs))
res := make([]network.AddrDelay, 0, totalAddrs)
res = append(res, getAddrDelay(pvt, PrivateTCPDelay, PrivateQUICDelay, PrivateOtherDelay, 0)...)
res = append(res, getAddrDelay(public, PublicTCPDelay, PublicQUICDelay, PublicOtherDelay, 0)...)
res = append(res, getAddrDelay(relay, PublicTCPDelay, PublicQUICDelay, PublicOtherDelay, relayOffset)...)
Expand Down
5 changes: 2 additions & 3 deletions p2p/net/swarm/dial_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,8 @@ loop:
// If they have errored, record the error in pr. If they have succeeded,
// respond with the connection.
// If they are pending, add them to tojoin.
// If we haven't seen any of the addresses before, add them to todial.
var todial []ma.Multiaddr
var tojoin []*addrDial
todial := make([]ma.Multiaddr, 0, len(addrRanking))
tojoin := make([]*addrDial, 0, len(addrRanking))

for _, adelay := range addrRanking {
ad, ok := w.trackedDials[string(adelay.Addr.Bytes())]
Expand Down
4 changes: 3 additions & 1 deletion p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@ func (r ResolverFromMaDNS) ResolveDNSAddr(ctx context.Context, expectedPeerID pe
if recursionLimit <= 0 {
return []ma.Multiaddr{maddr}, nil
}
var resolved, toResolve []ma.Multiaddr
addrs, err := r.Resolve(ctx, maddr)
if err != nil {
return nil, err
Expand All @@ -905,6 +904,9 @@ func (r ResolverFromMaDNS) ResolveDNSAddr(ctx context.Context, expectedPeerID pe
addrs = addrs[:outputLimit]
}

resolved := make([]ma.Multiaddr, 0, len(addrs))
toResolve := make([]ma.Multiaddr, 0, len(addrs))

for _, addr := range addrs {
if startsWithDNSADDR(addr) {
toResolve = append(toResolve, addr)
Expand Down
6 changes: 3 additions & 3 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ type resolveErr struct {

func chainResolvers(ctx context.Context, addrs []ma.Multiaddr, outputLimit int, resolvers []resolver) ([]ma.Multiaddr, []resolveErr) {
nextAddrs := make([]ma.Multiaddr, 0, len(addrs))
errs := make([]resolveErr, 0)
errs := make([]resolveErr, 0, len(addrs))
for _, r := range resolvers {
for _, a := range addrs {
if !r.canResolve(a) {
Expand Down Expand Up @@ -388,7 +388,7 @@ func (s *Swarm) resolveAddrs(ctx context.Context, pi peer.AddrInfo) []ma.Multiad
},
}

var skipped []ma.Multiaddr
skipped := make([]ma.Multiaddr, 0, len(pi.Addrs))
skipResolver := resolver{
canResolve: func(addr ma.Multiaddr) bool {
tpt := s.TransportForDialing(addr)
Expand Down Expand Up @@ -486,7 +486,7 @@ var quicDraft29DialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_UDP), mafmt.Bas
// know are going to fail or for which we have a better alternative.
func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) (goodAddrs []ma.Multiaddr, addrErrs []TransportError) {
lisAddrs, _ := s.InterfaceListenAddresses()
var ourAddrs []ma.Multiaddr
ourAddrs := make([]ma.Multiaddr, 0, len(lisAddrs))
for _, addr := range lisAddrs {
// we're only sure about filtering out /ip4 and /ip6 addresses, so far
ma.ForEach(addr, func(c ma.Component) bool {
Expand Down
3 changes: 3 additions & 0 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ func (ids *idService) getSignedRecord(snapshot *identifySnapshot) []byte {

// diff takes two slices of strings (a and b) and computes which elements were added and removed in b
func diff(a, b []protocol.ID) (added, removed []protocol.ID) {
added = make([]protocol.ID, 0, len(b))
removed = make([]protocol.ID, 0, len(a))

// This is O(n^2), but it's fine because the slices are small.
for _, x := range b {
var found bool
Expand Down
Loading