From 89cb16e93dfb26a550f08e34305286b3c932293d Mon Sep 17 00:00:00 2001 From: Moises Vega Date: Fri, 3 May 2024 15:35:48 -0700 Subject: [PATCH] feat: opt for concatenation instead of using fmt.Sprintf --- baggage/baggage.go | 4 ++-- baggage/baggage_test.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/baggage/baggage.go b/baggage/baggage.go index 75773bc1ce9..f98c54a3cba 100644 --- a/baggage/baggage.go +++ b/baggage/baggage.go @@ -335,9 +335,9 @@ func (m Member) String() string { // A key is just an ASCII string. A value is restricted to be // US-ASCII characters excluding CTLs, whitespace, // DQUOTE, comma, semicolon, and backslash. - s := fmt.Sprintf("%s%s%s", m.key, keyValueDelimiter, valueEscape(m.value)) + s := m.key + keyValueDelimiter + valueEscape(m.value) if len(m.properties) > 0 { - s = fmt.Sprintf("%s%s%s", s, propertyDelimiter, m.properties.String()) + s += propertyDelimiter + m.properties.String() } return s } diff --git a/baggage/baggage_test.go b/baggage/baggage_test.go index e8f67761f0b..8acd149f48c 100644 --- a/baggage/baggage_test.go +++ b/baggage/baggage_test.go @@ -1027,3 +1027,18 @@ func BenchmarkValueEscape(b *testing.B) { }) } } + +func BenchmarkMemberString(b *testing.B) { + b.ReportAllocs() + alphabet := "abcdefghijklmnopqrstuvwxyz" + props := make([]Property, len(alphabet)) + for i, r := range alphabet { + props[i] = Property{key: string(r)} + } + member, err := NewMember(alphabet, alphabet, props...) + require.NoError(b, err) + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = member.String() + } +}