Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Precompute encodeWithKeys buffer size to avoid resizes #1269

Merged
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
9 changes: 8 additions & 1 deletion stats/view/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ func (c *collector) clearRows() {
// encodeWithKeys encodes the map by using values
// only associated with the keys provided.
func encodeWithKeys(m *tag.Map, keys []tag.Key) []byte {
// Compute the buffer length we will need ahead of time to avoid resizing later
reqLen := 0
for _, k := range keys {
s, _ := m.Value(k)
// We will store each key + its length
reqLen += len(s) + 1
}
vb := &tagencoding.Values{
Buffer: make([]byte, len(keys)),
Buffer: make([]byte, reqLen),
}
for _, k := range keys {
v, _ := m.Value(k)
Expand Down