Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions pkg/filter/fields/id.go

This file was deleted.

27 changes: 4 additions & 23 deletions pkg/filter/valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ package filter
import (
"sync"

"github.com/rabbitstack/fibratus/pkg/filter/fields"
"github.com/rabbitstack/fibratus/pkg/filter/ql"
)

// ValuerCache caches extracted field values for a single event's lifetime.
type ValuerCache struct {
slots [fields.MaxFieldID]any
valuer ql.MapValuer
}

var valuerCachePool = sync.Pool{
New: func() any {
return &ValuerCache{
valuer: make(ql.MapValuer, 8), // pre-allocate buckets
valuer: make(ql.MapValuer),
}
},
}
Expand All @@ -44,30 +42,13 @@ func AcquireValuerCache() *ValuerCache {
}

func (c *ValuerCache) Release() {
c.slots = [fields.MaxFieldID]any{}
clear(c.valuer)
valuerCachePool.Put(c)
}

func (c *ValuerCache) populateValuer(f Field, extract func() any) {
id := f.Name.ID()
if id == -1 {
// if the field doesn't allow fast id lookup
// extract the value and cache inside valuer
n := f.String()
if _, ok := c.valuer[n]; !ok {
c.valuer[n] = extract()
}
return
}

// field value is already cached, skip
v := c.slots[id]
if v != nil {
return
n := f.String()
if _, ok := c.valuer[n]; !ok {
c.valuer[n] = extract()
}

// extract the value and cache
v = extract()
c.slots[id], c.valuer[f.String()] = v, v
}
19 changes: 0 additions & 19 deletions pkg/filter/valuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,6 @@ func TestValuerCacheDistinctFields(t *testing.T) {
assert.Equal(t, `C:\Windows\System32\cmd.exe`, c.valuer[f1.String()])
}

func TestValuerCacheNilValue(t *testing.T) {
c := AcquireValuerCache()
defer c.Release()

calls := 0
c.populateValuer(Field{Name: fields.PsName, Value: fields.PsName.String()}, func() any {
calls++
return nil
})

// nil is not cached in slots (v != nil check), so extract will be called again
c.populateValuer(Field{Name: fields.PsName, Value: fields.PsName.String()}, func() any {
calls++
return nil
})

assert.Equal(t, 2, calls, "nil values are not cached, extract is called on every invocation")
}

func TestValuerCacheReset(t *testing.T) {
c := AcquireValuerCache()

Expand Down
Loading