Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set the init width produced by BufferPool #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions bufferpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ import (
// channel.
type BufferPool struct {
c chan *bytes.Buffer
w int
}

// NewBufferPool creates a new BufferPool bounded to the given size.
func NewBufferPool(size int) (bp *BufferPool) {
// NewBufferPool creates a new BufferPool bounded to the given maxSize
// with a buffer width initialized to 0.
func NewBufferPool(maxSize int) (bp *BufferPool) {
return NewBufferPoolWidth(maxSize, 0)
}

// NewBufferPoolWidth creates a new BufferPool bounded to the given maxSize
// with new buffer's backend byte slices sized based on width.
func NewBufferPoolWidth(maxSize, width int) (bp *BufferPool) {
return &BufferPool{
c: make(chan *bytes.Buffer, size),
c: make(chan *bytes.Buffer, maxSize),
w: width,
}
}

Expand All @@ -25,7 +34,7 @@ func (bp *BufferPool) Get() (b *bytes.Buffer) {
// reuse existing buffer
default:
// create new buffer
b = bytes.NewBuffer([]byte{})
b = bytes.NewBuffer(make([]byte, 0, bp.w))
}
return
}
Expand Down
40 changes: 35 additions & 5 deletions bufferpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,55 @@ import (
)

func TestBufferPool(t *testing.T) {
var size int = 4
maxSize := 4

bufPool := NewBufferPool(size)
bufPool := NewBufferPool(maxSize)

// Test Get/Put
b := bufPool.Get()
if b.Cap() != 0 {
t.Errorf("bufferpool width invalid: got %v want %v", b.Cap(), 0)
}
bufPool.Put(b)

// Add some additional buffers beyond the pool size.
for i := 0; i < maxSize*2; i++ {
bufPool.Put(bytes.NewBuffer([]byte{}))
}

// Close the channel so we can iterate over it.
close(bufPool.c)

// Check the size of the pool.
if len(bufPool.c) != maxSize {
t.Fatalf("bufferpool size invalid: got %v want %v", len(bufPool.c), maxSize)
}
}

func TestBufferPoolWidth(t *testing.T) {
maxSize := 4
width := 10

bufPool := NewBufferPoolWidth(maxSize, width)

// Test Get/Put
b := bufPool.Get()
if b.Cap() != width {
t.Errorf("bufferpool width invalid: got %v want %v", b.Cap(), width)
}
bufPool.Put(b)

// Add some additional buffers beyond the pool size.
for i := 0; i < size*2; i++ {
for i := 0; i < maxSize*2; i++ {
bufPool.Put(bytes.NewBuffer([]byte{}))
}

// Close the channel so we can iterate over it.
close(bufPool.c)

// Check the size of the pool.
if len(bufPool.c) != size {
t.Fatalf("bufferpool size invalid: got %v want %v", len(bufPool.c), size)
if len(bufPool.c) != maxSize {
t.Fatalf("bufferpool size invalid: got %v want %v", len(bufPool.c), maxSize)
}

}