Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit a5cfc24

Browse files
committed
Prevent most allocations in ExpDecaySample
This reduces allocations to just what take place when emitting metrics to other services.
1 parent add548c commit a5cfc24

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

sample.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *ExpDecaySample) Clear() {
6666
s.count = 0
6767
s.t0 = time.Now()
6868
s.t1 = s.t0.Add(rescaleThreshold)
69-
s.values = newExpDecaySampleHeap(s.reservoirSize)
69+
s.values.Clear()
7070
}
7171

7272
// Count returns the number of samples recorded, which may exceed the
@@ -175,7 +175,7 @@ func (s *ExpDecaySample) update(t time.Time, v int64) {
175175
if t.After(s.t1) {
176176
values := s.values.Values()
177177
t0 := s.t0
178-
s.values = newExpDecaySampleHeap(s.reservoirSize)
178+
s.values.Clear()
179179
s.t0 = t
180180
s.t1 = s.t0.Add(rescaleThreshold)
181181
for _, v := range values {
@@ -543,6 +543,10 @@ type expDecaySampleHeap struct {
543543
s []expDecaySample
544544
}
545545

546+
func (h *expDecaySampleHeap) Clear() {
547+
h.s = h.s[:0]
548+
}
549+
546550
func (h *expDecaySampleHeap) Push(s expDecaySample) {
547551
n := len(h.s)
548552
h.s = h.s[0 : n+1]

0 commit comments

Comments
 (0)