From 0c05f95e929f1b62f500c9ed4a806a768acc34aa Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 26 Nov 2022 14:56:22 +0000 Subject: [PATCH] tsdb: use smaller allocation in blockBaseSeriesSet This reduces garbage, hence goes faster, when a short time range is required compared to the amount of chunks in the block. For example recording rules and alerts often look only at the last few minutes. Signed-off-by: Bryan Boreham --- tsdb/querier.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tsdb/querier.go b/tsdb/querier.go index 70b384e02..cf6b3c9dc 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -471,7 +471,14 @@ func (b *blockBaseSeriesSet) Next() bool { var trimFront, trimBack bool // Copy chunks as iterables are reusable. - chks := make([]chunks.Meta, 0, len(b.bufChks)) + // Count those in range to size allocation (roughly - ignoring tombstones). + nChks := 0 + for _, chk := range b.bufChks { + if !(chk.MaxTime < b.mint || chk.MinTime > b.maxt) { + nChks++ + } + } + chks := make([]chunks.Meta, 0, nChks) // Prefilter chunks and pick those which are not entirely deleted or totally outside of the requested range. for _, chk := range b.bufChks {