From 0acad4e1c3234efb10329162517c5ba5dd5cbb1a Mon Sep 17 00:00:00 2001 From: Marcin Dobosz Date: Tue, 14 Jul 2020 13:42:59 -0700 Subject: [PATCH] refactor(perf): improve tag serialization --- metrics/util.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/metrics/util.go b/metrics/util.go index dc0a98a..2181a91 100644 --- a/metrics/util.go +++ b/metrics/util.go @@ -32,7 +32,7 @@ func cloneTagsWithMap(original []string, newTags map[string]string) []string { i := len(original) for k, v := range newTags { - combined[i] = fmt.Sprintf("%s:%s", k, v) + combined[i] = buildTag(k, v) i++ } @@ -44,12 +44,21 @@ func mapToStrings(tagMap map[string]string) []string { tags := make([]string, 0, len(tagMap)) for k, v := range tagMap { - tags = append(tags, fmt.Sprintf("%s:%s", k, v)) + tags = append(tags, buildTag(k, v)) } return tags } +func buildTag(k, v string) string { + var b strings.Builder + b.Grow(len(k) + len(v) + 1) + b.WriteString(k) + b.WriteByte(':') + b.WriteString(v) + return b.String() +} + // convertType converts a value into an specific type if possible, otherwise // panics. The returned interface is guaranteed to cast properly. func convertType(value interface{}, toType reflect.Type) interface{} {