1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 23:36:25 -04:00

reduce memory usage of Buffer

This commit is contained in:
Darien Raymond 2018-03-08 22:30:52 +01:00
parent fbc025869b
commit eaf043f1b3
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 28 additions and 38 deletions

View File

@ -13,10 +13,10 @@ type Supplier func([]byte) (int, error)
// quickly.
type Buffer struct {
v []byte
pool Pool
pool *Pool
start int
end int
start int32
end int32
}
// Release recycles the buffer into an internal buffer pool.
@ -48,25 +48,25 @@ func (b *Buffer) AppendBytes(bytes ...byte) int {
// Append appends a byte array to the end of the buffer.
func (b *Buffer) Append(data []byte) int {
nBytes := copy(b.v[b.end:], data)
b.end += nBytes
b.end += int32(nBytes)
return nBytes
}
// AppendSupplier appends the content of a BytesWriter to the buffer.
func (b *Buffer) AppendSupplier(writer Supplier) error {
nBytes, err := writer(b.v[b.end:])
b.end += nBytes
b.end += int32(nBytes)
return err
}
// Byte returns the bytes at index.
func (b *Buffer) Byte(index int) byte {
return b.v[b.start+index]
return b.v[b.start+int32(index)]
}
// SetByte sets the byte value at index.
func (b *Buffer) SetByte(index int, value byte) {
b.v[b.start+index] = value
b.v[b.start+int32(index)] = value
}
// Bytes returns the content bytes of this Buffer.
@ -78,7 +78,7 @@ func (b *Buffer) Bytes() []byte {
func (b *Buffer) Reset(writer Supplier) error {
nBytes, err := writer(b.v)
b.start = 0
b.end = nBytes
b.end = int32(nBytes)
return err
}
@ -90,7 +90,7 @@ func (b *Buffer) BytesRange(from, to int) []byte {
if to < 0 {
to += b.Len()
}
return b.v[b.start+from : b.start+to]
return b.v[b.start+int32(from) : b.start+int32(to)]
}
// BytesFrom returns a slice of this Buffer starting from the given position.
@ -98,7 +98,7 @@ func (b *Buffer) BytesFrom(from int) []byte {
if from < 0 {
from += b.Len()
}
return b.v[b.start+from : b.end]
return b.v[b.start+int32(from) : b.end]
}
// BytesTo returns a slice of this Buffer from start to the given position.
@ -106,7 +106,7 @@ func (b *Buffer) BytesTo(to int) []byte {
if to < 0 {
to += b.Len()
}
return b.v[b.start : b.start+to]
return b.v[b.start : b.start+int32(to)]
}
// Slice cuts the buffer at the given position.
@ -120,8 +120,8 @@ func (b *Buffer) Slice(from, to int) {
if to < from {
panic("Invalid slice")
}
b.end = b.start + to
b.start += from
b.end = b.start + int32(to)
b.start += int32(from)
}
// SliceFrom cuts the buffer at the given position.
@ -129,7 +129,7 @@ func (b *Buffer) SliceFrom(from int) {
if from < 0 {
from += b.Len()
}
b.start += from
b.start += int32(from)
}
// Len returns the length of the buffer content.
@ -137,7 +137,7 @@ func (b *Buffer) Len() int {
if b == nil {
return 0
}
return b.end - b.start
return int(b.end - b.start)
}
// IsEmpty returns true if the buffer is empty.
@ -147,13 +147,13 @@ func (b *Buffer) IsEmpty() bool {
// IsFull returns true if the buffer has no more room to grow.
func (b *Buffer) IsFull() bool {
return b.end == len(b.v)
return b.end == int32(len(b.v))
}
// Write implements Write method in io.Writer.
func (b *Buffer) Write(data []byte) (int, error) {
nBytes := copy(b.v[b.end:], data)
b.end += nBytes
b.end += int32(nBytes)
return nBytes, nil
}
@ -166,7 +166,7 @@ func (b *Buffer) Read(data []byte) (int, error) {
if nBytes == b.Len() {
b.Clear()
} else {
b.start += nBytes
b.start += int32(nBytes)
}
return nBytes, nil
}

View File

@ -5,21 +5,13 @@ import (
)
// Pool provides functionality to generate and recycle buffers on demand.
type Pool interface {
// Allocate either returns a unused buffer from the pool, or generates a new one from system.
Allocate() *Buffer
// Free recycles the given buffer.
Free(*Buffer)
}
// SyncPool is a buffer pool based on sync.Pool
type SyncPool struct {
type Pool struct {
allocator *sync.Pool
}
// NewSyncPool creates a SyncPool with given buffer size.
func NewSyncPool(bufferSize uint32) *SyncPool {
pool := &SyncPool{
// NewPool creates a SyncPool with given buffer size.
func NewPool(bufferSize uint32) *Pool {
pool := &Pool{
allocator: &sync.Pool{
New: func() interface{} { return make([]byte, bufferSize) },
},
@ -27,16 +19,16 @@ func NewSyncPool(bufferSize uint32) *SyncPool {
return pool
}
// Allocate implements Pool.Allocate().
func (p *SyncPool) Allocate() *Buffer {
// Allocate either returns a unused buffer from the pool, or generates a new one from system.
func (p *Pool) Allocate() *Buffer {
return &Buffer{
v: p.allocator.Get().([]byte),
pool: p,
}
}
// Free implements Pool.Free().
func (p *SyncPool) Free(buffer *Buffer) {
// // Free recycles the given buffer.
func (p *Pool) Free(buffer *Buffer) {
if buffer.v != nil {
p.allocator.Put(buffer.v)
}
@ -47,6 +39,4 @@ const (
Size = 2 * 1024
)
var (
mediumPool Pool = NewSyncPool(Size)
)
var mediumPool = NewPool(Size)

View File

@ -58,7 +58,7 @@ func TestBufferWrite(t *testing.T) {
func TestSyncPool(t *testing.T) {
assert := With(t)
p := NewSyncPool(32)
p := NewPool(32)
b := p.Allocate()
assert(b.Len(), Equals, 0)