From afe77853b7c8976a7c59681a97838ae15fafa934 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 29 Mar 2017 16:55:28 -0700 Subject: [PATCH] server+lnwire: pre-allocate address slices --- lnwire/lnwire.go | 10 ++++++---- server.go | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index 2bc79c262..05d6ac150 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -619,13 +619,15 @@ func readElement(r io.Reader, element interface{}) error { } case *[]net.Addr: - var addresses []net.Addr - var numAddrs [2]byte - if _, err = io.ReadFull(r, numAddrs[:]); err != nil { + var numAddrsBytes [2]byte + if _, err = io.ReadFull(r, numAddrsBytes[:]); err != nil { return err } - for i := 0; i < int(binary.BigEndian.Uint16(numAddrs[:])); i++ { + numAddrs := binary.BigEndian.Uint16(numAddrsBytes[:]) + addresses := make([]net.Addr, 0, numAddrs) + + for i := 0; i < int(numAddrs); i++ { var descriptor [1]byte if _, err = io.ReadFull(r, descriptor[:]); err != nil { return err diff --git a/server.go b/server.go index a5c49e87c..c892535f4 100644 --- a/server.go +++ b/server.go @@ -156,7 +156,7 @@ func newServer(listenAddrs []string, notifier chainntnfs.ChainNotifier, // If external IP addresses have been specified, add those to the list // of this server's addresses. - selfAddrs := make([]net.Addr, 0) + selfAddrs := make([]net.Addr, 0, len(cfg.ExternalIPs)) for _, ip := range cfg.ExternalIPs { addr, err := net.ResolveTCPAddr("tcp", ip) if err != nil {