From 0cf18a6f636ba46cd8689a38978b8650c6a33454 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 15 Jul 2024 23:59:13 +0200 Subject: [PATCH] lib/uint64set: optimize Set.Has() for nil Set - it should be inlined now This makes unnecessary the checkDeleted variable at lib/storage/index_db.go This is a follow-up for b984f4672e4993f60562a482e18dae86262500fc Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6342 --- lib/storage/index_db.go | 19 +++++++------------ lib/uint64set/uint64set.go | 4 ++++ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index b4078fa7f..e2e9bcc07 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -679,7 +679,7 @@ func (is *indexSearch) searchLabelNamesWithFiltersOnDate(qt *querytracer.Tracer, if filter != nil && filter.Len() <= 100e3 { // It is faster to obtain label names by metricIDs from the filter // instead of scanning the inverted index for the matching filters. - // This would help https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2978 + // This should help https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2978 metricIDs := filter.AppendTo(nil) qt.Printf("sort %d metricIDs", len(metricIDs)) is.getLabelNamesForMetricIDs(qt, metricIDs, lns, maxLabelNames) @@ -772,13 +772,12 @@ func (is *indexSearch) getLabelNamesForMetricIDs(qt *querytracer.Tracer, metricI } dmis := is.db.s.getDeletedMetricIDs() - checkDeleted := dmis.Len() > 0 var mn MetricName foundLabelNames := 0 var buf []byte for _, metricID := range metricIDs { - if checkDeleted && dmis.Has(metricID) { + if dmis.Has(metricID) { // skip deleted IDs from result continue } @@ -1033,7 +1032,7 @@ func (is *indexSearch) searchLabelValuesWithFiltersOnDate(qt *querytracer.Tracer if filter != nil && filter.Len() <= 100e3 { // It is faster to obtain label values by metricIDs from the filter // instead of scanning the inverted index for the matching filters. - // This would help https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2978 + // This should help https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2978 metricIDs := filter.AppendTo(nil) qt.Printf("sort %d metricIDs", len(metricIDs)) is.getLabelValuesForMetricIDs(qt, lvs, labelName, metricIDs, maxLabelValues) @@ -1107,13 +1106,12 @@ func (is *indexSearch) getLabelValuesForMetricIDs(qt *querytracer.Tracer, lvs ma } dmis := is.db.s.getDeletedMetricIDs() - checkDeleted := dmis.Len() > 0 var mn MetricName foundLabelValues := 0 var buf []byte for _, metricID := range metricIDs { - if checkDeleted && dmis.Has(metricID) { + if dmis.Has(metricID) { // skip deleted IDs from result continue } @@ -2013,12 +2011,9 @@ func (is *indexSearch) getTSIDByMetricNameNoExtDB(dst *TSID, metricName []byte, if len(tail) > 0 { logger.Panicf("FATAL: unexpected non-empty tail left after unmarshaling TSID: %X", tail) } - if dmis.Len() > 0 { - // Verify whether the dst is marked as deleted. - if dmis.Has(dst.MetricID) { - // The dst is deleted. Continue searching. - continue - } + if dmis.Has(dst.MetricID) { + // The dst is deleted. Continue searching. + continue } // Found valid dst. return true diff --git a/lib/uint64set/uint64set.go b/lib/uint64set/uint64set.go index edd8bf042..95fee3ee0 100644 --- a/lib/uint64set/uint64set.go +++ b/lib/uint64set/uint64set.go @@ -183,6 +183,10 @@ func (s *Set) Has(x uint64) bool { if s == nil { return false } + return s.hasSlow(x) +} + +func (s *Set) hasSlow(x uint64) bool { hi32 := uint32(x >> 32) lo32 := uint32(x) bs := s.buckets