Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize allocations in dictionaries #1610

Merged
merged 7 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 18 additions & 4 deletions pkg/storage/dict/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/pyroscope-io/pyroscope/pkg/util/varint"
"github.com/valyala/bytebufferpool"
)

type (
Expand Down Expand Up @@ -69,10 +70,23 @@ func (t *Dict) readValue(key Key, w io.Writer) bool {
}
}

func (t *Dict) Put(val Value) Key {
var writerPool = sync.Pool{New: func() any { return varint.NewWriter() }}

func (t *Dict) PutValue(val Value, dst io.Writer) {
t.m.Lock()
defer t.m.Unlock()
var buf bytes.Buffer
t.root.findNodeAt(val, &buf)
return buf.Bytes()
vw := writerPool.Get().(varint.Writer)
defer writerPool.Put(vw)
t.root.findNodeAt(val, vw, dst)
}

var bufferPool bytebufferpool.Pool

func (t *Dict) Put(val Value) Key {
b := bufferPool.Get()
defer bufferPool.Put(b)
t.PutValue(val, b)
k := make([]byte, b.Len())
kolesnikovae marked this conversation as resolved.
Show resolved Hide resolved
copy(k, b.B)
return k
}
13 changes: 4 additions & 9 deletions pkg/storage/dict/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ type trieNode struct {
}

func newTrieNode(label []byte) *trieNode {
labelCopy := make([]byte, len(label))
copy(labelCopy, label)
return &trieNode{
label: label,
label: labelCopy,
children: make([]*trieNode, 0),
}
}
Expand All @@ -27,14 +29,7 @@ func (tn *trieNode) insert(t2 *trieNode) {
}

// TODO: too complicated, need to refactor / document this
func (tn *trieNode) findNodeAt(key []byte, w io.Writer) {
// log.Debug("findNodeAt")
key2 := make([]byte, len(key))
// TODO: remove
copy(key2, key)
key = key2
vw := varint.NewWriter()

kolesnikovae marked this conversation as resolved.
Show resolved Hide resolved
func (tn *trieNode) findNodeAt(key []byte, vw varint.Writer, w io.Writer) {
OuterLoop:
for {
// log.Debug("findNodeAt, key", string(key))
Expand Down