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: Add mergeHistogram fast path #66

Closed
wants to merge 9 commits into from
Closed
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
57 changes: 50 additions & 7 deletions aggregators/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package aggregators

import (
"io"
"sort"

"github.com/axiomhq/hyperloglog"
"github.com/cespare/xxhash/v2"
Expand Down Expand Up @@ -384,6 +385,7 @@ func mergeSpanMetrics(to, from *aggregationpb.SpanMetrics) {
// mergeHistogram merges two proto representation of HDRHistogram. The
// merge assumes both histograms are created with same arguments and
// their representations are sorted by bucket.
// Caution: this function may mutate from.Counts.
func mergeHistogram(to, from *aggregationpb.HDRHistogram) {
if len(from.Buckets) == 0 {
return
Expand All @@ -395,8 +397,49 @@ func mergeHistogram(to, from *aggregationpb.HDRHistogram) {
return
}

requiredLen := len(to.Buckets) + len(from.Buckets)
for toIdx, fromIdx := 0, 0; toIdx < len(to.Buckets) && fromIdx < len(from.Buckets); {
toLen, fromLen := len(to.Buckets), len(from.Buckets)
// Heuristics to decide whether to use the fast path.
if fromLen < toLen && from.Buckets[0] >= to.Buckets[0] && from.Buckets[fromLen-1] <= to.Buckets[toLen-1] {
// Fast path to optimize for cases where len(from.Buckets) << len(to.Buckets)
// Binary search for all from.Buckets in to.Buckets for fewer comparisons,
// mergeHistogram will be O(m lg n) where m = fromLen and n = toLen.
searchToLen := toLen
var fallback bool
for fromIdx := fromLen - 1; fromIdx >= 0; fromIdx-- {
// Instead of searching in to.Buckets[0:toLen] each time,
// make use of the result of the previous pass since from.Buckets[i] > from.Buckets[i-1],
// such that the search space can be reduced to to.Buckets[0:searchToLen].
toIdx, found := sort.Find(searchToLen, func(toIdx int) int {
return int(from.Buckets[fromIdx] - to.Buckets[toIdx])
})
if !found {
fallback = true
break
}

to.Counts[toIdx] += from.Counts[fromIdx]
from.Counts[fromIdx] = 0
// Invariants:
// to.Buckets[toIdx] == from.Buckets[fromIdx] (because we fallback immediately when not found)
// from.Buckets[i-1] < from.Buckets[i] (buckets are strictly increasing)
// to.Buckets[i-1] < to.Buckets[i] (buckets are strictly increasing)
//
// Therefore:
// from.Buckets[fromIdx-1] < to.Buckets[toIdx]
// In the next pass, we can safely search in to.Buckets[0:toIdx], i.e. calling sort.Find(toIdx, ...).
// Edge case: where from.Buckets[fromIdx-1] > to.Buckets[toIdx-1], sort.Find will return (toIdx, false).
searchToLen = toIdx
}
if !fallback {
// from.Buckets is a subset of to.Buckets.
// No further merging is needed.
return
}
}

// Determine the number of extra buckets needed so we can grow the slice in one go.
requiredLen := toLen + fromLen
for toIdx, fromIdx := 0, 0; toIdx < toLen && fromIdx < fromLen; {
v := to.Buckets[toIdx] - from.Buckets[fromIdx]
switch {
case v == 0:
Expand All @@ -410,12 +453,12 @@ func mergeHistogram(to, from *aggregationpb.HDRHistogram) {
fromIdx++
}
}
extra := requiredLen - toLen

toIdx, fromIdx := len(to.Buckets)-1, len(from.Buckets)-1
to.Buckets = slices.Grow(to.Buckets, requiredLen-len(to.Buckets))
to.Counts = slices.Grow(to.Counts, requiredLen-len(to.Counts))
to.Buckets = to.Buckets[:requiredLen]
to.Counts = to.Counts[:requiredLen]
// Merge the slices.
toIdx, fromIdx := toLen-1, fromLen-1
to.Buckets = slices.Grow(to.Buckets, extra)[:toLen+extra]
to.Counts = slices.Grow(to.Counts, extra)[:toLen+extra]
for idx := len(to.Buckets) - 1; idx >= 0; idx-- {
if fromIdx < 0 {
break
Expand Down
24 changes: 12 additions & 12 deletions aggregators/merger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,31 +1147,31 @@ func TestMergeHistogram(t *testing.T) {
{
name: "from_between_to",
to: &aggregationpb.HDRHistogram{
Buckets: []int32{1000, 3000},
Counts: []int64{1, 3},
Buckets: []int32{1000, 4000, 5000},
Counts: []int64{1, 4, 5},
},
from: &aggregationpb.HDRHistogram{
Buckets: []int32{2000},
Counts: []int64{2},
Buckets: []int32{2000, 3000},
Counts: []int64{2, 3},
},
expected: &aggregationpb.HDRHistogram{
Buckets: []int32{1000, 2000, 3000},
Counts: []int64{1, 2, 3},
Buckets: []int32{1000, 2000, 3000, 4000, 5000},
Counts: []int64{1, 2, 3, 4, 5},
},
},
{
name: "to_between_from",
to: &aggregationpb.HDRHistogram{
Buckets: []int32{2000},
Counts: []int64{2},
Buckets: []int32{2000, 3000},
Counts: []int64{2, 3},
},
from: &aggregationpb.HDRHistogram{
Buckets: []int32{1000, 3000},
Counts: []int64{1, 3},
Buckets: []int32{1000, 4000, 5000},
Counts: []int64{1, 4, 5},
},
expected: &aggregationpb.HDRHistogram{
Buckets: []int32{1000, 2000, 3000},
Counts: []int64{1, 2, 3},
Buckets: []int32{1000, 2000, 3000, 4000, 5000},
Counts: []int64{1, 2, 3, 4, 5},
},
},
{
Expand Down