From fbcd00db5fb4b81f0459ca07024cd463d7418b0e Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 25 Sep 2018 15:52:41 -0700 Subject: [PATCH 1/2] avoid the buffer-pool on read We can xor in-place (this is what secio does). --- p2p/net/pnet/psk_conn.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/p2p/net/pnet/psk_conn.go b/p2p/net/pnet/psk_conn.go index 18dbada1fe..a2e9e1e188 100644 --- a/p2p/net/pnet/psk_conn.go +++ b/p2p/net/pnet/psk_conn.go @@ -37,14 +37,9 @@ func (c *pskConn) Read(out []byte) (int, error) { c.readS20 = salsa20.New(c.psk, nonce) } - maxn := uint32(len(out)) - in := mpool.ByteSlicePool.Get(maxn).([]byte) // get buffer - defer mpool.ByteSlicePool.Put(maxn, in) // put the buffer back - - in = in[:maxn] // truncate to required length - n, err := c.Conn.Read(in) // read to in + n, err := c.Conn.Read(out) // read to in if n > 0 { - c.readS20.XORKeyStream(out[:n], in[:n]) // decrypt to out buffer + c.readS20.XORKeyStream(out[:n], out[:n]) // decrypt to out buffer } return n, err } From f9f82398e1a763e2666cf0238e00dd64f7252478 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 25 Sep 2018 15:53:24 -0700 Subject: [PATCH 2/2] switch to go-buffer-pool It has a nicer interface and we don't even need the rest of the msgio stuff. --- p2p/net/pnet/psk_conn.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/p2p/net/pnet/psk_conn.go b/p2p/net/pnet/psk_conn.go index a2e9e1e188..5b16ef7a6c 100644 --- a/p2p/net/pnet/psk_conn.go +++ b/p2p/net/pnet/psk_conn.go @@ -7,8 +7,8 @@ import ( "net" salsa20 "github.com/davidlazar/go-crypto/salsa20" + pool "github.com/libp2p/go-buffer-pool" ipnet "github.com/libp2p/go-libp2p-interface-pnet" - mpool "github.com/libp2p/go-msgio/mpool" ) // we are using buffer pool as user needs their slice back @@ -58,11 +58,9 @@ func (c *pskConn) Write(in []byte) (int, error) { c.writeS20 = salsa20.New(c.psk, nonce) } - n := uint32(len(in)) - out := mpool.ByteSlicePool.Get(n).([]byte) // get buffer - defer mpool.ByteSlicePool.Put(n, out) // put the buffer back + out := pool.Get(len(in)) + defer pool.Put(out) - out = out[:n] // truncate to required length c.writeS20.XORKeyStream(out, in) // encrypt return c.Conn.Write(out) // send