From 609934ce99eec9dcece2f89b812b57d4398ca5f9 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Mon, 15 Jul 2019 11:13:58 +0100 Subject: [PATCH 1/2] Preallocate slices; avoide append() reallocations when copying data --- registry/util.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/registry/util.go b/registry/util.go index 5eb51a62..414c6ff3 100644 --- a/registry/util.go +++ b/registry/util.go @@ -51,33 +51,32 @@ func delNodes(old, del []*Node) []*Node { // Copy makes a copy of services func Copy(current []*Service) []*Service { - var services []*Service - - for _, service := range current { + services := make([]*Service, len(current)) + for i, service := range current { // copy service s := new(Service) *s = *service // copy nodes - var nodes []*Node - for _, node := range service.Nodes { + nodes := make([]*Node, len(service.Nodes)) + for j, node := range service.Nodes { n := new(Node) *n = *node - nodes = append(nodes, n) + nodes[j] = n } s.Nodes = nodes // copy endpoints - var eps []*Endpoint - for _, ep := range service.Endpoints { + eps := make([]*Endpoint, len(service.Endpoints)) + for j, ep := range service.Endpoints { e := new(Endpoint) *e = *ep - eps = append(eps, e) + eps[j] = e } s.Endpoints = eps // append service - services = append(services, s) + services[i] = s } return services From c108188d65672d1784e07fb90d3320d754596653 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Mon, 15 Jul 2019 14:47:33 +0100 Subject: [PATCH 2/2] Preallocate nodes slice in addNodes before populating it --- registry/util.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/registry/util.go b/registry/util.go index 414c6ff3..5cd04eb9 100644 --- a/registry/util.go +++ b/registry/util.go @@ -1,12 +1,11 @@ package registry func addNodes(old, neu []*Node) []*Node { - var nodes []*Node - + nodes := make([]*Node, len(neu)) // add all new nodes - for _, n := range neu { + for i, n := range neu { node := *n - nodes = append(nodes, &node) + nodes[i] = &node } // look at old nodes