wgengine/wglog: optimize wireguardGoString

The new code is ugly, but much faster and leaner.

name        old time/op    new time/op    delta
SetPeers-8    7.81µs ± 1%    3.59µs ± 1%  -54.04%  (p=0.000 n=9+10)

name        old alloc/op   new alloc/op   delta
SetPeers-8    7.68kB ± 0%    2.53kB ± 0%  -67.08%  (p=0.000 n=10+10)

name        old allocs/op  new allocs/op  delta
SetPeers-8       237 ± 0%        99 ± 0%  -58.23%  (p=0.000 n=10+10)

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
pull/1839/head
Josh Bleecher Snyder 4 years ago committed by Josh Bleecher Snyder
parent 9356912053
commit 98cae48e70

@ -85,7 +85,7 @@ func (x *Logger) SetPeers(peers []wgcfg.Peer) {
// Construct a new peer public key log rewriter. // Construct a new peer public key log rewriter.
replace := make(map[string]string) replace := make(map[string]string)
for _, peer := range peers { for _, peer := range peers {
old := "peer(" + wireguardGoString(peer.PublicKey) + ")" old := wireguardGoString(peer.PublicKey)
new := peer.PublicKey.ShortString() new := peer.PublicKey.ShortString()
replace[old] = new replace[old] = new
} }
@ -94,10 +94,17 @@ func (x *Logger) SetPeers(peers []wgcfg.Peer) {
// wireguardGoString prints p in the same format used by wireguard-go. // wireguardGoString prints p in the same format used by wireguard-go.
func wireguardGoString(k wgkey.Key) string { func wireguardGoString(k wgkey.Key) string {
base64Key := base64.StdEncoding.EncodeToString(k[:]) const prefix = "peer("
abbreviatedKey := "invalid" b := make([]byte, len(prefix)+44)
if len(base64Key) == 44 { copy(b, prefix)
abbreviatedKey = base64Key[0:4] + "…" + base64Key[39:43] r := b[len(prefix):]
} base64.StdEncoding.Encode(r, k[:])
return abbreviatedKey r = r[4:]
copy(r, "…")
r = r[len("…"):]
copy(r, b[len(prefix)+39:len(prefix)+43])
r = r[4:]
r[0] = ')'
r = r[1:]
return string(b[:len(b)-len(r)])
} }

Loading…
Cancel
Save