lib/bytesutil: add InternBytes() function as a shortcut to InternString(ToUnsafeString(..))

This commit is contained in:
Aliaksandr Valialkin 2023-01-03 22:14:20 -08:00
parent f33e687723
commit add2c4bf07
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
7 changed files with 18 additions and 15 deletions

View file

@ -8,6 +8,12 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime" "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. // InternString returns interned s.
// //
// This may be needed for reducing the amounts of allocated memory. // This may be needed for reducing the amounts of allocated memory.

View file

@ -11,7 +11,7 @@ func Itoa(n int) string {
bb := bbPool.Get() bb := bbPool.Get()
b := bb.B[:0] b := bb.B[:0]
b = strconv.AppendInt(b, int64(n), 10) b = strconv.AppendInt(b, int64(n), 10)
s := InternString(ToUnsafeString(b)) s := InternBytes(b)
bb.B = b bb.B = b
bbPool.Put(bb) bbPool.Put(bb)
return s return s

View file

@ -181,7 +181,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) valueStr := bytesutil.InternBytes(bb.B)
labels = setLabelValue(labels, labelsOffset, gl.targetLabel, valueStr) labels = setLabelValue(labels, labelsOffset, gl.targetLabel, valueStr)
} }
relabelBufPool.Put(bb) relabelBufPool.Put(bb)
@ -194,7 +194,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 = bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) replacement = bytesutil.InternBytes(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 {
@ -202,7 +202,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) valueStr := bytesutil.InternBytes(bb.B)
relabelBufPool.Put(bb) relabelBufPool.Put(bb)
return setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr) 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` // 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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) sourceStr := bytesutil.InternBytes(bb.B)
relabelBufPool.Put(bb) relabelBufPool.Put(bb)
valueStr := prc.replaceStringSubmatchesFast(sourceStr) valueStr := prc.replaceStringSubmatchesFast(sourceStr)
if valueStr != sourceStr { if valueStr != sourceStr {
@ -381,7 +381,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) valueStr := bytesutil.InternBytes(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)
@ -389,7 +389,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) valueStr := bytesutil.InternBytes(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)
@ -478,7 +478,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 := bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) s := bytesutil.InternBytes(bb.B)
relabelBufPool.Put(bb) relabelBufPool.Put(bb)
return s return s
} }

View file

@ -72,8 +72,7 @@ func concatTwoStrings(x, y string) string {
b := bb.B[:0] b := bb.B[:0]
b = append(b, x...) b = append(b, x...)
b = append(b, y...) b = append(b, y...)
s := bytesutil.ToUnsafeString(b) s := bytesutil.InternBytes(b)
s = bytesutil.InternString(s)
bb.B = b bb.B = b
bbPool.Put(bb) bbPool.Put(bb)
return s return s

View file

@ -1338,8 +1338,7 @@ func getScrapeURL(scheme, address, metricsPath, optionalQuestion, paramsStr stri
b = append(b, metricsPath...) b = append(b, metricsPath...)
b = append(b, optionalQuestion...) b = append(b, optionalQuestion...)
b = append(b, paramsStr...) b = append(b, paramsStr...)
s := bytesutil.ToUnsafeString(b) s := bytesutil.InternBytes(b)
s = bytesutil.InternString(s)
bb.B = b bb.B = b
bbPool.Put(bb) bbPool.Put(bb)
return s return s

View file

@ -41,8 +41,7 @@ func JoinHostPort(host string, port int) string {
} }
b = append(b, ':') b = append(b, ':')
b = strconv.AppendInt(b, int64(port), 10) b = strconv.AppendInt(b, int64(port), 10)
s := bytesutil.ToUnsafeString(b) s := bytesutil.InternBytes(b)
s = bytesutil.InternString(s)
bb.B = b bb.B = b
bbPool.Put(bb) bbPool.Put(bb)
return s return s

View file

@ -848,7 +848,7 @@ func (sw *scrapeWork) addRowToTimeseries(wc *writeRequestCtx, r *parser.Row, tim
bb := bbPool.Get() bb := bbPool.Get()
bb.B = append(bb.B, "exported_"...) bb.B = append(bb.B, "exported_"...)
bb.B = append(bb.B, metric...) bb.B = append(bb.B, metric...)
metric = bytesutil.InternString(bytesutil.ToUnsafeString(bb.B)) metric = bytesutil.InternBytes(bb.B)
bbPool.Put(bb) bbPool.Put(bb)
} }
labelsLen := len(wc.labels) labelsLen := len(wc.labels)