]> Cypherpunks.ru repositories - govpn.git/commitdiff
Do not allocate memory in each iteration
authorSergey Matveev <stargrave@stargrave.org>
Tue, 24 Jan 2017 08:06:18 +0000 (11:06 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 24 Jan 2017 08:09:28 +0000 (11:09 +0300)
src/cypherpunks.ru/govpn/identity.go
src/cypherpunks.ru/govpn/peer.go

index d738c5563e091179bd1e1fa287fb1bbcbd6ef1d9..89b12ab5cb118687415ef05c0cbbed79cbd10a9c 100644 (file)
@@ -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)
index 792ebe7d7e1606adc0b1d8737db5ff1e7643ca9e..7bb978c8d0548b6e1e03f37f2a8d31d17e97f8e2 100644 (file)
@@ -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