summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josh@tailscale.com>2021-05-13 15:30:18 -0700
committerJason A. Donenfeld <Jason@zx2c4.com>2021-05-14 00:37:30 +0200
commit25ad08a59157ed8a9750e827c658260b4138fd93 (patch)
treea23107999b75765ec6c916b5e469d34abb717d4c
parent5846b622837e04dbc35b153d9ceda7fd66397520 (diff)
device: optimize Peer.String
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
-rw-r--r--device/peer.go27
1 files changed, 20 insertions, 7 deletions
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() {