lib/promrelabel: remove redundant memory allocations by using interned strings

This commit is contained in:
Aliaksandr Valialkin 2022-10-01 11:50:21 +03:00
parent 73dc17ef64
commit e8a64f6e7a
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1

View File

@ -172,7 +172,7 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
bb := relabelBufPool.Get()
for _, gl := range prc.graphiteLabelRules {
bb.B = gl.grt.Expand(bb.B[:0], gm.a)
valueStr := string(bb.B)
valueStr := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
labels = setLabelValue(labels, labelsOffset, gl.targetLabel, valueStr)
}
relabelBufPool.Put(bb)
@ -185,7 +185,7 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
if prc.hasLabelReferenceInReplacement {
// Fill {{labelName}} references in the replacement
bb.B = fillLabelReferences(bb.B[:0], replacement, labels[labelsOffset:])
replacement = string(bb.B)
replacement = bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
}
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
if prc.RegexAnchored == defaultRegexForRelabelConfig && !prc.hasCaptureGroupInTargetLabel {
@ -193,7 +193,7 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
// Fast path for the rule that copies source label values to destination:
// - source_labels: [...]
// target_label: foobar
valueStr := string(bb.B)
valueStr := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
relabelBufPool.Put(bb)
return setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
}
@ -236,7 +236,7 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
// and store the result at `target_label`
bb := relabelBufPool.Get()
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
sourceStr := string(bb.B)
sourceStr := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
relabelBufPool.Put(bb)
valueStr := prc.replaceStringSubmatchesFast(sourceStr)
if valueStr != sourceStr {
@ -350,7 +350,7 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
case "uppercase":
bb := relabelBufPool.Get()
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
valueStr := string(bb.B)
valueStr := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
relabelBufPool.Put(bb)
valueStr = strings.ToUpper(valueStr)
labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
@ -358,7 +358,7 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
case "lowercase":
bb := relabelBufPool.Get()
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
valueStr := string(bb.B)
valueStr := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
relabelBufPool.Put(bb)
valueStr = strings.ToLower(valueStr)
labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
@ -384,7 +384,7 @@ func (prc *parsedRelabelConfig) replaceFullStringFast(s string) string {
return s
}
if !strings.HasPrefix(s, prefix) {
// Fast path - s doesn't match literl prefix from regex
// Fast path - s doesn't match literal prefix from regex
return s
}
if replacement == "$1" {
@ -447,7 +447,7 @@ func (prc *parsedRelabelConfig) replaceStringSubmatchesSlow(s string) string {
func (prc *parsedRelabelConfig) expandCaptureGroups(template, source string, match []int) string {
bb := relabelBufPool.Get()
bb.B = prc.RegexAnchored.ExpandString(bb.B[:0], template, source, match)
s := string(bb.B)
s := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B))
relabelBufPool.Put(bb)
return s
}