Skip to content

Commit

Permalink
Prevent most allocations in ExpDecaySample
Browse files Browse the repository at this point in the history
This reduces allocations to just what take place when emitting metrics
to other services.
  • Loading branch information
rcrowley committed Jun 1, 2015
1 parent add548c commit a5cfc24
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *ExpDecaySample) Clear() {
s.count = 0
s.t0 = time.Now()
s.t1 = s.t0.Add(rescaleThreshold)
s.values = newExpDecaySampleHeap(s.reservoirSize)
s.values.Clear()
}

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

func (h *expDecaySampleHeap) Clear() {
h.s = h.s[:0]
}

func (h *expDecaySampleHeap) Push(s expDecaySample) {
n := len(h.s)
h.s = h.s[0 : n+1]
Expand Down

0 comments on commit a5cfc24

Please sign in to comment.