lib/promrelabel: there is no need in calling regex.HasPrefix() after the optimization at 17289ff481

This commit is contained in:
Aliaksandr Valialkin 2022-09-30 10:48:22 +03:00
parent fa46c28c5f
commit f38c9db74d
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 6 additions and 14 deletions

View file

@ -205,7 +205,8 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
return labels 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. // Fast path - regexp mismatch.
relabelBufPool.Put(bb) relabelBufPool.Put(bb)
return labels return labels
@ -216,7 +217,6 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
relabelBufPool.Put(bb) relabelBufPool.Put(bb)
return labels return labels
} }
sourceStr := bytesutil.ToUnsafeString(bb.B)
nameStr := prc.TargetLabel nameStr := prc.TargetLabel
if prc.hasCaptureGroupInTargetLabel { if prc.hasCaptureGroupInTargetLabel {
nameStr = prc.expandCaptureGroups(nameStr, sourceStr, match) 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. // Slow path - handle the rest of cases.
return prc.stringReplacer.Transform(s) 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$'. // s is returned as is if it doesn't match '^regex$'.
func (prc *parsedRelabelConfig) replaceFullStringSlow(s string) string { 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 // Slow path - regexp processing
match := prc.RegexAnchored.FindStringSubmatchIndex(s) match := prc.RegexAnchored.FindStringSubmatchIndex(s)
if match == nil { if match == nil {

View file

@ -65,14 +65,6 @@ func NewPromRegex(expr string) (*PromRegex, error) {
return pr, nil 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. // MatchString retruns true if s matches pr.
// //
// The pr is automatically anchored to the beginning and to the end // The pr is automatically anchored to the beginning and to the end