mirror of https://github.com/prometheus/prometheus
Merge pull request #12680 from bboreham/faster-has
labels: improve Has() method for stringlabels buildpull/12684/head
commit
5007fa305d
|
@ -300,13 +300,26 @@ func (ls Labels) Get(name string) string {
|
|||
|
||||
// Has returns true if the label with the given name is present.
|
||||
func (ls Labels) Has(name string) bool {
|
||||
if name == "" { // Avoid crash in loop if someone asks for "".
|
||||
return false // Prometheus does not store blank label names.
|
||||
}
|
||||
for i := 0; i < len(ls.data); {
|
||||
var lName string
|
||||
lName, i = decodeString(ls.data, i)
|
||||
_, i = decodeString(ls.data, i)
|
||||
if lName == name {
|
||||
return true
|
||||
var size int
|
||||
size, i = decodeSize(ls.data, i)
|
||||
if ls.data[i] == name[0] {
|
||||
lName := ls.data[i : i+size]
|
||||
i += size
|
||||
if lName == name {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if ls.data[i] > name[0] { // Stop looking if we've gone past.
|
||||
break
|
||||
}
|
||||
i += size
|
||||
}
|
||||
size, i = decodeSize(ls.data, i)
|
||||
i += size
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -472,16 +472,22 @@ func BenchmarkLabels_Get(b *testing.B) {
|
|||
for _, scenario := range []struct {
|
||||
desc, label string
|
||||
}{
|
||||
{"get first label", allLabels[0].Name},
|
||||
{"get middle label", allLabels[size/2].Name},
|
||||
{"get last label", allLabels[size-1].Name},
|
||||
{"get not-found label", "benchmark"},
|
||||
{"first label", allLabels[0].Name},
|
||||
{"middle label", allLabels[size/2].Name},
|
||||
{"last label", allLabels[size-1].Name},
|
||||
{"not-found label", "benchmark"},
|
||||
} {
|
||||
b.Run(scenario.desc, func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = labels.Get(scenario.label)
|
||||
}
|
||||
b.Run("get", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = labels.Get(scenario.label)
|
||||
}
|
||||
})
|
||||
b.Run("has", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = labels.Has(scenario.label)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue