Browse Source

chore: preallocate slices (#1842)

* Preallocate slices

* Move slice allocs to right before they're used

* Revert slice assignments to append

* Don't preallocate for tests or deprecated code

* Don't preallocate too much
pull/1857/head
Håvard Anda Estensen 2 years ago
committed by GitHub
parent
commit
21dc42bd72
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      core/peer/addrinfo.go
  2. 4
      core/peer/record.go
  3. 2
      p2p/discovery/routing/routing_test.go
  4. 2
      p2p/discovery/util/util.go
  5. 2
      p2p/net/mock/mock_peernet.go
  6. 16
      p2p/protocol/circuitv2/util/pbconv.go

2
core/peer/addrinfo.go

@ -86,7 +86,6 @@ func AddrInfoFromP2pAddr(m ma.Multiaddr) (*AddrInfo, error) {
// AddrInfoToP2pAddrs converts an AddrInfo to a list of Multiaddrs.
func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
var addrs []ma.Multiaddr
p2ppart, err := ma.NewComponent("p2p", Encode(pi.ID))
if err != nil {
return nil, err
@ -94,6 +93,7 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
if len(pi.Addrs) == 0 {
return []ma.Multiaddr{p2ppart}, nil
}
addrs := make([]ma.Multiaddr, 0, len(pi.Addrs))
for _, addr := range pi.Addrs {
addrs = append(addrs, addr.Encapsulate(p2ppart))
}

4
core/peer/record.go

@ -231,7 +231,7 @@ func (r *PeerRecord) ToProtobuf() (*pb.PeerRecord, error) {
}
func addrsFromProtobuf(addrs []*pb.PeerRecord_AddressInfo) []ma.Multiaddr {
var out []ma.Multiaddr
out := make([]ma.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
a, err := ma.NewMultiaddrBytes(addr.Multiaddr)
if err != nil {
@ -243,7 +243,7 @@ func addrsFromProtobuf(addrs []*pb.PeerRecord_AddressInfo) []ma.Multiaddr {
}
func addrsToProtobuf(addrs []ma.Multiaddr) []*pb.PeerRecord_AddressInfo {
var out []*pb.PeerRecord_AddressInfo
out := make([]*pb.PeerRecord_AddressInfo, 0, len(addrs))
for _, addr := range addrs {
out = append(out, &pb.PeerRecord_AddressInfo{Multiaddr: addr.Bytes()})
}

2
p2p/discovery/routing/routing_test.go

@ -135,7 +135,7 @@ func TestDiscoveryRouting(t *testing.T) {
pch := r2.FindProvidersAsync(ctx, c, 20)
var allAIs []peer.AddrInfo
allAIs := make([]peer.AddrInfo, 0, len(pch))
for ai := range pch {
allAIs = append(allAIs, ai)
}

2
p2p/discovery/util/util.go

@ -14,13 +14,13 @@ var log = logging.Logger("discovery-util")
// FindPeers is a utility function that synchronously collects peers from a Discoverer.
func FindPeers(ctx context.Context, d discovery.Discoverer, ns string, opts ...discovery.Option) ([]peer.AddrInfo, error) {
var res []peer.AddrInfo
ch, err := d.FindPeers(ctx, ns, opts...)
if err != nil {
return nil, err
}
res := make([]peer.AddrInfo, 0, len(ch))
for pi := range ch {
res = append(res, pi)
}

2
p2p/net/mock/mock_peernet.go

@ -255,7 +255,7 @@ func (pn *peernet) ConnsToPeer(p peer.ID) []network.Conn {
return nil
}
var cs2 []network.Conn
cs2 := make([]network.Conn, 0, len(cs))
for c := range cs {
cs2 = append(cs2, c)
}

16
p2p/protocol/circuitv2/util/pbconv.go

@ -36,10 +36,7 @@ func PeerToPeerInfoV1(p *pbv1.CircuitRelay_Peer) (peer.AddrInfo, error) {
}
func PeerInfoToPeerV1(pi peer.AddrInfo) *pbv1.CircuitRelay_Peer {
var addrs [][]byte
if len(pi.Addrs) > 0 {
addrs = make([][]byte, 0, len(pi.Addrs))
}
addrs := make([][]byte, 0, len(pi.Addrs))
for _, addr := range pi.Addrs {
addrs = append(addrs, addr.Bytes())
@ -62,10 +59,7 @@ func PeerToPeerInfoV2(p *pbv2.Peer) (peer.AddrInfo, error) {
return peer.AddrInfo{}, err
}
var addrs []ma.Multiaddr
if len(p.Addrs) > 0 {
addrs = make([]ma.Multiaddr, 0, len(p.Addrs))
}
addrs := make([]ma.Multiaddr, 0, len(p.Addrs))
for _, addrBytes := range p.Addrs {
a, err := ma.NewMultiaddrBytes(addrBytes)
@ -78,11 +72,7 @@ func PeerToPeerInfoV2(p *pbv2.Peer) (peer.AddrInfo, error) {
}
func PeerInfoToPeerV2(pi peer.AddrInfo) *pbv2.Peer {
var addrs [][]byte
if len(pi.Addrs) > 0 {
addrs = make([][]byte, 0, len(pi.Addrs))
}
addrs := make([][]byte, 0, len(pi.Addrs))
for _, addr := range pi.Addrs {
addrs = append(addrs, addr.Bytes())

Loading…
Cancel
Save