mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-11 13:57:36 -08:00
Preallocate Labels in labels.Builder (#10749)
This tries to avoid re-allocations of labels slice since we know possible max size Signed-off-by: Łukasz Mierzwa <l.mierzwa@gmail.com>
This commit is contained in:
parent
d3cb39044e
commit
08262454a3
|
@ -349,7 +349,7 @@ func FromStrings(ss ...string) Labels {
|
||||||
if len(ss)%2 != 0 {
|
if len(ss)%2 != 0 {
|
||||||
panic("invalid number of strings")
|
panic("invalid number of strings")
|
||||||
}
|
}
|
||||||
var res Labels
|
res := make(Labels, 0, len(ss)/2)
|
||||||
for i := 0; i < len(ss); i += 2 {
|
for i := 0; i < len(ss); i += 2 {
|
||||||
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
|
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
|
||||||
}
|
}
|
||||||
|
@ -452,7 +452,7 @@ func (b *Builder) Labels() Labels {
|
||||||
|
|
||||||
// In the general case, labels are removed, modified or moved
|
// In the general case, labels are removed, modified or moved
|
||||||
// rather than added.
|
// rather than added.
|
||||||
res := make(Labels, 0, len(b.base))
|
res := make(Labels, 0, len(b.base)+len(b.add))
|
||||||
Outer:
|
Outer:
|
||||||
for _, l := range b.base {
|
for _, l := range b.base {
|
||||||
for _, n := range b.del {
|
for _, n := range b.del {
|
||||||
|
|
|
@ -16,6 +16,7 @@ package promql
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -680,7 +681,6 @@ load 10s
|
||||||
Result: Matrix{
|
Result: Matrix{
|
||||||
Series{
|
Series{
|
||||||
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
Points: []Point{{V: 1, T: 0}, {V: 1, T: 1000}, {V: 1, T: 2000}},
|
||||||
Metric: labels.FromStrings(),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Start: time.Unix(0, 0),
|
Start: time.Unix(0, 0),
|
||||||
|
@ -717,7 +717,8 @@ load 10s
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for i, c := range cases {
|
||||||
|
t.Run(fmt.Sprintf("%d query=%s", i, c.Query), func(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
var qry Query
|
var qry Query
|
||||||
if c.Interval == 0 {
|
if c.Interval == 0 {
|
||||||
|
@ -730,11 +731,12 @@ load 10s
|
||||||
res := qry.Exec(test.Context())
|
res := qry.Exec(test.Context())
|
||||||
if c.ShouldError {
|
if c.ShouldError {
|
||||||
require.Error(t, res.Err, "expected error for the query %q", c.Query)
|
require.Error(t, res.Err, "expected error for the query %q", c.Query)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, res.Err)
|
require.NoError(t, res.Err)
|
||||||
require.Equal(t, c.Result, res.Value, "query %q failed", c.Query)
|
require.Equal(t, c.Result, res.Value, "query %q failed", c.Query)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue