Explorar el Código

Prevent most allocations in ExpDecaySample

This reduces allocations to just what take place when emitting metrics
to other services.
Richard Crowley hace 11 años
padre
commit
a5cfc242a5
Se han modificado 1 ficheros con 6 adiciones y 2 borrados
  1. 6 2
      sample.go

+ 6 - 2
sample.go

@@ -66,7 +66,7 @@ func (s *ExpDecaySample) Clear() {
 	s.count = 0
 	s.count = 0
 	s.t0 = time.Now()
 	s.t0 = time.Now()
 	s.t1 = s.t0.Add(rescaleThreshold)
 	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
 // 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) {
 	if t.After(s.t1) {
 		values := s.values.Values()
 		values := s.values.Values()
 		t0 := s.t0
 		t0 := s.t0
-		s.values = newExpDecaySampleHeap(s.reservoirSize)
+		s.values.Clear()
 		s.t0 = t
 		s.t0 = t
 		s.t1 = s.t0.Add(rescaleThreshold)
 		s.t1 = s.t0.Add(rescaleThreshold)
 		for _, v := range values {
 		for _, v := range values {
@@ -543,6 +543,10 @@ type expDecaySampleHeap struct {
 	s []expDecaySample
 	s []expDecaySample
 }
 }
 
 
+func (h *expDecaySampleHeap) Clear() {
+	h.s = h.s[:0]
+}
+
 func (h *expDecaySampleHeap) Push(s expDecaySample) {
 func (h *expDecaySampleHeap) Push(s expDecaySample) {
 	n := len(h.s)
 	n := len(h.s)
 	h.s = h.s[0 : n+1]
 	h.s = h.s[0 : n+1]