Skip to content

Commit

Permalink
Rename buckets operations
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed May 30, 2023
1 parent f764955 commit a02fa67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions output/cloud/expv2/hdr.go
Expand Up @@ -125,9 +125,9 @@ func (h *histogram) addToBucket(v float64) {
case len(h.Buckets) == 0:
h.init(index)
case index < h.FirstNotZeroBucket:
h.growLeft(index)
h.prependBuckets(index)
case index > h.LastNotZeroBucket:
h.growRight(index)
h.appendBuckets(index)
default:
h.Buckets[index-h.FirstNotZeroBucket]++
}
Expand All @@ -140,7 +140,9 @@ func (h *histogram) init(index uint32) {
h.Buckets[0] = 1
}

func (h *histogram) growLeft(index uint32) {
// prependBuckets expands the buckets slice with zeros up to the required index,
// then it increments the required bucket.
func (h *histogram) prependBuckets(index uint32) {
if h.FirstNotZeroBucket <= index {
panic("buckets is already contains the requested index")
}
Expand All @@ -160,10 +162,10 @@ func (h *histogram) growLeft(index uint32) {
h.FirstNotZeroBucket = index
}

// growRight expands the buckets slice
// with zeros up to the required index.
// If it array has enough capacity then it reuses it without allocate.
func (h *histogram) growRight(index uint32) {
// appendBuckets expands the buckets slice with zeros buckets till the required index,
// then it increments the required bucket.
// If the slice has enough capacity then it reuses it without allocate.
func (h *histogram) appendBuckets(index uint32) {
if h.LastNotZeroBucket >= index {
panic("buckets is already bigger than requested index")
}
Expand Down
8 changes: 4 additions & 4 deletions output/cloud/expv2/hdr_test.go
Expand Up @@ -233,28 +233,28 @@ func TestNewHistoramWithNoVals(t *testing.T) {
assert.Equal(t, exp, res)
}

func TestHistogramGrowRight(t *testing.T) {
func TestHistogramAppendBuckets(t *testing.T) {
t.Parallel()
h := histogram{}

// the cap is smaller than requested index
// so it creates a new slice
h.growRight(3)
h.appendBuckets(3)
assert.Len(t, h.Buckets, 4)

// it must preserve already existing items
h.Buckets[2] = 101

// it appends to the same slice
h.growRight(5)
h.appendBuckets(5)
assert.Len(t, h.Buckets, 6)
assert.Equal(t, uint32(101), h.Buckets[2])
assert.Equal(t, uint32(1), h.Buckets[5])

// it is not possible to request an index smaller than
// the last already available index
h.LastNotZeroBucket = 5
assert.Panics(t, func() { h.growRight(4) })
assert.Panics(t, func() { h.appendBuckets(4) })
}

func TestHistogramAsProto(t *testing.T) {
Expand Down

0 comments on commit a02fa67

Please sign in to comment.