Merge pull request #587 from milosgajdos83/registry-copy-perf

Preallocate slices in registry.Copy() to avoid append() reallocations when copying data
This commit is contained in:
Asim Aslam 2019-07-15 07:04:49 -07:00 committed by GitHub
commit 8c7e35c3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,11 @@
package registry package registry
func addNodes(old, neu []*Node) []*Node { func addNodes(old, neu []*Node) []*Node {
var nodes []*Node nodes := make([]*Node, len(neu))
// add all new nodes // add all new nodes
for _, n := range neu { for i, n := range neu {
node := *n node := *n
nodes = append(nodes, &node) nodes[i] = &node
} }
// look at old nodes // look at old nodes
@ -51,33 +50,32 @@ func delNodes(old, del []*Node) []*Node {
// Copy makes a copy of services // Copy makes a copy of services
func Copy(current []*Service) []*Service { func Copy(current []*Service) []*Service {
var services []*Service services := make([]*Service, len(current))
for i, service := range current {
for _, service := range current {
// copy service // copy service
s := new(Service) s := new(Service)
*s = *service *s = *service
// copy nodes // copy nodes
var nodes []*Node nodes := make([]*Node, len(service.Nodes))
for _, node := range service.Nodes { for j, node := range service.Nodes {
n := new(Node) n := new(Node)
*n = *node *n = *node
nodes = append(nodes, n) nodes[j] = n
} }
s.Nodes = nodes s.Nodes = nodes
// copy endpoints // copy endpoints
var eps []*Endpoint eps := make([]*Endpoint, len(service.Endpoints))
for _, ep := range service.Endpoints { for j, ep := range service.Endpoints {
e := new(Endpoint) e := new(Endpoint)
*e = *ep *e = *ep
eps = append(eps, e) eps[j] = e
} }
s.Endpoints = eps s.Endpoints = eps
// append service // append service
services = append(services, s) services[i] = s
} }
return services return services