From 56e2c172e1d8af6eb19e65d8da1c04f009fd9203 Mon Sep 17 00:00:00 2001 From: yaling888 <73897884+yaling888@users.noreply.github.com> Date: Tue, 29 Mar 2022 07:06:41 +0800 Subject: [PATCH] Chore: adjust tun_wireguard cache buffer --- listener/tun/device/tun/tun_wireguard.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/listener/tun/device/tun/tun_wireguard.go b/listener/tun/device/tun/tun_wireguard.go index 30398b55..6e0ad15f 100644 --- a/listener/tun/device/tun/tun_wireguard.go +++ b/listener/tun/device/tun/tun_wireguard.go @@ -8,7 +8,6 @@ import ( "os" "runtime" - "github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/listener/tun/device" "github.com/Dreamacro/clash/listener/tun/device/iobased" @@ -22,6 +21,8 @@ type TUN struct { mtu uint32 name string offset int + + cache []byte } func Open(name string, mtu uint32) (_ device.Device, err error) { @@ -70,6 +71,10 @@ func Open(name string, mtu uint32) (_ device.Device, err error) { } t.mtu = uint32(tunMTU) + if t.offset > 0 { + t.cache = make([]byte, 65535) + } + return t, nil } @@ -78,19 +83,9 @@ func (t *TUN) Read(packet []byte) (int, error) { return t.nt.Read(packet, t.offset) } - buff := pool.Get(t.offset + cap(packet)) - defer func() { - _ = pool.Put(buff) - }() + n, err := t.nt.Read(t.cache, t.offset) - n, err := t.nt.Read(buff, t.offset) - if err != nil { - return 0, err - } - - _ = buff[:t.offset] - - copy(packet, buff[t.offset:t.offset+n]) + copy(packet, t.cache[t.offset:t.offset+n]) return n, err } @@ -100,7 +95,8 @@ func (t *TUN) Write(packet []byte) (int, error) { return t.nt.Write(packet, t.offset) } - packet = append(make([]byte, t.offset), packet...) + _ = t.cache[:t.offset] + packet = append(t.cache[:t.offset], packet...) return t.nt.Write(packet, t.offset) }