Skip to content

Commit

Permalink
Merge "Cache signature of an empty label set"
Browse files Browse the repository at this point in the history
  • Loading branch information
bernerdschaefer authored and Gerrit Code Review committed Sep 16, 2013
2 parents a9b3602 + 26ad852 commit ad41ea8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions prometheus/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions prometheus/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package prometheus

import (
"runtime"
"testing"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ad41ea8

Please sign in to comment.