From ee87858c096f71aa37d21f5dc9aa20059cd8deda Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Thu, 14 Nov 2019 13:13:42 -0800 Subject: [PATCH] Avoid one memory allocation in LabelSet construction (#318) --- sdk/metric/sdk.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/sdk/metric/sdk.go b/sdk/metric/sdk.go index a06c4d34808..ae32d6e6f74 100644 --- a/sdk/metric/sdk.go +++ b/sdk/metric/sdk.go @@ -76,7 +76,7 @@ type ( // repeatedly. labels struct { meter *SDK - sorted []core.KeyValue + sorted sortedLabels encoded string } @@ -245,19 +245,23 @@ func (m *SDK) Labels(kvs ...core.KeyValue) api.LabelSet { return &m.empty } + ls := &labels{ + meter: m, + sorted: kvs, + } + // Sort and de-duplicate. - sorted := sortedLabels(kvs) - sort.Stable(&sorted) + sort.Stable(&ls.sorted) oi := 1 - for i := 1; i < len(sorted); i++ { - if sorted[i-1].Key == sorted[i].Key { - sorted[oi-1] = sorted[i] + for i := 1; i < len(ls.sorted); i++ { + if ls.sorted[i-1].Key == ls.sorted[i].Key { + ls.sorted[oi-1] = ls.sorted[i] continue } - sorted[oi] = sorted[i] + ls.sorted[oi] = ls.sorted[i] oi++ } - sorted = sorted[0:oi] + ls.sorted = ls.sorted[0:oi] // Serialize. buf := m.pool.Get().(*bytes.Buffer) @@ -265,7 +269,7 @@ func (m *SDK) Labels(kvs ...core.KeyValue) api.LabelSet { buf.Reset() _, _ = buf.WriteRune('|') delimiter := '#' - for _, kv := range sorted { + for _, kv := range ls.sorted { _, _ = buf.WriteRune(delimiter) _, _ = buf.WriteString(string(kv.Key)) _, _ = buf.WriteRune(':') @@ -273,11 +277,9 @@ func (m *SDK) Labels(kvs ...core.KeyValue) api.LabelSet { delimiter = ',' } - return &labels{ - meter: m, - sorted: sorted, - encoded: buf.String(), - } + ls.encoded = buf.String() + + return ls } // labsFor sanitizes the input LabelSet. The input will be rejected