From 8b5add5301808a449f8b58f466cd3b85a2e72546 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 21 May 2023 12:38:16 -0500 Subject: [PATCH 1/3] reduce allocations (also pulls in updated ironwood to do the same) --- go.mod | 2 +- go.sum | 4 ++-- src/core/core.go | 6 ++++-- src/core/pool.go | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 src/core/pool.go diff --git a/go.mod b/go.mod index 4fd7340..6811e98 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/yggdrasil-network/yggdrasil-go go 1.19 require ( - github.com/Arceliar/ironwood v0.0.0-20230515022317-31b976732ebe + github.com/Arceliar/ironwood v0.0.0-20230521173602-97ee6b09b8e0 github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d github.com/cheggaaa/pb/v3 v3.0.8 github.com/gologme/log v1.2.0 diff --git a/go.sum b/go.sum index ed5f744..6eff958 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/Arceliar/ironwood v0.0.0-20230515022317-31b976732ebe h1:u69sr6Y9jqu6sk43Yyt+izLnLGgqCw3OYh2HU+jYUBw= -github.com/Arceliar/ironwood v0.0.0-20230515022317-31b976732ebe/go.mod h1:MIfrhR4b+U6gurd5pln622Zwaf2kzpIvXcnvRZMvlRI= +github.com/Arceliar/ironwood v0.0.0-20230521173602-97ee6b09b8e0 h1:u0BeMjhq0+jU+zaL6zlaBo9Z5KuG26bMtm+XM2e6dSQ= +github.com/Arceliar/ironwood v0.0.0-20230521173602-97ee6b09b8e0/go.mod h1:5x7fWW0mshe9WQ1lvSMmmHBYC3BeHH9gpwW5tz7cbfw= github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d h1:UK9fsWbWqwIQkMCz1CP+v5pGbsGoWAw6g4AyvMpm1EM= github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d/go.mod h1:BCnxhRf47C/dy/e/D2pmB8NkB3dQVIrkD98b220rx5Q= github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= diff --git a/src/core/core.go b/src/core/core.go index 748b738..dfc1870 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -183,7 +183,8 @@ func (c *Core) MTU() uint64 { } func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { - buf := make([]byte, c.PacketConn.MTU()) + buf := allocBytes(int(c.PacketConn.MTU())) + defer freeBytes(buf) for { bs := buf n, from, err = c.PacketConn.ReadFrom(bs) @@ -217,7 +218,8 @@ func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { } func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) { - buf := make([]byte, 0, 65535) + buf := allocBytes(0) + defer freeBytes(buf) buf = append(buf, typeSessionTraffic) buf = append(buf, p...) n, err = c.PacketConn.WriteTo(buf, addr) diff --git a/src/core/pool.go b/src/core/pool.go new file mode 100644 index 0000000..63c6253 --- /dev/null +++ b/src/core/pool.go @@ -0,0 +1,17 @@ +package core + +import "sync" + +var bytePool = sync.Pool{New: func() interface{} { return []byte(nil) }} + +func allocBytes(size int) []byte { + bs := bytePool.Get().([]byte) + if cap(bs) < size { + bs = make([]byte, size) + } + return bs[:size] +} + +func freeBytes(bs []byte) { + bytePool.Put(bs[:0]) +} From 5a6f27e732757e1b1468f89bbcf487d113b88457 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 21 May 2023 12:43:03 -0500 Subject: [PATCH 2/3] cheer up the linter --- src/core/core.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/core.go b/src/core/core.go index dfc1870..8907dd0 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -184,7 +184,7 @@ func (c *Core) MTU() uint64 { func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { buf := allocBytes(int(c.PacketConn.MTU())) - defer freeBytes(buf) + defer freeBytes(buf) //nolint:staticcheck for { bs := buf n, from, err = c.PacketConn.ReadFrom(bs) @@ -219,7 +219,7 @@ func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) { buf := allocBytes(0) - defer freeBytes(buf) + defer freeBytes(buf) //nolint:staticcheck buf = append(buf, typeSessionTraffic) buf = append(buf, p...) n, err = c.PacketConn.WriteTo(buf, addr) From e94985c583857cd3077dcc37132c7eb879d02292 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 21 May 2023 12:49:49 -0500 Subject: [PATCH 3/3] try to cheer up the linter again --- go.mod | 2 +- go.sum | 4 ++-- src/core/core.go | 4 ++-- src/core/pool.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 6811e98..0f4ee97 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/yggdrasil-network/yggdrasil-go go 1.19 require ( - github.com/Arceliar/ironwood v0.0.0-20230521173602-97ee6b09b8e0 + github.com/Arceliar/ironwood v0.0.0-20230521174855-fdfa6326d125 github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d github.com/cheggaaa/pb/v3 v3.0.8 github.com/gologme/log v1.2.0 diff --git a/go.sum b/go.sum index 6eff958..d9cd440 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/Arceliar/ironwood v0.0.0-20230521173602-97ee6b09b8e0 h1:u0BeMjhq0+jU+zaL6zlaBo9Z5KuG26bMtm+XM2e6dSQ= -github.com/Arceliar/ironwood v0.0.0-20230521173602-97ee6b09b8e0/go.mod h1:5x7fWW0mshe9WQ1lvSMmmHBYC3BeHH9gpwW5tz7cbfw= +github.com/Arceliar/ironwood v0.0.0-20230521174855-fdfa6326d125 h1:l2elyrosw63mTqZzwR0Nv8vPZWZC/0Hvwl8Iuva5htM= +github.com/Arceliar/ironwood v0.0.0-20230521174855-fdfa6326d125/go.mod h1:5x7fWW0mshe9WQ1lvSMmmHBYC3BeHH9gpwW5tz7cbfw= github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d h1:UK9fsWbWqwIQkMCz1CP+v5pGbsGoWAw6g4AyvMpm1EM= github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d/go.mod h1:BCnxhRf47C/dy/e/D2pmB8NkB3dQVIrkD98b220rx5Q= github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= diff --git a/src/core/core.go b/src/core/core.go index 8907dd0..dfc1870 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -184,7 +184,7 @@ func (c *Core) MTU() uint64 { func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { buf := allocBytes(int(c.PacketConn.MTU())) - defer freeBytes(buf) //nolint:staticcheck + defer freeBytes(buf) for { bs := buf n, from, err = c.PacketConn.ReadFrom(bs) @@ -219,7 +219,7 @@ func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) { func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) { buf := allocBytes(0) - defer freeBytes(buf) //nolint:staticcheck + defer freeBytes(buf) buf = append(buf, typeSessionTraffic) buf = append(buf, p...) n, err = c.PacketConn.WriteTo(buf, addr) diff --git a/src/core/pool.go b/src/core/pool.go index 63c6253..7b1e93e 100644 --- a/src/core/pool.go +++ b/src/core/pool.go @@ -13,5 +13,5 @@ func allocBytes(size int) []byte { } func freeBytes(bs []byte) { - bytePool.Put(bs[:0]) + bytePool.Put(bs[:0]) //nolint:staticcheck }