1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

reuse 128k []byte

This commit is contained in:
wdvxdr 2021-04-15 22:32:43 +08:00
parent 3c0241b2e3
commit 5b46d1651f
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6
2 changed files with 43 additions and 2 deletions

View File

@ -95,3 +95,38 @@ func releaseZlibWriter(w *zlibWriter) {
zlibPool.Put(w)
}
}
const size128k = 128 * 1024
var b128kPool = sync.Pool{
New: func() interface{} {
return make128kSlicePointer()
},
}
// Get128KBytes 获取一个128k大小 []byte
func Get128KBytes() *[]byte {
buf := b128kPool.Get().(*[]byte)
if buf == nil {
return make128kSlicePointer()
}
if cap(*buf) < size128k {
return make128kSlicePointer()
}
*buf = (*buf)[:size128k]
return buf
}
// Put128KBytes 放回一个128k大小 []byte
func Put128KBytes(b *[]byte) {
if cap(*b) < size128k || cap(*b) > 2*size128k { // 太大或太小的 []byte 不要放入
return
}
*b = (*b)[:cap(*b)]
b128kPool.Put(b)
}
func make128kSlicePointer() *[]byte {
data := make([]byte, size128k)
return &data
}

View File

@ -46,7 +46,10 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea
defer conn.Close()
offset := 0
reader := binary.NewNetworkReader(conn)
chunk := make([]byte, chunkSize)
chunk := *binary.Get128KBytes()
defer func() { // 延迟捕获 chunk
binary.Put128KBytes(&chunk)
}()
w := binary.NewWriter()
defer binary.PutBuffer(w)
for {
@ -128,7 +131,10 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3
return nil, errors.Wrap(err, "echo error")
}
var rspExt []byte
chunk := make([]byte, chunkSize)
chunk := *binary.Get128KBytes()
defer func() { // 延迟捕获 chunk
binary.Put128KBytes(&chunk)
}()
w := binary.NewWriter()
defer binary.PutBuffer(w)
for {