From add2c4bf078ce67ac3c80a8cf9b35760daa1ceec Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 3 Jan 2023 22:14:20 -0800 Subject: [PATCH] lib/bytesutil: add InternBytes() function as a shortcut to InternString(ToUnsafeString(..)) --- lib/bytesutil/internstring.go | 6 ++++++ lib/bytesutil/itoa.go | 2 +- lib/promrelabel/relabel.go | 14 +++++++------- lib/promscrape/client.go | 3 +-- lib/promscrape/config.go | 3 +-- lib/promscrape/discoveryutils/utils.go | 3 +-- lib/promscrape/scrapework.go | 2 +- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/bytesutil/internstring.go b/lib/bytesutil/internstring.go index 138b00795..f14a8527f 100644 --- a/lib/bytesutil/internstring.go +++ b/lib/bytesutil/internstring.go @@ -8,6 +8,12 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime" ) +// InternBytes interns b as a string +func InternBytes(b []byte) string { + s := ToUnsafeString(b) + return InternString(s) +} + // InternString returns interned s. // // This may be needed for reducing the amounts of allocated memory. diff --git a/lib/bytesutil/itoa.go b/lib/bytesutil/itoa.go index 254db8d3c..d0aed39b9 100644 --- a/lib/bytesutil/itoa.go +++ b/lib/bytesutil/itoa.go @@ -11,7 +11,7 @@ func Itoa(n int) string { bb := bbPool.Get() b := bb.B[:0] b = strconv.AppendInt(b, int64(n), 10) - s := InternString(ToUnsafeString(b)) + s := InternBytes(b) bb.B = b bbPool.Put(bb) return s diff --git a/lib/promrelabel/relabel.go b/lib/promrelabel/relabel.go index 3492074ac..9c55d76a3 100644 --- a/lib/promrelabel/relabel.go +++ b/lib/promrelabel/relabel.go @@ -181,7 +181,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + valueStr := bytesutil.InternBytes(bb.B) labels = setLabelValue(labels, labelsOffset, gl.targetLabel, valueStr) } relabelBufPool.Put(bb) @@ -194,7 +194,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 = bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + replacement = bytesutil.InternBytes(bb.B) } bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator) if prc.RegexAnchored == defaultRegexForRelabelConfig && !prc.hasCaptureGroupInTargetLabel { @@ -202,7 +202,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + valueStr := bytesutil.InternBytes(bb.B) relabelBufPool.Put(bb) return setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr) } @@ -245,7 +245,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + sourceStr := bytesutil.InternBytes(bb.B) relabelBufPool.Put(bb) valueStr := prc.replaceStringSubmatchesFast(sourceStr) if valueStr != sourceStr { @@ -381,7 +381,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + valueStr := bytesutil.InternBytes(bb.B) relabelBufPool.Put(bb) valueStr = strings.ToUpper(valueStr) labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr) @@ -389,7 +389,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + valueStr := bytesutil.InternBytes(bb.B) relabelBufPool.Put(bb) valueStr = strings.ToLower(valueStr) labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr) @@ -478,7 +478,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + s := bytesutil.InternBytes(bb.B) relabelBufPool.Put(bb) return s } diff --git a/lib/promscrape/client.go b/lib/promscrape/client.go index 2be50051d..0a696f2d3 100644 --- a/lib/promscrape/client.go +++ b/lib/promscrape/client.go @@ -72,8 +72,7 @@ func concatTwoStrings(x, y string) string { b := bb.B[:0] b = append(b, x...) b = append(b, y...) - s := bytesutil.ToUnsafeString(b) - s = bytesutil.InternString(s) + s := bytesutil.InternBytes(b) bb.B = b bbPool.Put(bb) return s diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index fbee67759..1df85a8de 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -1338,8 +1338,7 @@ func getScrapeURL(scheme, address, metricsPath, optionalQuestion, paramsStr stri b = append(b, metricsPath...) b = append(b, optionalQuestion...) b = append(b, paramsStr...) - s := bytesutil.ToUnsafeString(b) - s = bytesutil.InternString(s) + s := bytesutil.InternBytes(b) bb.B = b bbPool.Put(bb) return s diff --git a/lib/promscrape/discoveryutils/utils.go b/lib/promscrape/discoveryutils/utils.go index 2a50a1971..23f0a4ca8 100644 --- a/lib/promscrape/discoveryutils/utils.go +++ b/lib/promscrape/discoveryutils/utils.go @@ -41,8 +41,7 @@ func JoinHostPort(host string, port int) string { } b = append(b, ':') b = strconv.AppendInt(b, int64(port), 10) - s := bytesutil.ToUnsafeString(b) - s = bytesutil.InternString(s) + s := bytesutil.InternBytes(b) bb.B = b bbPool.Put(bb) return s diff --git a/lib/promscrape/scrapework.go b/lib/promscrape/scrapework.go index 2a5f4dcc8..e18855e8b 100644 --- a/lib/promscrape/scrapework.go +++ b/lib/promscrape/scrapework.go @@ -848,7 +848,7 @@ func (sw *scrapeWork) addRowToTimeseries(wc *writeRequestCtx, r *parser.Row, tim bb := bbPool.Get() bb.B = append(bb.B, "exported_"...) bb.B = append(bb.B, metric...) - metric = bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) + metric = bytesutil.InternBytes(bb.B) bbPool.Put(bb) } labelsLen := len(wc.labels)