Skip to content

Commit

Permalink
perf: Add map pool for histogram merges (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonip authored Jul 28, 2023
1 parent 8bb2e5c commit fe23835
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion aggregators/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package aggregators

import (
"io"
"sync"

"github.com/axiomhq/hyperloglog"

Expand Down Expand Up @@ -406,9 +407,19 @@ func mergeSpanMetrics(to, from *aggregationpb.SpanMetrics) {
to.Sum += from.Sum
}

// mapPool is a pool of maps to facilitate histogram merges.
var mapPool = sync.Pool{New: func() interface{} {
return make(map[int32]int64)
}}

func mergeHistogram(to, from *aggregationpb.HDRHistogram) {
// Assume both histograms are created with same arguments
m := make(map[int32]int64)
m := mapPool.Get().(map[int32]int64)
defer mapPool.Put(m)
for k := range m {
delete(m, k)
}

for i := 0; i < len(to.Buckets); i++ {
m[to.Buckets[i]] = to.Counts[i]
}
Expand Down

0 comments on commit fe23835

Please sign in to comment.