From a64b0d51c4da614efb493627a7b5425bc65c6769 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 31 Oct 2018 13:28:56 +0000 Subject: [PATCH] Precalculate memSeries.head This is read far more than it changes. This cuts ~14% off walltme and ~27% off CPU for WAL reading. Signed-off-by: Brian Brazil --- head.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/head.go b/head.go index 894febb21..690489b83 100644 --- a/head.go +++ b/head.go @@ -1338,6 +1338,7 @@ type memSeries struct { ref uint64 lset labels.Labels chunks []*memChunk + headChunk *memChunk chunkRange int64 firstChunkID int @@ -1371,6 +1372,7 @@ func (s *memSeries) cut(mint int64) *memChunk { maxTime: math.MinInt64, } s.chunks = append(s.chunks, c) + s.headChunk = c // Set upper bound on when the next chunk must be started. An earlier timestamp // may be chosen dynamically at a later point. @@ -1439,6 +1441,11 @@ func (s *memSeries) truncateChunksBefore(mint int64) (removed int) { } s.chunks = append(s.chunks[:0], s.chunks[k:]...) s.firstChunkID += k + if len(s.chunks) == 0 { + s.headChunk = nil + } else { + s.headChunk = s.chunks[len(s.chunks)-1] + } return k } @@ -1521,10 +1528,7 @@ func (s *memSeries) iterator(id int) chunkenc.Iterator { } func (s *memSeries) head() *memChunk { - if len(s.chunks) == 0 { - return nil - } - return s.chunks[len(s.chunks)-1] + return s.headChunk } type memChunk struct {