]> git.feebdaed.xyz Git - 0xmirror/quic-go.git/commitdiff
protocol: optimize ConnectionID.String (#5351)
authorMarten Seemann <martenseemann@gmail.com>
Fri, 3 Oct 2025 06:27:39 +0000 (14:27 +0800)
committerGitHub <noreply@github.com>
Fri, 3 Oct 2025 06:27:39 +0000 (14:27 +0800)
This function is used by qlog, so it should be fast.

internal/protocol/connection_id.go
internal/protocol/connection_id_test.go

index 77259b5fa5842b9115301a5a16e5ffa24ffb585f..ce3171fc5200a601d80c80e4e08bc5624139cb54 100644 (file)
@@ -2,8 +2,8 @@ package protocol
 
 import (
        "crypto/rand"
+       "encoding/hex"
        "errors"
-       "fmt"
        "io"
 )
 
@@ -26,7 +26,7 @@ func (c ArbitraryLenConnectionID) String() string {
        if c.Len() == 0 {
                return "(empty)"
        }
-       return fmt.Sprintf("%x", c.Bytes())
+       return hex.EncodeToString(c.Bytes())
 }
 
 const maxConnectionIDLen = 20
@@ -100,7 +100,7 @@ func (c ConnectionID) String() string {
        if c.Len() == 0 {
                return "(empty)"
        }
-       return fmt.Sprintf("%x", c.Bytes())
+       return hex.EncodeToString(c.Bytes())
 }
 
 type DefaultConnectionIDGenerator struct {
index 52afa19f37d1f8bbe353c35a51be57c9c71b2fa5..7439e2906e754eb600c527213084ed8e0779c2d6 100644 (file)
@@ -68,6 +68,15 @@ func TestConnectionIDZeroValue(t *testing.T) {
        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)