1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-26 12:05:35 -05:00

small buffer

This commit is contained in:
Darien Raymond
2016-11-21 22:08:34 +01:00
parent c041740940
commit 70c75038a2
8 changed files with 67 additions and 24 deletions

View File

@@ -11,6 +11,31 @@ type Pool interface {
Free(*Buffer)
}
type SyncPool struct {
allocator *sync.Pool
}
func NewSyncPool(bufferSize uint32) *SyncPool {
pool := &SyncPool{
allocator: &sync.Pool{
New: func() interface{} { return make([]byte, bufferSize) },
},
}
return pool
}
func (p *SyncPool) Allocate() *Buffer {
return CreateBuffer(p.allocator.Get().([]byte), p)
}
func (p *SyncPool) Free(buffer *Buffer) {
rawBuffer := buffer.head
if rawBuffer == nil {
return
}
p.allocator.Put(rawBuffer)
}
type BufferPool struct {
chain chan []byte
allocator *sync.Pool
@@ -55,14 +80,15 @@ const (
mediumBufferByteSize = 8 * 1024
BufferSize = mediumBufferByteSize - defaultOffset
largeBufferByteSize = 64 * 1024
LargeBufferSize = largeBufferByteSize - defaultOffset
smallBufferByteSize = 2 * 1024
SmallBufferSize = smallBufferByteSize - defaultOffset
PoolSizeEnvKey = "v2ray.buffer.size"
)
var (
mediumPool *BufferPool
smallPool = NewSyncPool(2048)
)
func init() {