From e8a64f6e7a09df2db93f0905be0e624d0289e4c1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 1 Oct 2022 11:50:21 +0300 Subject: [PATCH] lib/promrelabel: remove redundant memory allocations by using interned strings --- lib/promrelabel/relabel.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/promrelabel/relabel.go b/lib/promrelabel/relabel.go index e5433e032e..6d55f3b643 100644 --- a/lib/promrelabel/relabel.go +++ b/lib/promrelabel/relabel.go @@ -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 }