device: avoid hex allocations in IpcGet
benchmark old ns/op new ns/op delta BenchmarkUAPIGet-16 2872 2157 -24.90% benchmark old allocs new allocs delta BenchmarkUAPIGet-16 30 18 -40.00% benchmark old bytes new bytes delta BenchmarkUAPIGet-16 737 256 -65.26% Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
d4725bc456
commit
34c047c762
@ -60,18 +60,10 @@ func (key *NoisePrivateKey) FromMaybeZeroHex(src string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (key NoisePrivateKey) ToHex() string {
|
|
||||||
return hex.EncodeToString(key[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (key *NoisePublicKey) FromHex(src string) error {
|
func (key *NoisePublicKey) FromHex(src string) error {
|
||||||
return loadExactHex(key[:], src)
|
return loadExactHex(key[:], src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (key NoisePublicKey) ToHex() string {
|
|
||||||
return hex.EncodeToString(key[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (key NoisePublicKey) IsZero() bool {
|
func (key NoisePublicKey) IsZero() bool {
|
||||||
var zero NoisePublicKey
|
var zero NoisePublicKey
|
||||||
return key.Equals(zero)
|
return key.Equals(zero)
|
||||||
@ -84,7 +76,3 @@ func (key NoisePublicKey) Equals(tar NoisePublicKey) bool {
|
|||||||
func (key *NoisePresharedKey) FromHex(src string) error {
|
func (key *NoisePresharedKey) FromHex(src string) error {
|
||||||
return loadExactHex(key[:], src)
|
return loadExactHex(key[:], src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (key NoisePresharedKey) ToHex() string {
|
|
||||||
return hex.EncodeToString(key[:])
|
|
||||||
}
|
|
||||||
|
@ -57,6 +57,17 @@ func (device *Device) IpcGetOperation(w io.Writer) error {
|
|||||||
fmt.Fprintf(buf, format, args...)
|
fmt.Fprintf(buf, format, args...)
|
||||||
buf.WriteByte('\n')
|
buf.WriteByte('\n')
|
||||||
}
|
}
|
||||||
|
keyf := func(prefix string, key *[32]byte) {
|
||||||
|
buf.Grow(len(key)*2 + 2 + len(prefix))
|
||||||
|
buf.WriteString(prefix)
|
||||||
|
buf.WriteByte('=')
|
||||||
|
const hex = "0123456789abcdef"
|
||||||
|
for i := 0; i < len(key); i++ {
|
||||||
|
buf.WriteByte(hex[key[i]>>4])
|
||||||
|
buf.WriteByte(hex[key[i]&0xf])
|
||||||
|
}
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
|
|
||||||
@ -74,7 +85,7 @@ func (device *Device) IpcGetOperation(w io.Writer) error {
|
|||||||
// serialize device related values
|
// serialize device related values
|
||||||
|
|
||||||
if !device.staticIdentity.privateKey.IsZero() {
|
if !device.staticIdentity.privateKey.IsZero() {
|
||||||
sendf("private_key=%s", device.staticIdentity.privateKey.ToHex())
|
keyf("private_key", (*[32]byte)(&device.staticIdentity.privateKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
if device.net.port != 0 {
|
if device.net.port != 0 {
|
||||||
@ -91,8 +102,8 @@ func (device *Device) IpcGetOperation(w io.Writer) error {
|
|||||||
peer.RLock()
|
peer.RLock()
|
||||||
defer peer.RUnlock()
|
defer peer.RUnlock()
|
||||||
|
|
||||||
sendf("public_key=%s", peer.handshake.remoteStatic.ToHex())
|
keyf("public_key", (*[32]byte)(&peer.handshake.remoteStatic))
|
||||||
sendf("preshared_key=%s", peer.handshake.presharedKey.ToHex())
|
keyf("preshared_key", (*[32]byte)(&peer.handshake.presharedKey))
|
||||||
sendf("protocol_version=1")
|
sendf("protocol_version=1")
|
||||||
if peer.endpoint != nil {
|
if peer.endpoint != nil {
|
||||||
sendf("endpoint=%s", peer.endpoint.DstToString())
|
sendf("endpoint=%s", peer.endpoint.DstToString())
|
||||||
|
Loading…
Reference in New Issue
Block a user