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 {
mb := m.bytes
ob := o.Bytes()
var b bytes.Buffer
b.Write(mb)
b.Write(ob)
return &multiaddr{bytes: b.Bytes()}
b := make([]byte, len(mb)+len(ob))
copy(b, mb)
copy(b[len(mb):], ob)
return &multiaddr{bytes: b}
}
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
......
package multiaddr
import (
"bytes"
"fmt"
)
import "fmt"
// Split returns the sub-address portions of a multiaddr.
func Split(m Multiaddr) []Multiaddr {
......@@ -21,11 +18,23 @@ func Split(m Multiaddr) []Multiaddr {
// Join returns a combination of addresses.
func Join(ms ...Multiaddr) Multiaddr {
var b bytes.Buffer
for _, m := range ms {
b.Write(m.Bytes())
length := 0
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.
......
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