Skip to content

Commit a79722b

Browse files
committed
feat: PR mailru#370 (avoid extra alloc)
1 parent e917fb7 commit a79722b

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

buffer/pool.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net"
88
"sync"
9+
"unsafe"
910
)
1011

1112
// PoolConfig contains configuration for the allocation and reuse strategy.
@@ -47,17 +48,19 @@ func putBuf(buf []byte) {
4748
return
4849
}
4950
if c := buffers[size]; c != nil {
50-
c.Put(buf[:0])
51+
c.Put(unsafe.Pointer(&buf[:1][0]))
5152
}
5253
}
5354

55+
const maxArraySize = uint((uint64(1)<<50 - 1) & uint64(^uint(0)>>1))
56+
5457
// getBuf gets a chunk from reuse pool or creates a new one if reuse failed.
5558
func getBuf(size int) []byte {
5659
if size >= config.PooledSize {
5760
if c := buffers[size]; c != nil {
5861
v := c.Get()
5962
if v != nil {
60-
return v.([]byte)
63+
return (*[maxArraySize]byte)(v.(unsafe.Pointer))[:0:size]
6164
}
6265
}
6366
}

0 commit comments

Comments
 (0)