Skip to content

Commit

Permalink
revert map pooling from #11484
Browse files Browse the repository at this point in the history
Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan committed Jan 4, 2024
1 parent ce57448 commit 0bd0f78
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 57 deletions.
14 changes: 5 additions & 9 deletions pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
49 changes: 1 addition & 48 deletions pkg/logql/log/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 0bd0f78

Please sign in to comment.