优化byte内存池

This commit is contained in:
bjdgyc 2021-07-19 12:30:20 +08:00
parent ea4dda0fca
commit 8ff77626d0
5 changed files with 29 additions and 23 deletions

View File

@ -34,7 +34,8 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
return
}
// hdata := make([]byte, BufferSize)
hdata := getByteFull()
hb := getByteFull()
hdata := *hb
n, err = conn.Read(hdata)
if err != nil {
base.Error("read hdata: ", err)
@ -68,7 +69,7 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
}
}
putByte(hdata)
putByte(hb)
}
}
@ -98,7 +99,8 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
}
h := []byte{'S', 'T', 'F', 0x01, 0x00, 0x00, payload.PType, 0x00}
header := getByteZero()
hb := getByteZero()
header := *hb
header = append(header, h...)
if payload.PType == 0x00 { // data
binary.BigEndian.PutUint16(header[4:6], uint16(len(payload.Data)))
@ -110,7 +112,7 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
return
}
putByte(header)
putByte(hb)
putPayload(payload)
// 限流设置

View File

@ -36,8 +36,8 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
return
}
// hdata := make([]byte, BufferSize)
hdata := getByteFull()
hb := getByteFull()
hdata := *hb
n, err := conn.Read(hdata)
if err != nil {
base.Error("read hdata: ", err)
@ -70,7 +70,7 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
}
}
putByte(hdata)
putByte(hb)
}
}
@ -99,7 +99,8 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
}
// header = []byte{payload.PType}
header := getByteZero()
hb := getByteZero()
header := *hb
header = append(header, payload.PType)
if payload.PType == 0x00 { // data
header = append(header, payload.Data...)
@ -110,7 +111,7 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
return
}
putByte(header)
putByte(hb)
putPayload(payload)
// 限流设置

View File

@ -101,7 +101,8 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
}
// var frame ethernet.Frame
frame = getByteFull()
fb := getByteFull()
frame = *fb
switch payload.LType {
default:
// log.Println(payload)
@ -153,7 +154,7 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
return
}
putByte(frame)
putByte(fb)
putPayload(payload)
}
}
@ -174,7 +175,8 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
for {
// var frame ethernet.Frame
// frame.Resize(BufferSize)
frame = getByteFull()
fb := getByteFull()
frame = *fb
n, err = ifce.Read(frame)
if err != nil {
base.Error("tap Read err", n, err)
@ -247,6 +249,6 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
}
putByte(frame)
putByte(fb)
}
}

View File

@ -102,7 +102,8 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
for {
// data := make([]byte, BufferSize)
data := getByteFull()
hb := getByteFull()
data := *hb
n, err = ifce.Read(data)
if err != nil {
base.Error("tun Read err", n, err)
@ -121,6 +122,6 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
return
}
putByte(data)
putByte(hb)
}
}

View File

@ -32,21 +32,21 @@ var bytePool = sync.Pool{
New: func() interface{} {
b := make([]byte, 0, BufferSize)
// fmt.Println("bytePool-init")
return b
return &b
},
}
func getByteZero() []byte {
b := bytePool.Get().([]byte)
func getByteZero() *[]byte {
b := bytePool.Get().(*[]byte)
return b
}
func getByteFull() []byte {
b := bytePool.Get().([]byte)
b = b[:BufferSize]
func getByteFull() *[]byte {
b := bytePool.Get().(*[]byte)
*b = (*b)[:BufferSize]
return b
}
func putByte(b []byte) {
b = b[:0]
func putByte(b *[]byte) {
*b = (*b)[:0]
bytePool.Put(b)
}