Browse Source

Compare equality of label.Label structs directly (#10427)

* labels.Equal benchmark for equal, not equal, and differing lengths

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>

* Compare equality of label.Label structs directly

Compare the structs using `==` instead of the name and value
of each label. This is functionally equivalent and about ~10%
faster in my testing.

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>

* Use longer more realistic names and values in benchmark

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
pull/10450/head
Nick Pillitteri 3 years ago committed by GitHub
parent
commit
53ac9d6d66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      model/labels/labels.go
  2. 30
      model/labels/labels_test.go

2
model/labels/labels.go

@ -307,7 +307,7 @@ func Equal(ls, o Labels) bool {
return false
}
for i, l := range ls {
if l.Name != o[i].Name || l.Value != o[i].Value {
if l != o[i] {
return false
}
}

30
model/labels/labels_test.go

@ -586,6 +586,36 @@ func BenchmarkLabels_Get(b *testing.B) {
}
}
func BenchmarkLabels_Equals(b *testing.B) {
for _, scenario := range []struct {
desc string
base, other Labels
}{
{
"equal",
Labels{{"a_label_name", "a_label_value"}, {"another_label_name", "another_label_value"}},
Labels{{"a_label_name", "a_label_value"}, {"another_label_name", "another_label_value"}},
},
{
"not equal",
Labels{{"a_label_name", "a_label_value"}, {"another_label_name", "another_label_value"}},
Labels{{"a_label_name", "a_label_value"}, {"another_label_name", "a_different_label_value"}},
},
{
"different sizes",
Labels{{"a_label_name", "a_label_value"}, {"another_label_name", "another_label_value"}},
Labels{{"a_label_name", "a_label_value"}},
},
} {
b.Run(scenario.desc, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Equal(scenario.base, scenario.other)
}
})
}
}
func TestLabels_Copy(t *testing.T) {
require.Equal(t, Labels{{"aaa", "111"}, {"bbb", "222"}}, Labels{{"aaa", "111"}, {"bbb", "222"}}.Copy())
}

Loading…
Cancel
Save