From 146021a07604672a61b41726be5e7f6fd9a5b86e Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 30 Sep 2022 10:48:22 +0300 Subject: [PATCH] lib/promrelabel: there is no need in calling regex.HasPrefix() after the optimization at 17289ff4813d255eef72bd6704ad21fea66d1b01 --- lib/promrelabel/relabel.go | 12 ++++++------ lib/regexutil/promregex.go | 8 -------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/promrelabel/relabel.go b/lib/promrelabel/relabel.go index f0a9b760d..32e65e513 100644 --- a/lib/promrelabel/relabel.go +++ b/lib/promrelabel/relabel.go @@ -205,7 +205,8 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset return labels } } - if re := prc.regex; re.HasPrefix() && !re.MatchString(bytesutil.ToUnsafeString(bb.B)) { + sourceStr := bytesutil.ToUnsafeString(bb.B) + if !prc.regex.MatchString(sourceStr) { // Fast path - regexp mismatch. relabelBufPool.Put(bb) return labels @@ -216,7 +217,6 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset relabelBufPool.Put(bb) return labels } - sourceStr := bytesutil.ToUnsafeString(bb.B) nameStr := prc.TargetLabel if prc.hasCaptureGroupInTargetLabel { nameStr = prc.expandCaptureGroups(nameStr, sourceStr, match) @@ -401,6 +401,10 @@ func (prc *parsedRelabelConfig) replaceFullStringFast(s string) string { } } } + if !prc.regex.MatchString(s) { + // Fast path - regex mismatch + return s + } // Slow path - handle the rest of cases. return prc.stringReplacer.Transform(s) } @@ -409,10 +413,6 @@ func (prc *parsedRelabelConfig) replaceFullStringFast(s string) string { // // s is returned as is if it doesn't match '^regex$'. func (prc *parsedRelabelConfig) replaceFullStringSlow(s string) string { - if re := prc.regex; re.HasPrefix() && !re.MatchString(s) { - // Fast path - regex mismatch - return s - } // Slow path - regexp processing match := prc.RegexAnchored.FindStringSubmatchIndex(s) if match == nil { diff --git a/lib/regexutil/promregex.go b/lib/regexutil/promregex.go index 29ec622f3..cf196b8cd 100644 --- a/lib/regexutil/promregex.go +++ b/lib/regexutil/promregex.go @@ -65,14 +65,6 @@ func NewPromRegex(expr string) (*PromRegex, error) { return pr, nil } -// HasPrefix returns true if pr contains non-empty literal prefix. -// -// For example, if pr is "foo(bar|baz)", then the prefix is "foo", -// so HasPrefix() returns true. -func (pr *PromRegex) HasPrefix() bool { - return len(pr.prefix) > 0 -} - // MatchString retruns true if s matches pr. // // The pr is automatically anchored to the beginning and to the end