Skip to content

Commit

Permalink
Optimize: do not recalc length of input buckets.
Browse files Browse the repository at this point in the history
Saves about 10%.


Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama authored and jpkrohling committed Jul 12, 2023
1 parent c1e8359 commit f9475bc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions pkg/translator/prometheusremotewrite/histograms.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,21 @@ func convertBucketsLayout(buckets pmetric.ExponentialHistogramDataPointBuckets,
prevCount = count
}

// Let the compiler figure out that this is const during this function by
// moving it into a local variable.
bucketsCount := bucketCounts.Len()

// The offset is scaled and adjusted by 1 as described above.
bucketIdx := buckets.Offset()>>scaleDown + 1
spans = append(spans, prompb.BucketSpan{
Offset: bucketIdx,
Length: 0,
})

for i := 0; i < bucketCounts.Len(); i++ {
for i := 0; i < bucketsCount; i++ {
// The offset is scaled and adjusted by 1 as described above.
nextBucketIdx = (int32(i)+buckets.Offset())>>scaleDown + 1
if bucketIdx == nextBucketIdx { // we have not collected enough buckets to merge yet
if bucketIdx == nextBucketIdx { // We have not collected enough buckets to merge yet.
count += int64(bucketCounts.At(i))
continue
}
Expand All @@ -152,7 +156,7 @@ func convertBucketsLayout(buckets pmetric.ExponentialHistogramDataPointBuckets,

gap := nextBucketIdx - bucketIdx - 1
if gap > 2 {
// We have to create a new span, either because we have found a gap
// We have to create a new span, because we have found a gap
// of more than two buckets. The constant 2 is copied from the logic in
// https://github.com/prometheus/client_golang/blob/27f0506d6ebbb117b6b697d0552ee5be2502c5f2/prometheus/histogram.go#L1296
spans = append(spans, prompb.BucketSpan{
Expand All @@ -170,7 +174,7 @@ func convertBucketsLayout(buckets pmetric.ExponentialHistogramDataPointBuckets,
count = int64(bucketCounts.At(i))
bucketIdx = nextBucketIdx
}
// nextBucketIdx is the last index, not the next one, hence no need to deduct 1
// nextBucketIdx is the last index, not the next one, hence no need to deduct 1.
gap := nextBucketIdx - bucketIdx
if gap > 2 {
// We have to create a new span, because we have found a gap
Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/prometheusremotewrite/histograms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func BenchmarkConvertBucketLayout(b *testing.B) {
}
b.Run(fmt.Sprintf("gap %d", scenario.gap), func(b *testing.B) {
for i := 0; i < b.N; i++ {
convertBucketsLayout(buckets)
convertBucketsLayout(buckets, 0)
}
})
}
Expand Down

0 comments on commit f9475bc

Please sign in to comment.