Commit 62a88e01 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

faster encapsulation + join

parent 9d041321
...@@ -84,10 +84,10 @@ func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr { ...@@ -84,10 +84,10 @@ func (m *multiaddr) Encapsulate(o Multiaddr) Multiaddr {
mb := m.bytes mb := m.bytes
ob := o.Bytes() ob := o.Bytes()
var b bytes.Buffer b := make([]byte, len(mb)+len(ob))
b.Write(mb) copy(b, mb)
b.Write(ob) copy(b[len(mb):], ob)
return &multiaddr{bytes: b.Bytes()} return &multiaddr{bytes: b}
} }
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found. // Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
......
package multiaddr package multiaddr
import ( import "fmt"
"bytes"
"fmt"
)
// Split returns the sub-address portions of a multiaddr. // Split returns the sub-address portions of a multiaddr.
func Split(m Multiaddr) []Multiaddr { func Split(m Multiaddr) []Multiaddr {
...@@ -21,11 +18,23 @@ func Split(m Multiaddr) []Multiaddr { ...@@ -21,11 +18,23 @@ func Split(m Multiaddr) []Multiaddr {
// Join returns a combination of addresses. // Join returns a combination of addresses.
func Join(ms ...Multiaddr) Multiaddr { func Join(ms ...Multiaddr) Multiaddr {
var b bytes.Buffer
for _, m := range ms { length := 0
b.Write(m.Bytes()) bs := make([][]byte, len(ms))
for i, m := range ms {
bs[i] = m.Bytes()
length += len(bs[i])
}
bidx := 0
b := make([]byte, length)
for _, mb := range bs {
for i := range mb {
b[bidx] = mb[i]
bidx++
} }
return &multiaddr{bytes: b.Bytes()} }
return &multiaddr{bytes: b}
} }
// Cast re-casts a byte slice as a multiaddr. will panic if it fails to parse. // Cast re-casts a byte slice as a multiaddr. will panic if it fails to parse.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment