From 25ad08a59157ed8a9750e827c658260b4138fd93 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 13 May 2021 15:30:18 -0700 Subject: device: optimize Peer.String Signed-off-by: Josh Bleecher Snyder --- device/peer.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'device') diff --git a/device/peer.go b/device/peer.go index 005976b..d2f6d07 100644 --- a/device/peer.go +++ b/device/peer.go @@ -9,7 +9,6 @@ import ( "container/list" "encoding/base64" "errors" - "fmt" "sync" "sync/atomic" "time" @@ -144,12 +143,26 @@ func (peer *Peer) SendBuffer(buffer []byte) error { } func (peer *Peer) String() string { - base64Key := base64.StdEncoding.EncodeToString(peer.handshake.remoteStatic[:]) - abbreviatedKey := "invalid" - if len(base64Key) == 44 { - abbreviatedKey = base64Key[0:4] + "…" + base64Key[39:43] - } - return fmt.Sprintf("peer(%s)", abbreviatedKey) + // The awful goo that follows is identical to: + // + // base64Key := base64.StdEncoding.EncodeToString(peer.handshake.remoteStatic[:]) + // abbreviatedKey := base64Key[0:4] + "…" + base64Key[39:43] + // return fmt.Sprintf("peer(%s)", abbreviatedKey) + // + // except that it is considerably more efficient. + const prefix = "peer(" + b := make([]byte, len(prefix)+44) + copy(b, prefix) + r := b[len(prefix):] + base64.StdEncoding.Encode(r, peer.handshake.remoteStatic[:]) + 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)]) } func (peer *Peer) Start() { -- cgit v1.2.3