Skip to content

Commit

Permalink
Merge pull request #342 from bobrik/ivan/decoder-cache
Browse files Browse the repository at this point in the history
Add decoder cache
  • Loading branch information
bobrik committed Feb 5, 2024
2 parents 5af4585 + b209828 commit 6133b48
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
20 changes: 20 additions & 0 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Decoder interface {
type Set struct {
mu sync.Mutex
decoders map[string]Decoder
cache map[string][]string
}

// NewSet creates a Set with all known decoders
Expand Down Expand Up @@ -56,6 +57,7 @@ func NewSet() (*Set, error) {
"syscall": &Syscall{},
"uint": &UInt{},
},
cache: map[string][]string{},
}, nil
}

Expand Down Expand Up @@ -87,6 +89,24 @@ func (s *Set) Decode(in []byte, label config.Label) ([]byte, error) {
// DecodeLabels transforms eBPF map key bytes into a list of label values
// according to configuration
func (s *Set) DecodeLabels(in []byte, labels []config.Label) ([]string, error) {
// string(in) must not be a variable to avoid allocation:
// * https://github.com/golang/go/commit/f5f5a8b6209f8
if cached, ok := s.cache[string(in)]; ok {
return cached, nil
}

values, err := s.decodeLabels(in, labels)
if err != nil {
return nil, err
}

s.cache[string(in)] = values

return values, nil
}

// decodeLabels is the inner function of DecodeLabels without any caching
func (s *Set) decodeLabels(in []byte, labels []config.Label) ([]string, error) {
values := make([]string, len(labels))

off := uint(0)
Expand Down
71 changes: 71 additions & 0 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,77 @@ func TestDecodeLabels(t *testing.T) {
}
}

func BenchmarkCache(b *testing.B) {
in := []byte{
0x8, 0xab, 0xce, 0xef,
0xde, 0xad,
0xbe, 0xef,
0x8, 0xab, 0xce, 0xef, 0x8, 0xab, 0xce, 0xef,
}

labels := []config.Label{
{
Name: "number1",
Size: 4,
Decoders: []config.Decoder{
{
Name: "uint",
},
},
},
{
Name: "number2",
Size: 2,
Decoders: []config.Decoder{
{
Name: "uint",
},
},
},
{
Name: "number3",
Size: 2,
Decoders: []config.Decoder{
{
Name: "uint",
},
},
},
{
Name: "number4",
Size: 8,
Decoders: []config.Decoder{
{
Name: "uint",
},
},
},
}

s, err := NewSet()
if err != nil {
b.Fatal(err)
}

b.Run("direct", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := s.decodeLabels(in, labels)
if err != nil {
b.Fatal(err)
}
}
})

b.Run("cached", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := s.DecodeLabels(in, labels)
if err != nil {
b.Fatal(err)
}
}
})
}

func zeroPaddedString(in string, size int) []byte {
if len(in) > size {
panic(fmt.Sprintf("string %q is longer than requested size %d", in, size))
Expand Down

0 comments on commit 6133b48

Please sign in to comment.