mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Merge pull request #12680 from bboreham/faster-has
labels: improve Has() method for stringlabels build
This commit is contained in:
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.
|
// Has returns true if the label with the given name is present.
|
||||||
func (ls Labels) Has(name string) bool {
|
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); {
|
for i := 0; i < len(ls.data); {
|
||||||
var lName string
|
var size int
|
||||||
lName, i = decodeString(ls.data, i)
|
size, i = decodeSize(ls.data, i)
|
||||||
_, i = decodeString(ls.data, i)
|
if ls.data[i] == name[0] {
|
||||||
|
lName := ls.data[i : i+size]
|
||||||
|
i += size
|
||||||
if lName == name {
|
if lName == name {
|
||||||
return true
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -472,17 +472,23 @@ func BenchmarkLabels_Get(b *testing.B) {
|
||||||
for _, scenario := range []struct {
|
for _, scenario := range []struct {
|
||||||
desc, label string
|
desc, label string
|
||||||
}{
|
}{
|
||||||
{"get first label", allLabels[0].Name},
|
{"first label", allLabels[0].Name},
|
||||||
{"get middle label", allLabels[size/2].Name},
|
{"middle label", allLabels[size/2].Name},
|
||||||
{"get last label", allLabels[size-1].Name},
|
{"last label", allLabels[size-1].Name},
|
||||||
{"get not-found label", "benchmark"},
|
{"not-found label", "benchmark"},
|
||||||
} {
|
} {
|
||||||
b.Run(scenario.desc, func(b *testing.B) {
|
b.Run(scenario.desc, func(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.Run("get", func(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_ = labels.Get(scenario.label)
|
_ = 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 a new issue