ソースを参照

Prevent most allocations in ExpDecaySample

This reduces allocations to just what take place when emitting metrics
to other services.
Richard Crowley 10 年 前
コミット
a5cfc242a5
1 ファイル変更6 行追加2 行削除
  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]