wgengine: don't allocate so much in userspaceEngine.getStatus

It was one of the top garbage producers on my phone.

It's slated to be deleted and replaced anyway, but this helps in the
meantime.

The go.sum changes look scary, but the new dep only adds 240 bytes to
the binary. The go.sum noise is just cmd/go being aggressive in
including a lot of stuff (which is being fixed in Go 1.15, for what I
understand). And I ran a go mod tidy, which added some too. (I had to
write a custom wrapper around go mod tidy because this mod tidy
normally breaks on tailscale.io/control being missing but referenced
in tests)

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2020-04-08 08:42:38 -07:00
committed by Brad Fitzpatrick
parent 6b2e29867e
commit 922d9546bf
4 changed files with 260 additions and 108 deletions

View File

@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"go4.org/mem"
"golang.org/x/crypto/curve25519"
)
@ -67,3 +68,35 @@ func (k Private) Public() Public {
curve25519.ScalarBaseMult(&pub, (*[32]byte)(&k))
return Public(pub)
}
// NewPublicFromHexMem parses a public key in its hex form, given in m.
// The provided m must be exactly 64 bytes in length.
func NewPublicFromHexMem(m mem.RO) (Public, error) {
if m.Len() != 64 {
return Public{}, errors.New("invalid length")
}
var p Public
for i := range p {
a, ok1 := fromHexChar(m.At(i*2 + 0))
b, ok2 := fromHexChar(m.At(i*2 + 1))
if !ok1 || !ok2 {
return Public{}, errors.New("invalid hex character")
}
p[i] = (a << 4) | b
}
return p, nil
}
// fromHexChar converts a hex character into its value and a success flag.
func fromHexChar(c byte) (byte, bool) {
switch {
case '0' <= c && c <= '9':
return c - '0', true
case 'a' <= c && c <= 'f':
return c - 'a' + 10, true
case 'A' <= c && c <= 'F':
return c - 'A' + 10, true
}
return 0, false
}