mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
[PERF] TSDB: Grow postings by doubling
Go's built-in append() grows larger slices with factor 1.3, which means we do a lot more allocating and copying for larger postings. Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
1435c8ae4a
commit
33adbe47b1
|
@ -345,13 +345,22 @@ func (p *MemPostings) Add(id storage.SeriesRef, lset labels.Labels) {
|
|||
p.mtx.Unlock()
|
||||
}
|
||||
|
||||
func appendWithExponentialGrowth[T any](a []T, v T) []T {
|
||||
if cap(a) < len(a)+1 {
|
||||
newList := make([]T, len(a), len(a)*2+1)
|
||||
copy(newList, a)
|
||||
a = newList
|
||||
}
|
||||
return append(a, v)
|
||||
}
|
||||
|
||||
func (p *MemPostings) addFor(id storage.SeriesRef, l labels.Label) {
|
||||
nm, ok := p.m[l.Name]
|
||||
if !ok {
|
||||
nm = map[string][]storage.SeriesRef{}
|
||||
p.m[l.Name] = nm
|
||||
}
|
||||
list := append(nm[l.Value], id)
|
||||
list := appendWithExponentialGrowth(nm[l.Value], id)
|
||||
nm[l.Value] = list
|
||||
|
||||
if !p.ordered {
|
||||
|
|
Loading…
Reference in a new issue