common/bytespool, common/buf: Pool array pointers to avoid allocations (#7491)

https://github.com/XTLS/Xray-core/pull/7487#issuecomment-7196411530

---------

Co-authored-by: 风扇滑翔翼 <Fangliding.fshxy@outlook.com>
This commit is contained in:
fanymagnet
2026-11-26 18:33:27 +00:00
committed by GitHub
co-authored by 风扇滑翔翼
parent 9576662b8e
commit 67756b3674
2 changed files with 67 additions and 75 deletions
+3 -23
View File
@@ -15,8 +15,6 @@ const (
var ErrBufferFull = errors.New("buffer is full") var ErrBufferFull = errors.New("buffer is full")
var pool = bytespool.GetPool(Size)
// ownership represents the data owner of the buffer. // ownership represents the data owner of the buffer.
type ownership uint8 type ownership uint8
@@ -39,16 +37,7 @@ type Buffer struct {
// New creates a Buffer with 0 length and 8K capacity, managed. // New creates a Buffer with 0 length and 8K capacity, managed.
func New() *Buffer { func New() *Buffer {
buf := pool.Get().([]byte) return &Buffer{v: bytespool.Alloc(Size)}
if cap(buf) >= Size {
buf = buf[:Size]
} else {
buf = make([]byte, Size)
}
return &Buffer{
v: buf,
}
} }
// NewExisted creates a standard size Buffer with an existed bytearray, managed. // NewExisted creates a standard size Buffer with an existed bytearray, managed.
@@ -80,16 +69,7 @@ func FromBytes(b []byte) *Buffer {
// StackNew creates a new Buffer object on stack, managed. // StackNew creates a new Buffer object on stack, managed.
// This method is for buffers that is released in the same function. // This method is for buffers that is released in the same function.
func StackNew() Buffer { func StackNew() Buffer {
buf := pool.Get().([]byte) return Buffer{v: bytespool.Alloc(Size)}
if cap(buf) >= Size {
buf = buf[:Size]
} else {
buf = make([]byte, Size)
}
return Buffer{
v: buf,
}
} }
// NewWithSize creates a Buffer with 0 length and capacity with at least the given size, bytespool's. // NewWithSize creates a Buffer with 0 length and capacity with at least the given size, bytespool's.
@@ -113,7 +93,7 @@ func (b *Buffer) Release() {
switch b.ownership { switch b.ownership {
case managed: case managed:
if cap(p) == Size { if cap(p) == Size {
pool.Put(p) bytespool.Free(p)
} }
case bytespools: case bytespools:
bytespool.Free(p) bytespool.Free(p)
+64 -52
View File
@@ -1,72 +1,84 @@
package bytespool package bytespool
import "sync" import (
"math/bits"
"sync"
)
func createAllocFunc(size int32) func() interface{} {
return func() interface{} {
return make([]byte, size)
}
}
// The following parameters controls the size of buffer pools.
// There are numPools pools. Starting from 2k size, the size of each pool is sizeMulti of the previous one.
// Package buf is guaranteed to not use buffers larger than the largest pool.
// Other packets may use larger buffers.
const ( const (
numPools = 4 minShift = 10 // 1KB
sizeMulti = 4 maxShift = 16 // 64KB
numPools = maxShift - minShift + 1
minSize = 1 << minShift
maxSize = 1 << maxShift
) )
var ( // pools hold array pointers (*[N]byte) to avoid interface boxing allocations on Put.
pool [numPools]sync.Pool var pools = [numPools]sync.Pool{
poolSize [numPools]int32 {New: func() any { return new([1 << 10]byte) }},
) {New: func() any { return new([1 << 11]byte) }},
{New: func() any { return new([1 << 12]byte) }},
func init() { {New: func() any { return new([1 << 13]byte) }},
size := int32(2048) {New: func() any { return new([1 << 14]byte) }},
for i := 0; i < numPools; i++ { {New: func() any { return new([1 << 15]byte) }},
pool[i] = sync.Pool{ {New: func() any { return new([1 << 16]byte) }},
New: createAllocFunc(size),
}
poolSize[i] = size
size *= sizeMulti
}
} }
// GetPool returns a sync.Pool that generates bytes array with at least the given size. // Alloc returns a byte slice with at least the given size. Minimum size of returned slice is 1024.
// It may return nil if no such pool exists.
//
// xray:api:stable
func GetPool(size int32) *sync.Pool {
for idx, ps := range poolSize {
if size <= ps {
return &pool[idx]
}
}
return nil
}
// Alloc returns a byte slice with at least the given size. Minimum size of returned slice is 2048.
// //
// xray:api:stable // xray:api:stable
func Alloc(size int32) []byte { func Alloc(size int32) []byte {
pool := GetPool(size) if size > maxSize {
if pool != nil { return make([]byte, size)
return pool.Get().([]byte) }
index := 0
if size > minSize {
index = bits.Len32(uint32(size-1)) - minShift
}
p := pools[index].Get()
switch index {
case 0:
return p.(*[1 << 10]byte)[:]
case 1:
return p.(*[1 << 11]byte)[:]
case 2:
return p.(*[1 << 12]byte)[:]
case 3:
return p.(*[1 << 13]byte)[:]
case 4:
return p.(*[1 << 14]byte)[:]
case 5:
return p.(*[1 << 15]byte)[:]
case 6:
return p.(*[1 << 16]byte)[:]
default:
panic("invalid pool index")
} }
return make([]byte, size)
} }
// Free puts a byte slice into the internal pool. // Free puts a byte slice into the internal pool.
// //
// xray:api:stable // xray:api:stable
func Free(b []byte) { func Free(b []byte) {
size := int32(cap(b)) c := cap(b)
b = b[0:cap(b)] if c < minSize || c > maxSize {
for i := numPools - 1; i >= 0; i-- { return
if size >= poolSize[i] { }
pool[i].Put(b) index := bits.Len(uint(c)) - 1 - minShift
return switch index {
} case 0:
pools[0].Put((*[1 << 10]byte)(b[:1<<10]))
case 1:
pools[1].Put((*[1 << 11]byte)(b[:1<<11]))
case 2:
pools[2].Put((*[1 << 12]byte)(b[:1<<12]))
case 3:
pools[3].Put((*[1 << 13]byte)(b[:1<<13]))
case 4:
pools[4].Put((*[1 << 14]byte)(b[:1<<14]))
case 5:
pools[5].Put((*[1 << 15]byte)(b[:1<<15]))
case 6:
pools[6].Put((*[1 << 16]byte)(b[:1<<16]))
} }
} }