Commit 68edc965 authored by Steven Allen's avatar Steven Allen

feat: reduce allocations

After profiling allocations on a bootstrap node, I've noticed that
peerInfoToPBPeer is a very heavy source of allocations. This change should
improve the situation significantly.
parent 6edcea2f
package dht_pb
import (
"encoding/json"
)
type byteString string
func (b byteString) Marshal() ([]byte, error) {
return []byte(b), nil
}
func (b *byteString) MarshalTo(data []byte) (int, error) {
return copy(data, *b), nil
}
func (b *byteString) Unmarshal(data []byte) error {
*b = byteString(data)
return nil
}
func (b *byteString) Size() int {
return len(*b)
}
func (b byteString) MarshalJSON() ([]byte, error) {
return json.Marshal([]byte(b))
}
func (b *byteString) UnmarshalJSON(data []byte) error {
var buf []byte
err := json.Unmarshal(data, &buf)
if err != nil {
return err
}
*b = byteString(buf)
return nil
}
func (b byteString) Equal(other byteString) bool {
return b == other
}
...@@ -5,6 +5,7 @@ package dht_pb ...@@ -5,6 +5,7 @@ package dht_pb
import ( import (
fmt "fmt" fmt "fmt"
_ "github.com/gogo/protobuf/gogoproto"
proto "github.com/gogo/protobuf/proto" proto "github.com/gogo/protobuf/proto"
pb "github.com/libp2p/go-libp2p-record/pb" pb "github.com/libp2p/go-libp2p-record/pb"
io "io" io "io"
...@@ -110,13 +111,13 @@ type Message struct { ...@@ -110,13 +111,13 @@ type Message struct {
Record *pb.Record `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"` Record *pb.Record `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"`
// Used to return peers closer to a key in a query // Used to return peers closer to a key in a query
// GET_VALUE, GET_PROVIDERS, FIND_NODE // GET_VALUE, GET_PROVIDERS, FIND_NODE
CloserPeers []*Message_Peer `protobuf:"bytes,8,rep,name=closerPeers,proto3" json:"closerPeers,omitempty"` CloserPeers []Message_Peer `protobuf:"bytes,8,rep,name=closerPeers,proto3" json:"closerPeers"`
// Used to return Providers // Used to return Providers
// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS // GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
ProviderPeers []*Message_Peer `protobuf:"bytes,9,rep,name=providerPeers,proto3" json:"providerPeers,omitempty"` ProviderPeers []Message_Peer `protobuf:"bytes,9,rep,name=providerPeers,proto3" json:"providerPeers"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"` XXX_sizecache int32 `json:"-"`
} }
func (m *Message) Reset() { *m = Message{} } func (m *Message) Reset() { *m = Message{} }
...@@ -180,14 +181,14 @@ func (m *Message) GetRecord() *pb.Record { ...@@ -180,14 +181,14 @@ func (m *Message) GetRecord() *pb.Record {
return nil return nil
} }
func (m *Message) GetCloserPeers() []*Message_Peer { func (m *Message) GetCloserPeers() []Message_Peer {
if m != nil { if m != nil {
return m.CloserPeers return m.CloserPeers
} }
return nil return nil
} }
func (m *Message) GetProviderPeers() []*Message_Peer { func (m *Message) GetProviderPeers() []Message_Peer {
if m != nil { if m != nil {
return m.ProviderPeers return m.ProviderPeers
} }
...@@ -196,7 +197,7 @@ func (m *Message) GetProviderPeers() []*Message_Peer { ...@@ -196,7 +197,7 @@ func (m *Message) GetProviderPeers() []*Message_Peer {
type Message_Peer struct { type Message_Peer struct {
// ID of a given peer. // ID of a given peer.
Id []byte `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Id byteString `protobuf:"bytes,1,opt,name=id,proto3,customtype=byteString" json:"id"`
// multiaddrs for a given peer // multiaddrs for a given peer
Addrs [][]byte `protobuf:"bytes,2,rep,name=addrs,proto3" json:"addrs,omitempty"` Addrs [][]byte `protobuf:"bytes,2,rep,name=addrs,proto3" json:"addrs,omitempty"`
// used to signal the sender's connection capabilities to the peer // used to signal the sender's connection capabilities to the peer
...@@ -239,13 +240,6 @@ func (m *Message_Peer) XXX_DiscardUnknown() { ...@@ -239,13 +240,6 @@ func (m *Message_Peer) XXX_DiscardUnknown() {
var xxx_messageInfo_Message_Peer proto.InternalMessageInfo var xxx_messageInfo_Message_Peer proto.InternalMessageInfo
func (m *Message_Peer) GetId() []byte {
if m != nil {
return m.Id
}
return nil
}
func (m *Message_Peer) GetAddrs() [][]byte { func (m *Message_Peer) GetAddrs() [][]byte {
if m != nil { if m != nil {
return m.Addrs return m.Addrs
...@@ -270,34 +264,37 @@ func init() { ...@@ -270,34 +264,37 @@ func init() {
func init() { proto.RegisterFile("dht.proto", fileDescriptor_616a434b24c97ff4) } func init() { proto.RegisterFile("dht.proto", fileDescriptor_616a434b24c97ff4) }
var fileDescriptor_616a434b24c97ff4 = []byte{ var fileDescriptor_616a434b24c97ff4 = []byte{
// 428 bytes of a gzipped FileDescriptorProto // 469 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x52, 0xc1, 0x6e, 0x9b, 0x40, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x92, 0xb1, 0x6f, 0x9b, 0x40,
0x10, 0xed, 0x02, 0x76, 0xe3, 0x01, 0x93, 0xcd, 0x28, 0x07, 0x94, 0x4a, 0x16, 0xf2, 0x89, 0x1e, 0x18, 0xc5, 0x73, 0x80, 0xdd, 0xf8, 0x03, 0x3b, 0xe4, 0x94, 0x01, 0xb9, 0x92, 0x83, 0x3c, 0xd1,
0x02, 0x12, 0x95, 0x7a, 0xe8, 0xa1, 0x92, 0x0b, 0x34, 0xb2, 0x94, 0x62, 0x6b, 0xeb, 0xa4, 0x47, 0xc1, 0x20, 0xd1, 0xb5, 0xaa, 0x6a, 0x03, 0x8d, 0x2c, 0xa5, 0xd8, 0xba, 0x38, 0xe9, 0x68, 0x19,
0xcb, 0xc0, 0xca, 0x41, 0xa5, 0x5e, 0x04, 0x24, 0x95, 0xbf, 0xb0, 0x3d, 0xf6, 0x13, 0x2a, 0x7f, 0xb8, 0x12, 0x54, 0xd7, 0x87, 0x00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0xf6, 0xeb, 0x84, 0x2a, 0x60, 0x0c,
0x3d, 0x67, 0xec, 0x37, 0x28, 0x55, 0x9c, 0x28, 0x8f, 0xfb, 0x01, 0xfa, 0xb9, 0x1f, 0xa0, 0xdf,
0xfb, 0x01, 0x0a, 0xda, 0xfc, 0xfd, 0xbd, 0xfd, 0x1b, 0x00, 0x00, 0xff, 0xff, 0x8d, 0x1a, 0xa1,
0xbe, 0xf7, 0x02, 0x00, 0x00,
} }
func (m *Message) Marshal() (dAtA []byte, err error) { func (m *Message) Marshal() (dAtA []byte, err error) {
...@@ -422,13 +419,16 @@ func (m *Message_Peer) MarshalToSizedBuffer(dAtA []byte) (int, error) { ...@@ -422,13 +419,16 @@ func (m *Message_Peer) MarshalToSizedBuffer(dAtA []byte) (int, error) {
dAtA[i] = 0x12 dAtA[i] = 0x12
} }
} }
if len(m.Id) > 0 { {
i -= len(m.Id) size := m.Id.Size()
copy(dAtA[i:], m.Id) i -= size
i = encodeVarintDht(dAtA, i, uint64(len(m.Id))) if _, err := m.Id.MarshalTo(dAtA[i:]); err != nil {
i-- return 0, err
dAtA[i] = 0xa }
i = encodeVarintDht(dAtA, i, uint64(size))
} }
i--
dAtA[i] = 0xa
return len(dAtA) - i, nil return len(dAtA) - i, nil
} }
...@@ -487,10 +487,8 @@ func (m *Message_Peer) Size() (n int) { ...@@ -487,10 +487,8 @@ func (m *Message_Peer) Size() (n int) {
} }
var l int var l int
_ = l _ = l
l = len(m.Id) l = m.Id.Size()
if l > 0 { n += 1 + l + sovDht(uint64(l))
n += 1 + l + sovDht(uint64(l))
}
if len(m.Addrs) > 0 { if len(m.Addrs) > 0 {
for _, b := range m.Addrs { for _, b := range m.Addrs {
l = len(b) l = len(b)
...@@ -659,7 +657,7 @@ func (m *Message) Unmarshal(dAtA []byte) error { ...@@ -659,7 +657,7 @@ func (m *Message) Unmarshal(dAtA []byte) error {
if postIndex > l { if postIndex > l {
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
m.CloserPeers = append(m.CloserPeers, &Message_Peer{}) m.CloserPeers = append(m.CloserPeers, Message_Peer{})
if err := m.CloserPeers[len(m.CloserPeers)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { if err := m.CloserPeers[len(m.CloserPeers)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err return err
} }
...@@ -693,7 +691,7 @@ func (m *Message) Unmarshal(dAtA []byte) error { ...@@ -693,7 +691,7 @@ func (m *Message) Unmarshal(dAtA []byte) error {
if postIndex > l { if postIndex > l {
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
m.ProviderPeers = append(m.ProviderPeers, &Message_Peer{}) m.ProviderPeers = append(m.ProviderPeers, Message_Peer{})
if err := m.ProviderPeers[len(m.ProviderPeers)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { if err := m.ProviderPeers[len(m.ProviderPeers)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err return err
} }
...@@ -800,9 +798,8 @@ func (m *Message_Peer) Unmarshal(dAtA []byte) error { ...@@ -800,9 +798,8 @@ func (m *Message_Peer) Unmarshal(dAtA []byte) error {
if postIndex > l { if postIndex > l {
return io.ErrUnexpectedEOF return io.ErrUnexpectedEOF
} }
m.Id = append(m.Id[:0], dAtA[iNdEx:postIndex]...) if err := m.Id.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
if m.Id == nil { return err
m.Id = []byte{}
} }
iNdEx = postIndex iNdEx = postIndex
case 2: case 2:
...@@ -938,9 +935,6 @@ func skipDht(dAtA []byte) (n int, err error) { ...@@ -938,9 +935,6 @@ func skipDht(dAtA []byte) (n int, err error) {
return 0, ErrInvalidLengthDht return 0, ErrInvalidLengthDht
} }
iNdEx += length iNdEx += length
if iNdEx < 0 {
return 0, ErrInvalidLengthDht
}
case 3: case 3:
depth++ depth++
case 4: case 4:
...@@ -953,6 +947,9 @@ func skipDht(dAtA []byte) (n int, err error) { ...@@ -953,6 +947,9 @@ func skipDht(dAtA []byte) (n int, err error) {
default: default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType) return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
} }
if iNdEx < 0 {
return 0, ErrInvalidLengthDht
}
if depth == 0 { if depth == 0 {
return iNdEx, nil return iNdEx, nil
} }
......
...@@ -9,6 +9,7 @@ syntax = "proto3"; ...@@ -9,6 +9,7 @@ syntax = "proto3";
package dht.pb; package dht.pb;
import "github.com/libp2p/go-libp2p-record/pb/record.proto"; import "github.com/libp2p/go-libp2p-record/pb/record.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
message Message { message Message {
enum MessageType { enum MessageType {
...@@ -37,7 +38,7 @@ message Message { ...@@ -37,7 +38,7 @@ message Message {
message Peer { message Peer {
// ID of a given peer. // ID of a given peer.
bytes id = 1; bytes id = 1 [(gogoproto.customtype) = "byteString", (gogoproto.nullable) = false];
// multiaddrs for a given peer // multiaddrs for a given peer
repeated bytes addrs = 2; repeated bytes addrs = 2;
...@@ -63,9 +64,9 @@ message Message { ...@@ -63,9 +64,9 @@ message Message {
// Used to return peers closer to a key in a query // Used to return peers closer to a key in a query
// GET_VALUE, GET_PROVIDERS, FIND_NODE // GET_VALUE, GET_PROVIDERS, FIND_NODE
repeated Peer closerPeers = 8; repeated Peer closerPeers = 8 [(gogoproto.nullable) = false];
// Used to return Providers // Used to return Providers
// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS // GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
repeated Peer providerPeers = 9; repeated Peer providerPeers = 9 [(gogoproto.nullable) = false];
} }
...@@ -25,43 +25,41 @@ func NewMessage(typ Message_MessageType, key []byte, level int) *Message { ...@@ -25,43 +25,41 @@ func NewMessage(typ Message_MessageType, key []byte, level int) *Message {
return m return m
} }
func peerRoutingInfoToPBPeer(p PeerRoutingInfo) *Message_Peer { func peerRoutingInfoToPBPeer(p PeerRoutingInfo) Message_Peer {
pbp := new(Message_Peer) var pbp Message_Peer
pbp.Addrs = make([][]byte, len(p.Addrs)) pbp.Addrs = make([][]byte, len(p.Addrs))
for i, maddr := range p.Addrs { for i, maddr := range p.Addrs {
pbp.Addrs[i] = maddr.Bytes() // Bytes, not String. Compressed. pbp.Addrs[i] = maddr.Bytes() // Bytes, not String. Compressed.
} }
s := string(p.ID) pbp.Id = byteString(p.ID)
pbp.Id = []byte(s) pbp.Connection = ConnectionType(p.Connectedness)
c := ConnectionType(p.Connectedness)
pbp.Connection = c
return pbp return pbp
} }
func peerInfoToPBPeer(p peer.AddrInfo) *Message_Peer { func peerInfoToPBPeer(p peer.AddrInfo) Message_Peer {
pbp := new(Message_Peer) var pbp Message_Peer
pbp.Addrs = make([][]byte, len(p.Addrs)) pbp.Addrs = make([][]byte, len(p.Addrs))
for i, maddr := range p.Addrs { for i, maddr := range p.Addrs {
pbp.Addrs[i] = maddr.Bytes() // Bytes, not String. Compressed. pbp.Addrs[i] = maddr.Bytes() // Bytes, not String. Compressed.
} }
pbp.Id = []byte(p.ID) pbp.Id = byteString(p.ID)
return pbp return pbp
} }
// PBPeerToPeer turns a *Message_Peer into its peer.AddrInfo counterpart // PBPeerToPeer turns a *Message_Peer into its peer.AddrInfo counterpart
func PBPeerToPeerInfo(pbp *Message_Peer) *peer.AddrInfo { func PBPeerToPeerInfo(pbp Message_Peer) peer.AddrInfo {
return &peer.AddrInfo{ return peer.AddrInfo{
ID: peer.ID(pbp.GetId()), ID: peer.ID(pbp.Id),
Addrs: pbp.Addresses(), Addrs: pbp.Addresses(),
} }
} }
// RawPeerInfosToPBPeers converts a slice of Peers into a slice of *Message_Peers, // RawPeerInfosToPBPeers converts a slice of Peers into a slice of *Message_Peers,
// ready to go out on the wire. // ready to go out on the wire.
func RawPeerInfosToPBPeers(peers []peer.AddrInfo) []*Message_Peer { func RawPeerInfosToPBPeers(peers []peer.AddrInfo) []Message_Peer {
pbpeers := make([]*Message_Peer, len(peers)) pbpeers := make([]Message_Peer, len(peers))
for i, p := range peers { for i, p := range peers {
pbpeers[i] = peerInfoToPBPeer(p) pbpeers[i] = peerInfoToPBPeer(p)
} }
...@@ -72,7 +70,7 @@ func RawPeerInfosToPBPeers(peers []peer.AddrInfo) []*Message_Peer { ...@@ -72,7 +70,7 @@ func RawPeerInfosToPBPeers(peers []peer.AddrInfo) []*Message_Peer {
// which can be written to a message and sent out. the key thing this function // which can be written to a message and sent out. the key thing this function
// does (in addition to PeersToPBPeers) is set the ConnectionType with // does (in addition to PeersToPBPeers) is set the ConnectionType with
// information from the given network.Network. // information from the given network.Network.
func PeerInfosToPBPeers(n network.Network, peers []peer.AddrInfo) []*Message_Peer { func PeerInfosToPBPeers(n network.Network, peers []peer.AddrInfo) []Message_Peer {
pbps := RawPeerInfosToPBPeers(peers) pbps := RawPeerInfosToPBPeers(peers)
for i, pbp := range pbps { for i, pbp := range pbps {
c := ConnectionType(n.Connectedness(peers[i].ID)) c := ConnectionType(n.Connectedness(peers[i].ID))
...@@ -81,8 +79,8 @@ func PeerInfosToPBPeers(n network.Network, peers []peer.AddrInfo) []*Message_Pee ...@@ -81,8 +79,8 @@ func PeerInfosToPBPeers(n network.Network, peers []peer.AddrInfo) []*Message_Pee
return pbps return pbps
} }
func PeerRoutingInfosToPBPeers(peers []PeerRoutingInfo) []*Message_Peer { func PeerRoutingInfosToPBPeers(peers []PeerRoutingInfo) []Message_Peer {
pbpeers := make([]*Message_Peer, len(peers)) pbpeers := make([]Message_Peer, len(peers))
for i, p := range peers { for i, p := range peers {
pbpeers[i] = peerRoutingInfoToPBPeer(p) pbpeers[i] = peerRoutingInfoToPBPeer(p)
} }
...@@ -91,10 +89,11 @@ func PeerRoutingInfosToPBPeers(peers []PeerRoutingInfo) []*Message_Peer { ...@@ -91,10 +89,11 @@ func PeerRoutingInfosToPBPeers(peers []PeerRoutingInfo) []*Message_Peer {
// PBPeersToPeerInfos converts given []*Message_Peer into []peer.AddrInfo // PBPeersToPeerInfos converts given []*Message_Peer into []peer.AddrInfo
// Invalid addresses will be silently omitted. // Invalid addresses will be silently omitted.
func PBPeersToPeerInfos(pbps []*Message_Peer) []*peer.AddrInfo { func PBPeersToPeerInfos(pbps []Message_Peer) []*peer.AddrInfo {
peers := make([]*peer.AddrInfo, 0, len(pbps)) peers := make([]*peer.AddrInfo, 0, len(pbps))
for _, pbp := range pbps { for _, pbp := range pbps {
peers = append(peers, PBPeerToPeerInfo(pbp)) ai := PBPeerToPeerInfo(pbp)
peers = append(peers, &ai)
} }
return peers return peers
} }
...@@ -109,7 +108,7 @@ func (m *Message_Peer) Addresses() []ma.Multiaddr { ...@@ -109,7 +108,7 @@ func (m *Message_Peer) Addresses() []ma.Multiaddr {
for _, addr := range m.Addrs { for _, addr := range m.Addrs {
maddr, err := ma.NewMultiaddrBytes(addr) maddr, err := ma.NewMultiaddrBytes(addr)
if err != nil { if err != nil {
log.Debugw("error decoding multiaddr for peer", "peer", m.GetId(), "error", err) log.Debugw("error decoding multiaddr for peer", "peer", peer.ID(m.Id), "error", err)
continue continue
} }
......
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