mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
reduce allocations (also pulls in updated ironwood to do the same)
This commit is contained in:
parent
52709696a5
commit
8b5add5301
4 changed files with 24 additions and 5 deletions
|
@ -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)
|
||||
|
|
17
src/core/pool.go
Normal file
17
src/core/pool.go
Normal file
|
@ -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])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue