all: use metricsql.CompileRegexp instead of regexp.Compile for compiling regexps used in graphite queries

This should speed up repeated queries, since metricsql.CompileRegexp returns regexps from the cache
on subsequent calls for the same input regexp.
This commit is contained in:
Aliaksandr Valialkin 2023-01-09 21:43:04 -08:00
parent 67ab49baa9
commit 7afcca0c51
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 6 additions and 4 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
"github.com/VictoriaMetrics/metrics"
"github.com/VictoriaMetrics/metricsql"
)
var maxTagValueSuffixes = flag.Int("search.maxTagValueSuffixesPerSearch", 100e3, "The maximum number of tag value suffixes returned from /metrics/find")
@ -349,7 +350,7 @@ func getRegexpForQuery(query string, delimiter byte) (*regexp.Regexp, error) {
if len(tail) > 0 {
return nil, fmt.Errorf("unexpected tail left after parsing query %q; tail: %q", query, tail)
}
re, err := regexp.Compile(rs)
re, err := metricsql.CompileRegexp(rs)
regexpCache[k] = &regexpCacheEntry{
re: re,
err: err,

View file

@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"math/rand"
"regexp"
"sort"
"sync"
"sync/atomic"
@ -21,6 +20,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/querytracer"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
"github.com/VictoriaMetrics/metrics"
"github.com/VictoriaMetrics/metricsql"
"github.com/valyala/fastrand"
)
@ -1176,7 +1176,7 @@ func applyGraphiteRegexpFilter(filter string, ss []string) ([]string, error) {
// Anchor filter regexp to the beginning of the string as Graphite does.
// See https://github.com/graphite-project/graphite-web/blob/3ad279df5cb90b211953e39161df416e54a84948/webapp/graphite/tags/localdatabase.py#L157
filter = "^(?:" + filter + ")"
re, err := regexp.Compile(filter)
re, err := metricsql.CompileRegexp(filter)
if err != nil {
return nil, fmt.Errorf("cannot parse regexp filter=%q: %w", filter, err)
}

View file

@ -30,6 +30,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/uint64set"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/workingsetcache"
"github.com/VictoriaMetrics/fastcache"
"github.com/VictoriaMetrics/metricsql"
)
const (
@ -1362,7 +1363,7 @@ func getRegexpForGraphiteQuery(q string) (*regexp.Regexp, error) {
return nil, fmt.Errorf("unexpected tail left after parsing %q: %q", q, tail)
}
reStr := "^" + strings.Join(parts, "") + "$"
return regexp.Compile(reStr)
return metricsql.CompileRegexp(reStr)
}
func getRegexpPartsForGraphiteQuery(q string) ([]string, string) {