From 8b30dc0a74d068b1a081e897dc0bb6d4d80281ab Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 24 Jan 2017 11:06:18 +0300 Subject: [PATCH] Do not allocate memory in each iteration --- src/cypherpunks.ru/govpn/identity.go | 3 ++- src/cypherpunks.ru/govpn/peer.go | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cypherpunks.ru/govpn/identity.go b/src/cypherpunks.ru/govpn/identity.go index d738c55..89b12ab 100644 --- a/src/cypherpunks.ru/govpn/identity.go +++ b/src/cypherpunks.ru/govpn/identity.go @@ -106,6 +106,7 @@ func (mc *MACCache) Find(data []byte) *PeerId { return nil } buf := make([]byte, 8) + sum := make([]byte, 32) mc.l.RLock() for pid, mt := range mc.cache { copy(buf, data) @@ -113,7 +114,7 @@ func (mc *MACCache) Find(data []byte) *PeerId { mt.l.Lock() mt.mac.Reset() mt.mac.Write(buf) - sum := mt.mac.Sum(nil) + mt.mac.Sum(sum[:0]) mt.l.Unlock() if subtle.ConstantTimeCompare(sum[len(sum)-8:], data[len(data)-8:]) == 1 { ppid := PeerId(pid) diff --git a/src/cypherpunks.ru/govpn/peer.go b/src/cypherpunks.ru/govpn/peer.go index 792ebe7..7bb978c 100644 --- a/src/cypherpunks.ru/govpn/peer.go +++ b/src/cypherpunks.ru/govpn/peer.go @@ -63,10 +63,8 @@ func newNonces(key *[32]byte, i uint64) chan *[NonceSize]byte { buf := new([NonceSize]byte) binary.BigEndian.PutUint64(buf[:], i) mac.Write(buf[:]) - sum = mac.Sum(nil) - for index := 0; index < NonceSize; index++ { - buf[index] = sum[index] - } + mac.Sum(sum[0:]) + copy(buf[:], sum) nonces <- buf mac.Reset() i += 2 -- 2.44.0