Skip to content

Commit

Permalink
use a map in sameAddrs to avoid quadratic behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Apr 19, 2019
1 parent 5503cd7 commit b17ba31
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,21 @@ func sameAddrs(a, b []ma.Multiaddr) bool {
return false
}

// this is O(n*m), might be worth using a map to turn into O(n+m)
bmap := make(map[string]struct{})
for _, addr := range b {
bmap[string(addr.Bytes())] = struct{}{}
}

for _, addr := range a {
if !findAddr(addr, b) {
_, ok := bmap[string(addr.Bytes())]
if !ok {
return false
}
}

return true
}

func findAddr(addr ma.Multiaddr, addrs []ma.Multiaddr) bool {
for _, xaddr := range addrs {
if addr.Equal(xaddr) {
return true
}
}
return false
}

// ID returns the (local) peer.ID associated with this Host
func (h *BasicHost) ID() peer.ID {
return h.Network().LocalPeer()
Expand Down

0 comments on commit b17ba31

Please sign in to comment.