Skip to content
Open
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
12 changes: 9 additions & 3 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (h *histogram) String() string {
func (h *histogram) Reset() {
h.Lock()
defer h.Unlock()
h.bins = nil
h.bins = h.bins[:0]
h.total = 0
}

Expand All @@ -269,7 +269,12 @@ func (h *histogram) Add(n float64) {
newbin := bin{value: n, count: 1}
for i := range h.bins {
if h.bins[i].value > n {
h.bins = append(h.bins[:i], append([]bin{newbin}, h.bins[i:]...)...)
// heap optimization, append empty slot and rotate. As maxBins is small, it's much cheaper than gc
h.bins = append(h.bins, bin{})

// shift elements to make space for newbin
copy(h.bins[i+1:], h.bins[i:])
h.bins[i] = newbin
return
}
}
Expand Down Expand Up @@ -303,7 +308,8 @@ func (h *histogram) trim() {
value: (h.bins[i-1].value*h.bins[i-1].count + h.bins[i].value*h.bins[i].count) / count,
count: count,
}
h.bins = append(h.bins[:i-1], h.bins[i:]...)
copy(h.bins[i-1:], h.bins[i:]) // it's faster to copy than to allocate new slices
h.bins = h.bins[:len(h.bins)-1]
h.bins[i-1] = merged
}
}
Expand Down