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

reduce allocations in net.ResolveUnspecifiedAddress #200

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 11 additions & 11 deletions net/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import (
)

// ResolveUnspecifiedAddress expands an unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces. If ifaceAddr is nil, we request interface addresses
// from the network stack. (this is so you can provide a cached value if resolving many addrs)
// use the known local interfaces
func ResolveUnspecifiedAddress(resolve ma.Multiaddr, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
// split address into its components
split := ma.Split(resolve)

// if first component (ip) is not unspecified, use it as is.
if !IsIPUnspecified(split[0]) {
first, rest := ma.SplitFirst(resolve)
if first == nil || !IsIPUnspecified(first) {
return []ma.Multiaddr{resolve}, nil
}

out := make([]ma.Multiaddr, 0, len(ifaceAddrs))
for _, ia := range ifaceAddrs {
// must match the first protocol to be resolve.
if ia.Protocols()[0].Code != resolve.Protocols()[0].Code {
// must match the first protocol to resolve.
match := false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be solved by #198.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. It's still inefficient and it's very difficult to optimise this beyond the method in the PR without doing #198.

ma.ForEach(ia, func(c ma.Component) bool {
match = c.Protocol().Code == first.Protocol().Code
return false
})
if !match {
continue
}

split[0] = ia
joined := ma.Join(split...)
joined := ma.Join(ia, rest)
out = append(out, joined)
}
if len(out) < 1 {
Expand Down
15 changes: 15 additions & 0 deletions net/resolve_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manet

import (
"math/rand"
"testing"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -55,3 +56,17 @@ func TestResolvingAddrs(t *testing.T) {
t.Fatal("should have failed")
}
}

func BenchmarkResolveAddr(b *testing.B) {
b.ReportAllocs()
unspec := []ma.Multiaddr{
ma.StringCast("/ip4/0.0.0.0/tcp/1234"),
}

iface := []ma.Multiaddr{
ma.StringCast("/ip4/127.0.0.1"),
}
for i := 0; i < b.N; i++ {
ResolveUnspecifiedAddress(unspec[rand.Intn(len(unspec))], iface)
}
}
10 changes: 4 additions & 6 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ func Join(ms ...Multiaddr) Multiaddr {
}

length := 0
bs := make([][]byte, len(ms))
for i, m := range ms {
bs[i] = m.Bytes()
length += len(bs[i])
for _, m := range ms {
length += len(m.Bytes())
}

bidx := 0
b := make([]byte, length)
for _, mb := range bs {
bidx += copy(b[bidx:], mb)
for _, m := range ms {
bidx += copy(b[bidx:], m.Bytes())
}
return &multiaddr{bytes: b}
}
Expand Down