Browse Source

Prevent most allocations in ExpDecaySample

This reduces allocations to just what take place when emitting metrics
to other services.
Richard Crowley 10 năm trước cách đây
mục cha
commit
a5cfc242a5
1 tập tin đã thay đổi với 6 bổ sung2 xóa
  1. 6 2
      sample.go

+ 6 - 2
sample.go

@@ -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
@@ -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 {
@@ -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]