This function is used by qlog, so it should be fast.
import (
"crypto/rand"
+ "encoding/hex"
"errors"
- "fmt"
"io"
)
if c.Len() == 0 {
return "(empty)"
}
- return fmt.Sprintf("%x", c.Bytes())
+ return hex.EncodeToString(c.Bytes())
}
const maxConnectionIDLen = 20
if c.Len() == 0 {
return "(empty)"
}
- return fmt.Sprintf("%x", c.Bytes())
+ return hex.EncodeToString(c.Bytes())
}
type DefaultConnectionIDGenerator struct {
require.Equal(t, "(empty)", (ConnectionID{}).String())
}
+// The string representation of a connection ID is used in qlog, so it should be fast.
+func BenchmarkConnectionIDStringer(b *testing.B) {
+ c := ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef, 0x42})
+ b.ReportAllocs()
+ for b.Loop() {
+ _ = c.String()
+ }
+}
+
func TestArbitraryLenConnectionID(t *testing.T) {
b := make([]byte, 42)
rand.Read(b)