Cache signature of an empty label set

This is an optimization of labelsToSignature to avoid excess allocations
when the label set is empty.

Change-Id: If2d59bbc3ae6d4457e2ded197b6f4e7c67e6a173
This commit is contained in:
Bernerd Schaefer 2013-09-11 17:16:34 +02:00
parent 148fde894b
commit 26ad852c94
2 changed files with 27 additions and 0 deletions

View File

@ -14,9 +14,16 @@ import (
"github.com/prometheus/client_golang/model"
)
// cache the signature of an empty label set.
var emptyLabelSignature = fnv.New64a().Sum64()
// LabelsToSignature provides a way of building a unique signature
// (i.e., fingerprint) for a given label set sequence.
func labelsToSignature(labels map[string]string) uint64 {
if len(labels) == 0 {
return emptyLabelSignature
}
names := make(model.LabelNames, 0, len(labels))
for name := range labels {
names = append(names, model.LabelName(name))

View File

@ -7,6 +7,7 @@
package prometheus
import (
"runtime"
"testing"
)
@ -38,6 +39,25 @@ func TestLabelToSignature(t *testing.T) {
testLabelsToSignature(t)
}
func TestEmptyLabelSignature(t *testing.T) {
input := []map[string]string{nil, {}}
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
alloc := ms.Alloc
for _, labels := range input {
labelsToSignature(labels)
}
runtime.ReadMemStats(&ms)
if got := ms.Alloc; alloc != got {
t.Fatal("expected labelsToSignature with empty labels not to perform allocations")
}
}
func BenchmarkLabelToSignature(b *testing.B) {
for i := 0; i < b.N; i++ {
testLabelsToSignature(b)