device: optimize Peer.String even more
This reduces the allocation, branches, and amount of base64 encoding. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
25ad08a591
commit
9087e444e6
@ -7,7 +7,6 @@ package device
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -150,19 +149,22 @@ func (peer *Peer) String() string {
|
|||||||
// return fmt.Sprintf("peer(%s)", abbreviatedKey)
|
// return fmt.Sprintf("peer(%s)", abbreviatedKey)
|
||||||
//
|
//
|
||||||
// except that it is considerably more efficient.
|
// except that it is considerably more efficient.
|
||||||
const prefix = "peer("
|
src := peer.handshake.remoteStatic
|
||||||
b := make([]byte, len(prefix)+44)
|
b64 := func(input byte) byte {
|
||||||
copy(b, prefix)
|
return input + 'A' + byte(((25-int(input))>>8)&6) - byte(((51-int(input))>>8)&75) - byte(((61-int(input))>>8)&15) + byte(((62-int(input))>>8)&3)
|
||||||
r := b[len(prefix):]
|
}
|
||||||
base64.StdEncoding.Encode(r, peer.handshake.remoteStatic[:])
|
b := []byte("peer(____…____)")
|
||||||
r = r[4:]
|
const first = len("peer(")
|
||||||
copy(r, "…")
|
const second = len("peer(____…")
|
||||||
r = r[len("…"):]
|
b[first+0] = b64((src[0] >> 2) & 63)
|
||||||
copy(r, b[len(prefix)+39:len(prefix)+43])
|
b[first+1] = b64(((src[0] << 4) | (src[1] >> 4)) & 63)
|
||||||
r = r[4:]
|
b[first+2] = b64(((src[1] << 2) | (src[2] >> 6)) & 63)
|
||||||
r[0] = ')'
|
b[first+3] = b64(src[2] & 63)
|
||||||
r = r[1:]
|
b[second+0] = b64(src[29] & 63)
|
||||||
return string(b[:len(b)-len(r)])
|
b[second+1] = b64((src[30] >> 2) & 63)
|
||||||
|
b[second+2] = b64(((src[30] << 4) | (src[31] >> 4)) & 63)
|
||||||
|
b[second+3] = b64((src[31] << 2) & 63)
|
||||||
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (peer *Peer) Start() {
|
func (peer *Peer) Start() {
|
||||||
|
Loading…
Reference in New Issue
Block a user