Skip to content

Commit e6e38b5

Browse files
committed
perf: Reduce allocations when creating metric families
Signed-off-by: fpetkovski <filip.petkovsky@gmail.com>
1 parent 807f8bd commit e6e38b5

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

pkg/metric/family.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package metric
1818

1919
import (
20-
"strings"
20+
"bytes"
2121
)
2222

2323
// FamilyInterface interface for a family
@@ -40,11 +40,11 @@ func (f Family) Inspect(inspect func(Family)) {
4040

4141
// ByteSlice returns the given Family in its string representation.
4242
func (f Family) ByteSlice() []byte {
43-
b := strings.Builder{}
43+
b := bytes.Buffer{}
4444
for _, m := range f.Metrics {
4545
b.WriteString(f.Name)
4646
m.Write(&b)
4747
}
4848

49-
return []byte(b.String())
49+
return b.Bytes()
5050
}

pkg/metric/metric.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package metric
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"math"
2223
"strconv"
@@ -64,7 +65,7 @@ type Metric struct {
6465
Value float64
6566
}
6667

67-
func (m *Metric) Write(s *strings.Builder) {
68+
func (m *Metric) Write(s *bytes.Buffer) {
6869
if len(m.LabelKeys) != len(m.LabelValues) {
6970
panic(fmt.Sprintf(
7071
"expected labelKeys %q to be of same length as labelValues %q",
@@ -78,7 +79,7 @@ func (m *Metric) Write(s *strings.Builder) {
7879
s.WriteByte('\n')
7980
}
8081

81-
func labelsToString(m *strings.Builder, keys, values []string) {
82+
func labelsToString(m *bytes.Buffer, keys, values []string) {
8283
if len(keys) > 0 {
8384
var separator byte = '{'
8485

@@ -102,15 +103,15 @@ var (
102103
// escapeString replaces '\' by '\\', new line character by '\n', and '"' by
103104
// '\"'.
104105
// Taken from github.com/prometheus/common/expfmt/text_create.go.
105-
func escapeString(m *strings.Builder, v string) {
106+
func escapeString(m *bytes.Buffer, v string) {
106107
escapeWithDoubleQuote.WriteString(m, v)
107108
}
108109

109110
// writeFloat is equivalent to fmt.Fprint with a float64 argument but hardcodes
110111
// a few common cases for increased efficiency. For non-hardcoded cases, it uses
111112
// strconv.AppendFloat to avoid allocations, similar to writeInt.
112113
// Taken from github.com/prometheus/common/expfmt/text_create.go.
113-
func writeFloat(w *strings.Builder, f float64) {
114+
func writeFloat(w *bytes.Buffer, f float64) {
114115
switch {
115116
case f == 1:
116117
w.WriteByte('1')

pkg/metric/metric_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package metric
1818

1919
import (
20+
"bytes"
2021
"strings"
2122
"testing"
2223
)
@@ -70,7 +71,7 @@ func BenchmarkMetricWrite(b *testing.B) {
7071
for _, test := range tests {
7172
b.Run(test.testName, func(b *testing.B) {
7273
for i := 0; i < b.N; i++ {
73-
builder := strings.Builder{}
74+
builder := bytes.Buffer{}
7475

7576
test.metric.Write(&builder)
7677

0 commit comments

Comments
 (0)