mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 21:27:31 +00:00
all: implement AppendText alongside MarshalText (#9207)
This eventually allows encoding packages that may respect the proposed encoding.TextAppender interface. The performance gains from this is between 10-30%. Updates tailscale/corp#14379 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
@@ -7,7 +7,6 @@ package tailcfg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -445,6 +444,10 @@ const (
|
||||
MachineInvalid // server has explicitly rejected this machine key
|
||||
)
|
||||
|
||||
func (m MachineStatus) AppendText(b []byte) ([]byte, error) {
|
||||
return append(b, m.String()...), nil
|
||||
}
|
||||
|
||||
func (m MachineStatus) MarshalText() ([]byte, error) {
|
||||
return []byte(m.String()), nil
|
||||
}
|
||||
@@ -921,6 +924,10 @@ const (
|
||||
SignatureV2
|
||||
)
|
||||
|
||||
func (st SignatureType) AppendText(b []byte) ([]byte, error) {
|
||||
return append(b, st.String()...), nil
|
||||
}
|
||||
|
||||
func (st SignatureType) MarshalText() ([]byte, error) {
|
||||
return []byte(st.String()), nil
|
||||
}
|
||||
@@ -1765,18 +1772,6 @@ type Debug struct {
|
||||
Exit *int `json:",omitempty"`
|
||||
}
|
||||
|
||||
func appendKey(base []byte, prefix string, k [32]byte) []byte {
|
||||
ret := append(base, make([]byte, len(prefix)+64)...)
|
||||
buf := ret[len(base):]
|
||||
copy(buf, prefix)
|
||||
hex.Encode(buf[len(prefix):], k[:])
|
||||
return ret
|
||||
}
|
||||
|
||||
func keyMarshalText(prefix string, k [32]byte) []byte {
|
||||
return appendKey(nil, prefix, k)
|
||||
}
|
||||
|
||||
func (id ID) String() string { return fmt.Sprintf("id:%x", int64(id)) }
|
||||
func (id UserID) String() string { return fmt.Sprintf("userid:%x", int64(id)) }
|
||||
func (id LoginID) String() string { return fmt.Sprintf("loginid:%x", int64(id)) }
|
||||
|
@@ -1,6 +0,0 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package tailcfg
|
||||
|
||||
var ExportKeyMarshalText = keyMarshalText
|
@@ -16,11 +16,9 @@ import (
|
||||
"time"
|
||||
|
||||
. "tailscale.com/tailcfg"
|
||||
"tailscale.com/tstest"
|
||||
"tailscale.com/types/key"
|
||||
"tailscale.com/types/ptr"
|
||||
"tailscale.com/util/must"
|
||||
"tailscale.com/version"
|
||||
)
|
||||
|
||||
func fieldsOf(t reflect.Type) (fields []string) {
|
||||
@@ -683,29 +681,6 @@ func TestEndpointTypeMarshal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var sinkBytes []byte
|
||||
|
||||
func BenchmarkKeyMarshalText(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
var k [32]byte
|
||||
for i := 0; i < b.N; i++ {
|
||||
sinkBytes = ExportKeyMarshalText("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
|
||||
err := tstest.MinAllocsPerRun(t, 1, func() {
|
||||
sinkBytes = ExportKeyMarshalText("prefix", k)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRequestNilClone(t *testing.T) {
|
||||
var nilReq *RegisterRequest
|
||||
got := nilReq.Clone()
|
||||
|
Reference in New Issue
Block a user