Skip to content

Commit

Permalink
feat: opt for concatenation instead of using fmt.Sprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
moisesvega committed May 3, 2024
1 parent dbfc758 commit 89cb16e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 89cb16e

Please sign in to comment.