From d8c8e4e6e4f690c0bd7c165f7b9718fce58c165a Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 31 Oct 2018 12:51:21 +0000 Subject: [PATCH] Keep local cache of ids. With the various goroutines running, the locking in getByID is notable. This cuts cpu usage by ~25% and walltime by ~20%. Signed-off-by: Brian Brazil --- head.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/head.go b/head.go index e8794b66d..894febb21 100644 --- a/head.go +++ b/head.go @@ -242,6 +242,9 @@ func (h *Head) processWALSamples( ) (unknownRefs uint64) { defer close(output) + // Mitigate lock contention in getByID. + refSeries := map[uint64]*memSeries{} + mint, maxt := int64(math.MaxInt64), int64(math.MinInt64) for samples := range input { @@ -249,10 +252,14 @@ func (h *Head) processWALSamples( if s.T < minValidTime || s.Ref%total != partition { continue } - ms := h.series.getByID(s.Ref) + ms := refSeries[s.Ref] if ms == nil { - unknownRefs++ - continue + ms = h.series.getByID(s.Ref) + if ms == nil { + unknownRefs++ + continue + } + refSeries[s.Ref] = ms } _, chunkCreated := ms.append(s.T, s.V) if chunkCreated {