diff --git a/pkg/logql/log/fmt.go b/pkg/logql/log/fmt.go index 9257834eee345..e28f5a119a48b 100644 --- a/pkg/logql/log/fmt.go +++ b/pkg/logql/log/fmt.go @@ -221,10 +221,7 @@ func (lf *LineFormatter) Process(ts int64, line []byte, lbs *LabelsBuilder) ([]b lf.currentLine = line lf.currentTs = ts - // map now is taking from a pool - m := lbs.Map() - defer smp.Put(m) - if err := lf.Template.Execute(lf.buf, m); err != nil { + if err := lf.Template.Execute(lf.buf, lbs.Map()); err != nil { lbs.SetErr(errTemplateFormat) lbs.SetErrorDetails(err.Error()) return line, true @@ -383,8 +380,7 @@ func (lf *LabelsFormatter) Process(ts int64, l []byte, lbs *LabelsBuilder) ([]by lf.currentLine = l lf.currentTs = ts - var m = smp.Get() - defer smp.Put(m) + var data interface{} for _, f := range lf.formats { if f.Rename { v, category, ok := lbs.GetWithCategory(f.Value) @@ -395,10 +391,10 @@ func (lf *LabelsFormatter) Process(ts int64, l []byte, lbs *LabelsBuilder) ([]by continue } lf.buf.Reset() - if len(m) == 0 { - lbs.IntoMap(m) + if data == nil { + data = lbs.Map() } - if err := f.tmpl.Execute(lf.buf, m); err != nil { + if err := f.tmpl.Execute(lf.buf, data); err != nil { lbs.SetErr(errTemplateFormat) lbs.SetErrorDetails(err.Error()) continue diff --git a/pkg/logql/log/labels.go b/pkg/logql/log/labels.go index 76a1ae0d7d5e7..579724e0bdbcd 100644 --- a/pkg/logql/log/labels.go +++ b/pkg/logql/log/labels.go @@ -449,52 +449,6 @@ func (b *LabelsBuilder) UnsortedLabels(buf labels.Labels, categories ...LabelCat return buf } -type stringMapPool struct { - pool sync.Pool -} - -func newStringMapPool() *stringMapPool { - return &stringMapPool{ - pool: sync.Pool{ - New: func() interface{} { - return make(map[string]string) - }, - }, - } -} - -func (s *stringMapPool) Get() map[string]string { - m := s.pool.Get().(map[string]string) - clear(m) - return m -} - -func (s *stringMapPool) Put(m map[string]string) { - s.pool.Put(m) -} - -var smp = newStringMapPool() - -// puts labels entries into an existing map, it is up to the caller to -// properly clear the map if it is going to be reused -func (b *LabelsBuilder) IntoMap(m map[string]string) { - if !b.hasDel() && !b.hasAdd() && !b.HasErr() { - if b.baseMap == nil { - b.baseMap = b.base.Map() - for k, v := range b.baseMap { - m[k] = v - } - } - return - } - b.buf = b.UnsortedLabels(b.buf) - // todo should we also cache maps since limited by the result ? - // Maps also don't create a copy of the labels. - for _, l := range b.buf { - m[l.Name] = l.Value - } -} - func (b *LabelsBuilder) Map() map[string]string { if !b.hasDel() && !b.hasAdd() && !b.HasErr() { if b.baseMap == nil { @@ -505,8 +459,7 @@ func (b *LabelsBuilder) Map() map[string]string { b.buf = b.UnsortedLabels(b.buf) // todo should we also cache maps since limited by the result ? // Maps also don't create a copy of the labels. - res := smp.Get() - clear(res) + res := make(map[string]string, len(b.buf)) for _, l := range b.buf { res[l.Name] = l.Value }