From 2083b7b04debbb152f87fa1bf3ad6d5d76a52c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Savc=C4=B1?= Date: Wed, 1 Apr 2020 11:54:51 +0300 Subject: [PATCH] agent: add len, cap while initializing arrays --- agent/config/runtime.go | 2 +- agent/consul/state/catalog.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/agent/config/runtime.go b/agent/config/runtime.go index eee6e5b720..f80d28de79 100644 --- a/agent/config/runtime.go +++ b/agent/config/runtime.go @@ -1793,7 +1793,7 @@ func sanitize(name string, v reflect.Value) reflect.Value { return reflect.ValueOf(m) case isArray(typ) || isSlice(typ): - ma := make([]interface{}, 0) + ma := make([]interface{}, 0, v.Len()) for i := 0; i < v.Len(); i++ { ma = append(ma, sanitize(fmt.Sprintf("%s[%d]", name, i), v.Index(i)).Interface()) } diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index 0c17654855..3f045ad2dc 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -739,7 +739,7 @@ func (s *Store) Services(ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (ui // Generate the output structure. var results = make(structs.Services) for service, tags := range unique { - results[service] = make([]string, 0) + results[service] = make([]string, 0, len(tags)) for tag := range tags { results[service] = append(results[service], tag) } @@ -837,7 +837,7 @@ func (s *Store) ServicesByNodeMeta(ws memdb.WatchSet, filters map[string]string, // Generate the output structure. var results = make(structs.Services) for service, tags := range unique { - results[service] = make([]string, 0) + results[service] = make([]string, 0, len(tags)) for tag := range tags { results[service] = append(results[service], tag) }