diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go index 483a67867..f1a9e3749 100644 --- a/tailcfg/tailcfg.go +++ b/tailcfg/tailcfg.go @@ -7,7 +7,7 @@ package tailcfg //go:generate go run tailscale.com/cmd/cloner --type=User,Node,Hostinfo,NetInfo,Login,DNSConfig,DNSResolver,RegisterResponse --clonefunc=true --output=tailcfg_clone.go import ( - "bytes" + "encoding/hex" "errors" "fmt" "reflect" @@ -1027,9 +1027,10 @@ func (k MachineKey) HexString() string { return fmt.Sprintf("%x", func (k *MachineKey) UnmarshalText(text []byte) error { return keyUnmarshalText(k[:], "mkey:", text) } func keyMarshalText(prefix string, k [32]byte) []byte { - buf := bytes.NewBuffer(make([]byte, 0, len(prefix)+64)) - fmt.Fprintf(buf, "%s%x", prefix, k[:]) - return buf.Bytes() + buf := make([]byte, len(prefix)+64) + copy(buf, prefix) + hex.Encode(buf[len(prefix):], k[:]) + return buf } func keyUnmarshalText(dst []byte, prefix string, text []byte) error { diff --git a/tailcfg/tailcfg_test.go b/tailcfg/tailcfg_test.go index 410e532e5..7632f3ccb 100644 --- a/tailcfg/tailcfg_test.go +++ b/tailcfg/tailcfg_test.go @@ -518,3 +518,13 @@ func TestEndpointTypeMarshal(t *testing.T) { t.Errorf("got %s; want %s", got, want) } } + +var sinkBytes []byte + +func BenchmarkKeyMarshalText(b *testing.B) { + b.ReportAllocs() + var k [32]byte + for i := 0; i < b.N; i++ { + sinkBytes = keyMarshalText("prefix", k) + } +}