From 367613be2e4cc7406af1d10c94c77906c0f0f741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 30 Oct 2022 12:50:35 +0100 Subject: [PATCH 1/5] Preallocate slices --- core/peer/addrinfo.go | 6 +++--- core/peer/record.go | 12 ++++++------ p2p/discovery/routing/routing_test.go | 2 +- p2p/discovery/util/util.go | 2 +- p2p/net/connmgr/connmgr_test.go | 12 ++++++------ p2p/net/mock/mock_peernet.go | 2 +- p2p/net/swarm/swarm_listen.go | 2 +- p2p/protocol/circuitv2/relay/relay.go | 6 +++--- p2p/protocol/circuitv2/util/pbconv.go | 28 +++++++++------------------ 9 files changed, 31 insertions(+), 41 deletions(-) diff --git a/core/peer/addrinfo.go b/core/peer/addrinfo.go index 19b07a4b7f..730d6f2ea6 100644 --- a/core/peer/addrinfo.go +++ b/core/peer/addrinfo.go @@ -86,7 +86,7 @@ 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 + addrs := make([]ma.Multiaddr, len(pi.Addrs)) p2ppart, err := ma.NewComponent("p2p", Encode(pi.ID)) if err != nil { return nil, err @@ -94,8 +94,8 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) { if len(pi.Addrs) == 0 { return []ma.Multiaddr{p2ppart}, nil } - for _, addr := range pi.Addrs { - addrs = append(addrs, addr.Encapsulate(p2ppart)) + for i, addr := range pi.Addrs { + addrs[i] = addr.Encapsulate(p2ppart) } return addrs, nil } diff --git a/core/peer/record.go b/core/peer/record.go index 9f8861af86..8685766c44 100644 --- a/core/peer/record.go +++ b/core/peer/record.go @@ -231,21 +231,21 @@ func (r *PeerRecord) ToProtobuf() (*pb.PeerRecord, error) { } func addrsFromProtobuf(addrs []*pb.PeerRecord_AddressInfo) []ma.Multiaddr { - var out []ma.Multiaddr - for _, addr := range addrs { + out := make([]ma.Multiaddr, len(addrs)) + for i, addr := range addrs { a, err := ma.NewMultiaddrBytes(addr.Multiaddr) if err != nil { continue } - out = append(out, a) + out[i] = a } return out } func addrsToProtobuf(addrs []ma.Multiaddr) []*pb.PeerRecord_AddressInfo { - var out []*pb.PeerRecord_AddressInfo - for _, addr := range addrs { - out = append(out, &pb.PeerRecord_AddressInfo{Multiaddr: addr.Bytes()}) + out := make([]*pb.PeerRecord_AddressInfo, len(addrs)) + for i, addr := range addrs { + out[i] = &pb.PeerRecord_AddressInfo{Multiaddr: addr.Bytes()} } return out } diff --git a/p2p/discovery/routing/routing_test.go b/p2p/discovery/routing/routing_test.go index ed8ab25d1d..6e28c06f06 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 7417526158..b21a676a7a 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/connmgr/connmgr_test.go b/p2p/net/connmgr/connmgr_test.go index 2053e3e6f7..533c49197f 100644 --- a/p2p/net/connmgr/connmgr_test.go +++ b/p2p/net/connmgr/connmgr_test.go @@ -520,10 +520,10 @@ func TestPeerProtectionSingleTag(t *testing.T) { } // protect the first 5 peers. - var protected []network.Conn - for _, c := range conns[0:5] { + protected := make([]network.Conn, len(conns[0:5])) + for i, c := range conns[0:5] { cm.Protect(c.RemotePeer(), "global") - protected = append(protected, c) + protected[i] = c // tag them negatively to make them preferred for pruning. cm.TagPeer(c.RemotePeer(), "test", -100) } @@ -599,11 +599,11 @@ func TestPeerProtectionMultipleTags(t *testing.T) { } // protect the first 5 peers under two tags. - var protected []network.Conn - for _, c := range conns[0:5] { + protected := make([]network.Conn, len(conns[0:5])) + for i, c := range conns[0:5] { cm.Protect(c.RemotePeer(), "tag1") cm.Protect(c.RemotePeer(), "tag2") - protected = append(protected, c) + protected[i] = c // tag them negatively to make them preferred for pruning. cm.TagPeer(c.RemotePeer(), "test", -100) } diff --git a/p2p/net/mock/mock_peernet.go b/p2p/net/mock/mock_peernet.go index 3fb0701322..85e636788f 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/net/swarm/swarm_listen.go b/p2p/net/swarm/swarm_listen.go index 044da2e8de..39e2f22182 100644 --- a/p2p/net/swarm/swarm_listen.go +++ b/p2p/net/swarm/swarm_listen.go @@ -39,7 +39,7 @@ func (s *Swarm) Listen(addrs ...ma.Multiaddr) error { // ListenClose stop and delete listeners for all of the given addresses. func (s *Swarm) ListenClose(addrs ...ma.Multiaddr) { - var listenersToClose []transport.Listener + listenersToClose := make([]transport.Listener, 0, len(s.listeners.m)) s.listeners.Lock() for l := range s.listeners.m { diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index 5e2d9f1835..2aa392da65 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -492,14 +492,14 @@ func (r *Relay) writeResponse(s network.Stream, status pbv2.Status, rsvp *pbv2.R func (r *Relay) makeReservationMsg(p peer.ID, expire time.Time) *pbv2.Reservation { expireUnix := uint64(expire.Unix()) - var addrBytes [][]byte - for _, addr := range r.host.Addrs() { + addrBytes := make([][]byte, len(r.host.Addrs())) + for i, addr := range r.host.Addrs() { if !manet.IsPublicAddr(addr) { continue } addr = addr.Encapsulate(r.selfAddr) - addrBytes = append(addrBytes, addr.Bytes()) + addrBytes[i] = addr.Bytes() } rsvp := &pbv2.Reservation{ diff --git a/p2p/protocol/circuitv2/util/pbconv.go b/p2p/protocol/circuitv2/util/pbconv.go index ae1b9b50b3..19bcebb4ae 100644 --- a/p2p/protocol/circuitv2/util/pbconv.go +++ b/p2p/protocol/circuitv2/util/pbconv.go @@ -36,13 +36,10 @@ 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, len(pi.Addrs)) - for _, addr := range pi.Addrs { - addrs = append(addrs, addr.Bytes()) + for i, addr := range pi.Addrs { + addrs[i] = addr.Bytes() } p := new(pbv1.CircuitRelay_Peer) @@ -62,15 +59,12 @@ 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, len(p.Addrs)) - for _, addrBytes := range p.Addrs { + for i, addrBytes := range p.Addrs { a, err := ma.NewMultiaddrBytes(addrBytes) if err == nil { - addrs = append(addrs, a) + addrs[i] = a } } @@ -78,14 +72,10 @@ 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, len(pi.Addrs)) - for _, addr := range pi.Addrs { - addrs = append(addrs, addr.Bytes()) + for i, addr := range pi.Addrs { + addrs[i] = addr.Bytes() } p := new(pbv2.Peer) From 99a81482611be754b81d5a6afb4a32ce4088be5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 6 Nov 2022 11:23:45 +0100 Subject: [PATCH 2/5] Move slice allocs to right before they're used --- core/peer/addrinfo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/peer/addrinfo.go b/core/peer/addrinfo.go index 730d6f2ea6..9c22c32f62 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) { - addrs := make([]ma.Multiaddr, len(pi.Addrs)) 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, len(pi.Addrs)) for i, addr := range pi.Addrs { addrs[i] = addr.Encapsulate(p2ppart) } From de2f46c1d62a35ea1a5c88145e24f20ede58768c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 6 Nov 2022 11:31:56 +0100 Subject: [PATCH 3/5] Revert slice assignments to append --- core/peer/addrinfo.go | 6 +++--- core/peer/record.go | 12 ++++++------ p2p/net/connmgr/connmgr_test.go | 12 ++++++------ p2p/protocol/circuitv2/relay/relay.go | 6 +++--- p2p/protocol/circuitv2/util/pbconv.go | 18 +++++++++--------- .../internal/circuitv1-deprecated/util.go | 6 +++--- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/core/peer/addrinfo.go b/core/peer/addrinfo.go index 9c22c32f62..b479df9e06 100644 --- a/core/peer/addrinfo.go +++ b/core/peer/addrinfo.go @@ -93,9 +93,9 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) { if len(pi.Addrs) == 0 { return []ma.Multiaddr{p2ppart}, nil } - addrs := make([]ma.Multiaddr, len(pi.Addrs)) - for i, addr := range pi.Addrs { - addrs[i] = addr.Encapsulate(p2ppart) + addrs := make([]ma.Multiaddr, 0, len(pi.Addrs)) + for _, addr := range pi.Addrs { + addrs = append(addrs, addr.Encapsulate(p2ppart)) } return addrs, nil } diff --git a/core/peer/record.go b/core/peer/record.go index 8685766c44..b502c8dd14 100644 --- a/core/peer/record.go +++ b/core/peer/record.go @@ -231,21 +231,21 @@ func (r *PeerRecord) ToProtobuf() (*pb.PeerRecord, error) { } func addrsFromProtobuf(addrs []*pb.PeerRecord_AddressInfo) []ma.Multiaddr { - out := make([]ma.Multiaddr, len(addrs)) - for i, addr := range addrs { + out := make([]ma.Multiaddr, 0, len(addrs)) + for _, addr := range addrs { a, err := ma.NewMultiaddrBytes(addr.Multiaddr) if err != nil { continue } - out[i] = a + out = append(out, a) } return out } func addrsToProtobuf(addrs []ma.Multiaddr) []*pb.PeerRecord_AddressInfo { - out := make([]*pb.PeerRecord_AddressInfo, len(addrs)) - for i, addr := range addrs { - out[i] = &pb.PeerRecord_AddressInfo{Multiaddr: addr.Bytes()} + out := make([]*pb.PeerRecord_AddressInfo, 0, len(addrs)) + for _, addr := range addrs { + out = append(out, &pb.PeerRecord_AddressInfo{Multiaddr: addr.Bytes()}) } return out } diff --git a/p2p/net/connmgr/connmgr_test.go b/p2p/net/connmgr/connmgr_test.go index 533c49197f..2295459015 100644 --- a/p2p/net/connmgr/connmgr_test.go +++ b/p2p/net/connmgr/connmgr_test.go @@ -520,10 +520,10 @@ func TestPeerProtectionSingleTag(t *testing.T) { } // protect the first 5 peers. - protected := make([]network.Conn, len(conns[0:5])) - for i, c := range conns[0:5] { + protected := make([]network.Conn, 0, len(conns[0:5])) + for _, c := range conns[0:5] { cm.Protect(c.RemotePeer(), "global") - protected[i] = c + protected = append(protected, c) // tag them negatively to make them preferred for pruning. cm.TagPeer(c.RemotePeer(), "test", -100) } @@ -599,11 +599,11 @@ func TestPeerProtectionMultipleTags(t *testing.T) { } // protect the first 5 peers under two tags. - protected := make([]network.Conn, len(conns[0:5])) - for i, c := range conns[0:5] { + protected := make([]network.Conn, 0, len(conns[0:5])) + for _, c := range conns[0:5] { cm.Protect(c.RemotePeer(), "tag1") cm.Protect(c.RemotePeer(), "tag2") - protected[i] = c + protected = append(protected, c) // tag them negatively to make them preferred for pruning. cm.TagPeer(c.RemotePeer(), "test", -100) } diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index 2aa392da65..c37311c26e 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -492,14 +492,14 @@ func (r *Relay) writeResponse(s network.Stream, status pbv2.Status, rsvp *pbv2.R func (r *Relay) makeReservationMsg(p peer.ID, expire time.Time) *pbv2.Reservation { expireUnix := uint64(expire.Unix()) - addrBytes := make([][]byte, len(r.host.Addrs())) - for i, addr := range r.host.Addrs() { + addrBytes := make([][]byte, 0, len(r.host.Addrs())) + for _, addr := range r.host.Addrs() { if !manet.IsPublicAddr(addr) { continue } addr = addr.Encapsulate(r.selfAddr) - addrBytes[i] = addr.Bytes() + addrBytes = append(addrBytes, addr.Bytes()) } rsvp := &pbv2.Reservation{ diff --git a/p2p/protocol/circuitv2/util/pbconv.go b/p2p/protocol/circuitv2/util/pbconv.go index 19bcebb4ae..4a884351ee 100644 --- a/p2p/protocol/circuitv2/util/pbconv.go +++ b/p2p/protocol/circuitv2/util/pbconv.go @@ -36,10 +36,10 @@ func PeerToPeerInfoV1(p *pbv1.CircuitRelay_Peer) (peer.AddrInfo, error) { } func PeerInfoToPeerV1(pi peer.AddrInfo) *pbv1.CircuitRelay_Peer { - addrs := make([][]byte, len(pi.Addrs)) + addrs := make([][]byte, 0, len(pi.Addrs)) - for i, addr := range pi.Addrs { - addrs[i] = addr.Bytes() + for _, addr := range pi.Addrs { + addrs = append(addrs, addr.Bytes()) } p := new(pbv1.CircuitRelay_Peer) @@ -59,12 +59,12 @@ func PeerToPeerInfoV2(p *pbv2.Peer) (peer.AddrInfo, error) { return peer.AddrInfo{}, err } - addrs := make([]ma.Multiaddr, len(p.Addrs)) + addrs := make([]ma.Multiaddr, 0, len(p.Addrs)) - for i, addrBytes := range p.Addrs { + for _, addrBytes := range p.Addrs { a, err := ma.NewMultiaddrBytes(addrBytes) if err == nil { - addrs[i] = a + addrs = append(addrs, a) } } @@ -72,10 +72,10 @@ func PeerToPeerInfoV2(p *pbv2.Peer) (peer.AddrInfo, error) { } func PeerInfoToPeerV2(pi peer.AddrInfo) *pbv2.Peer { - addrs := make([][]byte, len(pi.Addrs)) + addrs := make([][]byte, 0, len(pi.Addrs)) - for i, addr := range pi.Addrs { - addrs[i] = addr.Bytes() + for _, addr := range pi.Addrs { + addrs = append(addrs, addr.Bytes()) } p := new(pbv2.Peer) diff --git a/p2p/protocol/internal/circuitv1-deprecated/util.go b/p2p/protocol/internal/circuitv1-deprecated/util.go index 966d9c59ae..f964ba1f45 100644 --- a/p2p/protocol/internal/circuitv1-deprecated/util.go +++ b/p2p/protocol/internal/circuitv1-deprecated/util.go @@ -38,9 +38,9 @@ func peerToPeerInfo(p *pb.CircuitRelay_Peer) (peer.AddrInfo, error) { } func peerInfoToPeer(pi peer.AddrInfo) *pb.CircuitRelay_Peer { - addrs := make([][]byte, len(pi.Addrs)) - for i, addr := range pi.Addrs { - addrs[i] = addr.Bytes() + addrs := make([][]byte, 0, len(pi.Addrs)) + for _, addr := range pi.Addrs { + addrs = append(addrs, addr.Bytes()) } p := new(pb.CircuitRelay_Peer) From a6540749d818da0525c8ee0493e18a8ec292c61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 6 Nov 2022 11:52:48 +0100 Subject: [PATCH 4/5] Don't preallocate for tests or deprecated code --- p2p/net/connmgr/connmgr_test.go | 4 ++-- p2p/protocol/internal/circuitv1-deprecated/util.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/p2p/net/connmgr/connmgr_test.go b/p2p/net/connmgr/connmgr_test.go index 2295459015..2053e3e6f7 100644 --- a/p2p/net/connmgr/connmgr_test.go +++ b/p2p/net/connmgr/connmgr_test.go @@ -520,7 +520,7 @@ func TestPeerProtectionSingleTag(t *testing.T) { } // protect the first 5 peers. - protected := make([]network.Conn, 0, len(conns[0:5])) + var protected []network.Conn for _, c := range conns[0:5] { cm.Protect(c.RemotePeer(), "global") protected = append(protected, c) @@ -599,7 +599,7 @@ func TestPeerProtectionMultipleTags(t *testing.T) { } // protect the first 5 peers under two tags. - protected := make([]network.Conn, 0, len(conns[0:5])) + var protected []network.Conn for _, c := range conns[0:5] { cm.Protect(c.RemotePeer(), "tag1") cm.Protect(c.RemotePeer(), "tag2") diff --git a/p2p/protocol/internal/circuitv1-deprecated/util.go b/p2p/protocol/internal/circuitv1-deprecated/util.go index f964ba1f45..966d9c59ae 100644 --- a/p2p/protocol/internal/circuitv1-deprecated/util.go +++ b/p2p/protocol/internal/circuitv1-deprecated/util.go @@ -38,9 +38,9 @@ func peerToPeerInfo(p *pb.CircuitRelay_Peer) (peer.AddrInfo, error) { } func peerInfoToPeer(pi peer.AddrInfo) *pb.CircuitRelay_Peer { - addrs := make([][]byte, 0, len(pi.Addrs)) - for _, addr := range pi.Addrs { - addrs = append(addrs, addr.Bytes()) + addrs := make([][]byte, len(pi.Addrs)) + for i, addr := range pi.Addrs { + addrs[i] = addr.Bytes() } p := new(pb.CircuitRelay_Peer) From 48b1b040569969a6a810ea3cc840142a2a6dd0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 6 Nov 2022 12:02:06 +0100 Subject: [PATCH 5/5] Don't preallocate too much --- p2p/net/swarm/swarm_listen.go | 2 +- p2p/protocol/circuitv2/relay/relay.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/net/swarm/swarm_listen.go b/p2p/net/swarm/swarm_listen.go index 39e2f22182..044da2e8de 100644 --- a/p2p/net/swarm/swarm_listen.go +++ b/p2p/net/swarm/swarm_listen.go @@ -39,7 +39,7 @@ func (s *Swarm) Listen(addrs ...ma.Multiaddr) error { // ListenClose stop and delete listeners for all of the given addresses. func (s *Swarm) ListenClose(addrs ...ma.Multiaddr) { - listenersToClose := make([]transport.Listener, 0, len(s.listeners.m)) + var listenersToClose []transport.Listener s.listeners.Lock() for l := range s.listeners.m { diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index c37311c26e..5e2d9f1835 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -492,7 +492,7 @@ func (r *Relay) writeResponse(s network.Stream, status pbv2.Status, rsvp *pbv2.R func (r *Relay) makeReservationMsg(p peer.ID, expire time.Time) *pbv2.Reservation { expireUnix := uint64(expire.Unix()) - addrBytes := make([][]byte, 0, len(r.host.Addrs())) + var addrBytes [][]byte for _, addr := range r.host.Addrs() { if !manet.IsPublicAddr(addr) { continue