Preallocate slices; avoide append() reallocations when copying data
This commit is contained in:
parent
5157241c88
commit
609934ce99
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user