Skip to content

Commit 45effea

Browse files
committed
remove bin and sum
1 parent b979930 commit 45effea

File tree

2 files changed

+16
-73
lines changed

2 files changed

+16
-73
lines changed

sdk/metric/internal/aggregate/histogram.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ type histogramPointCounters[N int64 | float64] struct {
3939
minMax atomicMinMax[N]
4040
}
4141

42-
func (b *histogramPointCounters[N]) sum(value N) { b.total.add(value) }
43-
44-
func (b *histogramPointCounters[N]) bin(bounds []float64, value N) {
45-
// This search will return an index in the range [0, len(s.bounds)], where
46-
// it will return len(s.bounds) if value is greater than the last element
47-
// of s.bounds. This aligns with the histogramPoint in that the length of histogramPoint
48-
// is len(s.bounds)+1, with the last bucket representing:
49-
// (s.bounds[len(s.bounds)-1], +∞).
50-
idx := sort.SearchFloat64s(bounds, float64(value))
51-
b.counts[idx].Add(1)
52-
}
53-
5442
func (b *histogramPointCounters[N]) loadCountsInto(into *[]uint64) uint64 {
5543
// TODO (#3047): Making copies for bounds and counts incurs a large
5644
// memory allocation footprint. Alternatives should be explored.
@@ -138,12 +126,18 @@ func (s *deltaHistogram[N]) measure(
138126
return hPt
139127
}).(*histogramPoint[N])
140128

141-
h.bin(s.bounds, value)
129+
// This search will return an index in the range [0, len(s.bounds)], where
130+
// it will return len(s.bounds) if value is greater than the last element
131+
// of s.bounds. This aligns with the histogramPoint in that the length of histogramPoint
132+
// is len(s.bounds)+1, with the last bucket representing:
133+
// (s.bounds[len(s.bounds)-1], +∞).
134+
idx := sort.SearchFloat64s(s.bounds, float64(value))
135+
h.counts[idx].Add(1)
142136
if !s.noMinMax {
143137
h.minMax.Update(value)
144138
}
145139
if !s.noSum {
146-
h.sum(value)
140+
h.total.add(value)
147141
}
148142
h.res.Offer(ctx, value, droppedAttr)
149143
}
@@ -311,12 +305,18 @@ func (s *cumulativeHistogram[N]) measure(
311305
hotIdx := h.hcwg.start()
312306
defer h.hcwg.done(hotIdx)
313307

314-
h.hotColdPoint[hotIdx].bin(s.bounds, value)
308+
// This search will return an index in the range [0, len(s.bounds)], where
309+
// it will return len(s.bounds) if value is greater than the last element
310+
// of s.bounds. This aligns with the histogramPoint in that the length of histogramPoint
311+
// is len(s.bounds)+1, with the last bucket representing:
312+
// (s.bounds[len(s.bounds)-1], +∞).
313+
idx := sort.SearchFloat64s(s.bounds, float64(value))
314+
h.hotColdPoint[hotIdx].counts[idx].Add(1)
315315
if !s.noMinMax {
316316
h.hotColdPoint[hotIdx].minMax.Update(value)
317317
}
318318
if !s.noSum {
319-
h.hotColdPoint[hotIdx].sum(value)
319+
h.hotColdPoint[hotIdx].total.add(value)
320320
}
321321
h.res.Offer(ctx, value, droppedAttr)
322322
}

sdk/metric/internal/aggregate/histogram_test.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -339,63 +339,6 @@ func hPoint[N int64 | float64](
339339
}
340340
}
341341

342-
func TestBucketsBin(t *testing.T) {
343-
t.Run("Int64", testBucketsBin[int64]())
344-
t.Run("Float64", testBucketsBin[float64]())
345-
}
346-
347-
func testBucketsBin[N int64 | float64]() func(t *testing.T) {
348-
return func(t *testing.T) {
349-
b := newHistogramPoint[N](alice, 3)
350-
assertB := func(expectedBucketCounts []uint64, expectedCount uint64, mi, ma N) {
351-
t.Helper()
352-
var bucketCounts []uint64
353-
count := b.loadCountsInto(&bucketCounts)
354-
assert.Equal(t, expectedBucketCounts, bucketCounts)
355-
assert.Equal(t, expectedCount, count)
356-
if mi != 0 {
357-
assert.True(t, b.minMax.set.Load())
358-
assert.Equal(t, mi, b.minMax.minimum.Load())
359-
}
360-
if ma != 0 {
361-
assert.True(t, b.minMax.set.Load())
362-
assert.Equal(t, ma, b.minMax.maximum.Load())
363-
}
364-
}
365-
366-
bounds := []float64{0, 2, 4}
367-
assertB([]uint64{0, 0, 0}, 0, 0, 0)
368-
b.bin(bounds, 1)
369-
b.minMax.Update(2)
370-
assertB([]uint64{0, 1, 0}, 1, 0, 2)
371-
b.bin(bounds, -1)
372-
b.minMax.Update(-1)
373-
assertB([]uint64{1, 1, 0}, 2, -1, 2)
374-
}
375-
}
376-
377-
func TestBucketsSum(t *testing.T) {
378-
t.Run("Int64", testBucketsSum[int64]())
379-
t.Run("Float64", testBucketsSum[float64]())
380-
}
381-
382-
func testBucketsSum[N int64 | float64]() func(t *testing.T) {
383-
return func(t *testing.T) {
384-
b := newHistogramPoint[N](alice, 3)
385-
386-
var want N
387-
assert.Equal(t, want, b.total.load())
388-
389-
b.sum(2)
390-
want = 2
391-
assert.Equal(t, want, b.total.load())
392-
393-
b.sum(-1)
394-
want = 1
395-
assert.Equal(t, want, b.total.load())
396-
}
397-
}
398-
399342
func TestHistogramImmutableBounds(t *testing.T) {
400343
b := []float64{0, 1, 2}
401344
cpB := make([]float64, len(b))

0 commit comments

Comments
 (0)