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:
Joe Tsai
2023-09-01 18:15:19 -07:00
committed by GitHub
parent 9a3bc9049c
commit c6fadd6d71
12 changed files with 108 additions and 69 deletions
+14 -5
View File
@@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"slices"
"go4.org/mem"
)
@@ -49,11 +50,19 @@ func clamp25519Private(b []byte) {
b[31] = (b[31] & 127) | 64
}
func toHex(k []byte, prefix string) []byte {
ret := make([]byte, len(prefix)+len(k)*2)
copy(ret, prefix)
hex.Encode(ret[len(prefix):], k)
return ret
func appendHexKey(dst []byte, prefix string, key []byte) []byte {
dst = slices.Grow(dst, len(prefix)+hex.EncodedLen(len(key)))
dst = append(dst, prefix...)
dst = hexAppendEncode(dst, key)
return dst
}
// TODO(https://go.dev/issue/53693): Use hex.AppendEncode instead.
func hexAppendEncode(dst, src []byte) []byte {
n := hex.EncodedLen(len(src))
dst = slices.Grow(dst, n)
hex.Encode(dst[len(dst):][:n], src)
return dst[:len(dst)+n]
}
// parseHex decodes a key string of the form "<prefix><hex string>"