mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-21 03:16:00 -08:00
Merge pull request #12073 from bboreham/slices-sort2
labels: use slices.Sort for better performance
This commit is contained in:
commit
ff993b279a
|
@ -18,11 +18,11 @@ package labels
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/cespare/xxhash/v2"
|
"github.com/cespare/xxhash/v2"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Well-known label names used by Prometheus components.
|
// Well-known label names used by Prometheus components.
|
||||||
|
@ -360,7 +360,7 @@ func EmptyLabels() Labels {
|
||||||
func New(ls ...Label) Labels {
|
func New(ls ...Label) Labels {
|
||||||
set := make(Labels, 0, len(ls))
|
set := make(Labels, 0, len(ls))
|
||||||
set = append(set, ls...)
|
set = append(set, ls...)
|
||||||
sort.Sort(set)
|
slices.SortFunc(set, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
|
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
@ -384,7 +384,7 @@ func FromStrings(ss ...string) Labels {
|
||||||
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
|
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(res)
|
slices.SortFunc(res, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -564,7 +564,7 @@ Outer:
|
||||||
}
|
}
|
||||||
if len(b.add) > 0 { // Base is already in order, so we only need to sort if we add to it.
|
if len(b.add) > 0 { // Base is already in order, so we only need to sort if we add to it.
|
||||||
res = append(res, b.add...)
|
res = append(res, b.add...)
|
||||||
sort.Sort(res)
|
slices.SortFunc(res, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -591,7 +591,7 @@ func (b *ScratchBuilder) Add(name, value string) {
|
||||||
|
|
||||||
// Sort the labels added so far by name.
|
// Sort the labels added so far by name.
|
||||||
func (b *ScratchBuilder) Sort() {
|
func (b *ScratchBuilder) Sort() {
|
||||||
sort.Sort(b.add)
|
slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asssign is for when you already have a Labels which you want this ScratchBuilder to return.
|
// Asssign is for when you already have a Labels which you want this ScratchBuilder to return.
|
||||||
|
|
|
@ -19,12 +19,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/cespare/xxhash/v2"
|
"github.com/cespare/xxhash/v2"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Well-known label names used by Prometheus components.
|
// Well-known label names used by Prometheus components.
|
||||||
|
@ -385,7 +385,7 @@ func yoloBytes(s string) (b []byte) {
|
||||||
// New returns a sorted Labels from the given labels.
|
// New returns a sorted Labels from the given labels.
|
||||||
// The caller has to guarantee that all label names are unique.
|
// The caller has to guarantee that all label names are unique.
|
||||||
func New(ls ...Label) Labels {
|
func New(ls ...Label) Labels {
|
||||||
sort.Sort(labelSlice(ls))
|
slices.SortFunc(ls, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
size := labelsSize(ls)
|
size := labelsSize(ls)
|
||||||
buf := make([]byte, size)
|
buf := make([]byte, size)
|
||||||
marshalLabelsToSizedBuffer(ls, buf)
|
marshalLabelsToSizedBuffer(ls, buf)
|
||||||
|
@ -411,7 +411,7 @@ func FromStrings(ss ...string) Labels {
|
||||||
ls = append(ls, Label{Name: ss[i], Value: ss[i+1]})
|
ls = append(ls, Label{Name: ss[i], Value: ss[i+1]})
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(labelSlice(ls))
|
slices.SortFunc(ls, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
return New(ls...)
|
return New(ls...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -595,8 +595,8 @@ func (b *Builder) Labels(res Labels) Labels {
|
||||||
return b.base
|
return b.base
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(labelSlice(b.add))
|
slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
sort.Strings(b.del)
|
slices.Sort(b.del)
|
||||||
a, d := 0, 0
|
a, d := 0, 0
|
||||||
|
|
||||||
bufSize := len(b.base.data) + labelsSize(b.add)
|
bufSize := len(b.base.data) + labelsSize(b.add)
|
||||||
|
@ -754,7 +754,7 @@ func (b *ScratchBuilder) Add(name, value string) {
|
||||||
|
|
||||||
// Sort the labels added so far by name.
|
// Sort the labels added so far by name.
|
||||||
func (b *ScratchBuilder) Sort() {
|
func (b *ScratchBuilder) Sort() {
|
||||||
sort.Sort(labelSlice(b.add))
|
slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asssign is for when you already have a Labels which you want this ScratchBuilder to return.
|
// Asssign is for when you already have a Labels which you want this ScratchBuilder to return.
|
||||||
|
|
|
@ -696,6 +696,31 @@ func BenchmarkLabels_Hash(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkBuilder(b *testing.B) {
|
||||||
|
m := []Label{
|
||||||
|
{"job", "node"},
|
||||||
|
{"instance", "123.123.1.211:9090"},
|
||||||
|
{"path", "/api/v1/namespaces/<namespace>/deployments/<name>"},
|
||||||
|
{"method", "GET"},
|
||||||
|
{"namespace", "system"},
|
||||||
|
{"status", "500"},
|
||||||
|
{"prometheus", "prometheus-core-1"},
|
||||||
|
{"datacenter", "eu-west-1"},
|
||||||
|
{"pod_name", "abcdef-99999-defee"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var l Labels
|
||||||
|
builder := NewBuilder(EmptyLabels())
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
builder.Reset(EmptyLabels())
|
||||||
|
for _, l := range m {
|
||||||
|
builder.Set(l.Name, l.Value)
|
||||||
|
}
|
||||||
|
l = builder.Labels(EmptyLabels())
|
||||||
|
}
|
||||||
|
require.Equal(b, 9, l.Len())
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkLabels_Copy(b *testing.B) {
|
func BenchmarkLabels_Copy(b *testing.B) {
|
||||||
m := map[string]string{
|
m := map[string]string{
|
||||||
"job": "node",
|
"job": "node",
|
||||||
|
|
Loading…
Reference in a new issue