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