reduce memory usage of Buffer

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

View File

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

View File

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