From db48726a6b967c583691993a58125a30d8865684 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Mon, 16 Jan 2017 17:24:00 +0100 Subject: [PATCH] pkg/textparse: allocate single string per metric --- pkg/textparse/parse.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/textparse/parse.go b/pkg/textparse/parse.go index 8139120c6..6a41e4c7c 100644 --- a/pkg/textparse/parse.go +++ b/pkg/textparse/parse.go @@ -73,18 +73,21 @@ func (p *Parser) Err() error { } func (p *Parser) Metric(l *labels.Labels) { + // Allocate the full immutable string immediately, so we just + // have to create references on it below. + s := string(p.l.b[p.l.mstart:p.l.mend]) + *l = append(*l, labels.Label{ Name: labels.MetricName, - Value: string(p.l.b[p.l.mstart:p.l.offsets[0]]), + Value: s[:p.l.offsets[0]-p.l.mstart], }) for i := 1; i < len(p.l.offsets); i += 3 { - a, b, c := p.l.offsets[i], p.l.offsets[i+1], p.l.offsets[i+2] + a := p.l.offsets[i] - p.l.mstart + b := p.l.offsets[i+1] - p.l.mstart + c := p.l.offsets[i+2] - p.l.mstart - *l = append(*l, labels.Label{ - Name: string(p.l.b[a:b]), - Value: string(p.l.b[b+2 : c]), - }) + *l = append(*l, labels.Label{Name: s[a:b], Value: s[b+2 : c]}) } sort.Sort((*l)[1:])