mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
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>
This commit is contained in:
parent
9356912053
commit
98cae48e70
@ -85,7 +85,7 @@ func (x *Logger) SetPeers(peers []wgcfg.Peer) {
|
||||
// Construct a new peer public key log rewriter.
|
||||
replace := make(map[string]string)
|
||||
for _, peer := range peers {
|
||||
old := "peer(" + wireguardGoString(peer.PublicKey) + ")"
|
||||
old := wireguardGoString(peer.PublicKey)
|
||||
new := peer.PublicKey.ShortString()
|
||||
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.
|
||||
func wireguardGoString(k wgkey.Key) string {
|
||||
base64Key := base64.StdEncoding.EncodeToString(k[:])
|
||||
abbreviatedKey := "invalid"
|
||||
if len(base64Key) == 44 {
|
||||
abbreviatedKey = base64Key[0:4] + "…" + base64Key[39:43]
|
||||
}
|
||||
return abbreviatedKey
|
||||
const prefix = "peer("
|
||||
b := make([]byte, len(prefix)+44)
|
||||
copy(b, prefix)
|
||||
r := b[len(prefix):]
|
||||
base64.StdEncoding.Encode(r, k[:])
|
||||
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…
Reference in New Issue
Block a user