tailcfg, types/wgkey: add AppendTo methods on some types

Add MarshalText-like appending variants. Like:
https://pkg.go.dev/inet.af/netaddr#IP.AppendTo

To be used by @josharian's pending deephash optimizations.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2021-05-24 14:54:47 -07:00
committed by Brad Fitzpatrick
parent b340beff8e
commit e66d4e4c81
3 changed files with 46 additions and 13 deletions

View File

@ -14,6 +14,7 @@ import (
"inet.af/netaddr"
"tailscale.com/types/wgkey"
"tailscale.com/version"
)
func fieldsOf(t reflect.Type) (fields []string) {
@ -528,3 +529,25 @@ func BenchmarkKeyMarshalText(b *testing.B) {
sinkBytes = keyMarshalText("prefix", k)
}
}
func TestAppendKeyAllocs(t *testing.T) {
if version.IsRace() {
t.Skip("skipping in race detector") // append(b, make([]byte, N)...) not optimized in compiler with race
}
var k [32]byte
n := int(testing.AllocsPerRun(1000, func() {
sinkBytes = keyMarshalText("prefix", k)
}))
if n != 1 {
t.Fatalf("allocs = %v; want 1", n)
}
}
func TestDiscoKeyAppend(t *testing.T) {
d := DiscoKey{1: 1, 2: 2}
got := string(d.AppendTo([]byte("foo")))
want := "foodiscokey:0001020000000000000000000000000000000000000000000000000000000000"
if got != want {
t.Errorf("got %q; want %q", got, want)
}
}