From 402d1d7c48ab4eb77835f4ebb9ef7cabf1dd7449 Mon Sep 17 00:00:00 2001 From: Dylan Guedes Date: Fri, 19 Apr 2024 04:56:03 -0300 Subject: [PATCH] perf: TSDB: Add fast-path to `inversePostingsForMatcher` (#12679) --- .../stores/shipper/indexshipper/tsdb/querier.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/storage/stores/shipper/indexshipper/tsdb/querier.go b/pkg/storage/stores/shipper/indexshipper/tsdb/querier.go index 48de47a70c3b..b29556c348cf 100644 --- a/pkg/storage/stores/shipper/indexshipper/tsdb/querier.go +++ b/pkg/storage/stores/shipper/indexshipper/tsdb/querier.go @@ -217,6 +217,22 @@ func postingsForMatcher(ix IndexReader, fpFilter index.FingerprintFilter, m *lab // inversePostingsForMatcher returns the postings for the series with the label name set but not matching the matcher. func inversePostingsForMatcher(ix IndexReader, fpFilter index.FingerprintFilter, m *labels.Matcher) (index.Postings, error) { + // Fast-path for MatchNotRegexp matching. + // Inverse of a MatchNotRegexp is MatchRegexp (double negation). + // Fast-path for set matching. + if m.Type == labels.MatchNotRegexp { + setMatches := findSetMatches(m.GetRegexString()) + if len(setMatches) > 0 { + return ix.Postings(m.Name, fpFilter, setMatches...) + } + } + + // Fast-path for MatchNotEqual matching. + // Inverse of a MatchNotEqual is MatchEqual (double negation). + if m.Type == labels.MatchNotEqual { + return ix.Postings(m.Name, fpFilter, m.Value) + } + vals, err := ix.LabelValues(m.Name) if err != nil { return nil, err