From 21dc42bd72fc6064c27a098db680e94ced5a76d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 6 Nov 2022 13:48:40 +0100 Subject: [PATCH] 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 --- core/peer/addrinfo.go | 2 +- core/peer/record.go | 4 ++-- p2p/discovery/routing/routing_test.go | 2 +- p2p/discovery/util/util.go | 2 +- p2p/net/mock/mock_peernet.go | 2 +- p2p/protocol/circuitv2/util/pbconv.go | 16 +++------------- 6 files changed, 9 insertions(+), 19 deletions(-) diff --git a/core/peer/addrinfo.go b/core/peer/addrinfo.go index 19b07a4b7..b479df9e0 100644 --- a/core/peer/addrinfo.go +++ b/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)) } diff --git a/core/peer/record.go b/core/peer/record.go index 9f8861af8..b502c8dd1 100644 --- a/core/peer/record.go +++ b/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()}) } diff --git a/p2p/discovery/routing/routing_test.go b/p2p/discovery/routing/routing_test.go index ed8ab25d1..6e28c06f0 100644 --- a/p2p/discovery/routing/routing_test.go +++ b/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) } diff --git a/p2p/discovery/util/util.go b/p2p/discovery/util/util.go index 741752615..b21a676a7 100644 --- a/p2p/discovery/util/util.go +++ b/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) } diff --git a/p2p/net/mock/mock_peernet.go b/p2p/net/mock/mock_peernet.go index 3fb070132..85e636788 100644 --- a/p2p/net/mock/mock_peernet.go +++ b/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) } diff --git a/p2p/protocol/circuitv2/util/pbconv.go b/p2p/protocol/circuitv2/util/pbconv.go index ae1b9b50b..4a884351e 100644 --- a/p2p/protocol/circuitv2/util/pbconv.go +++ b/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())