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

View File

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

View File

@ -101,7 +101,8 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
} }
// var frame ethernet.Frame // var frame ethernet.Frame
frame = getByteFull() fb := getByteFull()
frame = *fb
switch payload.LType { switch payload.LType {
default: default:
// log.Println(payload) // log.Println(payload)
@ -153,7 +154,7 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
return return
} }
putByte(frame) putByte(fb)
putPayload(payload) putPayload(payload)
} }
} }
@ -174,7 +175,8 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
for { for {
// var frame ethernet.Frame // var frame ethernet.Frame
// frame.Resize(BufferSize) // frame.Resize(BufferSize)
frame = getByteFull() fb := getByteFull()
frame = *fb
n, err = ifce.Read(frame) n, err = ifce.Read(frame)
if err != nil { if err != nil {
base.Error("tap Read err", n, err) 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 { for {
// data := make([]byte, BufferSize) // data := make([]byte, BufferSize)
data := getByteFull() hb := getByteFull()
data := *hb
n, err = ifce.Read(data) n, err = ifce.Read(data)
if err != nil { if err != nil {
base.Error("tun Read err", n, err) base.Error("tun Read err", n, err)
@ -121,6 +122,6 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
return return
} }
putByte(data) putByte(hb)
} }
} }

View File

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