Chore: adjust tun_wireguard cache buffer

This commit is contained in:
yaling888 2022-03-29 07:06:41 +08:00
parent b3b7a393f8
commit 56e2c172e1

View file

@ -8,7 +8,6 @@ import (
"os" "os"
"runtime" "runtime"
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/listener/tun/device" "github.com/Dreamacro/clash/listener/tun/device"
"github.com/Dreamacro/clash/listener/tun/device/iobased" "github.com/Dreamacro/clash/listener/tun/device/iobased"
@ -22,6 +21,8 @@ type TUN struct {
mtu uint32 mtu uint32
name string name string
offset int offset int
cache []byte
} }
func Open(name string, mtu uint32) (_ device.Device, err error) { 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) t.mtu = uint32(tunMTU)
if t.offset > 0 {
t.cache = make([]byte, 65535)
}
return t, nil return t, nil
} }
@ -78,19 +83,9 @@ func (t *TUN) Read(packet []byte) (int, error) {
return t.nt.Read(packet, t.offset) return t.nt.Read(packet, t.offset)
} }
buff := pool.Get(t.offset + cap(packet)) n, err := t.nt.Read(t.cache, t.offset)
defer func() {
_ = pool.Put(buff)
}()
n, err := t.nt.Read(buff, t.offset) copy(packet, t.cache[t.offset:t.offset+n])
if err != nil {
return 0, err
}
_ = buff[:t.offset]
copy(packet, buff[t.offset:t.offset+n])
return n, err return n, err
} }
@ -100,7 +95,8 @@ func (t *TUN) Write(packet []byte) (int, error) {
return t.nt.Write(packet, t.offset) 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) return t.nt.Write(packet, t.offset)
} }