diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 00419f4..d690cc0 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -14,6 +14,10 @@ "ImportPath": "github.com/julienschmidt/httprouter", "Rev": "00ce1c6a267162792c367acc43b1681a884e1872" }, + { + "ImportPath": "github.com/pushrax/bufferpool", + "Rev": "c8962db3ad1e65fe9a83e7298dcd213938cf02d5" + }, { "ImportPath": "github.com/pushrax/faststats", "Rev": "0fc2c5e41a187240ffaa09320eea7df9f8071388" diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/.travis.yml b/Godeps/_workspace/src/github.com/pushrax/bufferpool/.travis.yml new file mode 100644 index 0000000..4f2ee4d --- /dev/null +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/.travis.yml @@ -0,0 +1 @@ +language: go diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/AUTHORS b/Godeps/_workspace/src/github.com/pushrax/bufferpool/AUTHORS new file mode 100644 index 0000000..6c576d0 --- /dev/null +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/AUTHORS @@ -0,0 +1,4 @@ +# This is the official list of Bufferpool authors for copyright purposes. + +Jimmy Zelinskie +Justin Li diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/LICENSE b/Godeps/_workspace/src/github.com/pushrax/bufferpool/LICENSE new file mode 100644 index 0000000..4fbcf1a --- /dev/null +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/LICENSE @@ -0,0 +1,24 @@ +Bufferpool is released under a BSD 2-Clause license, reproduced below. + +Copyright (c) 2013, The Bufferpool Authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/README.md b/Godeps/_workspace/src/github.com/pushrax/bufferpool/README.md new file mode 100644 index 0000000..50f90d3 --- /dev/null +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/README.md @@ -0,0 +1,9 @@ +# bufferpool [![Build Status](https://secure.travis-ci.org/pushrax/bufferpool.png)](http://travis-ci.org/pushrax/bufferpool) + +The bufferpool package implements a thread-safe pool of reusable, equally sized `byte.Buffer`s. +If you're allocating `byte.Buffer`s very frequently, you can use this to speed up your +program and take strain off the garbage collector. + +## docs + +[GoDoc](http://godoc.org/github.com/pushrax/bufferpool) diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go new file mode 100644 index 0000000..b83919d --- /dev/null +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go @@ -0,0 +1,68 @@ +// Copyright 2013 The Bufferpool Authors. All rights reserved. +// Use of this source code is governed by the BSD 2-Clause license, +// which can be found in the LICENSE file. + +// Package bufferpool implements a capacity-limited pool of reusable, +// equally-sized buffers. +package bufferpool + +import ( + "bytes" + "errors" +) + +// A BufferPool is a capacity-limited pool of equally sized buffers. +type BufferPool struct { + bufferSize int + pool chan []byte +} + +// New returns a newly allocated BufferPool with the given maximum pool size +// and buffer size. +func New(poolSize, bufferSize int) *BufferPool { + return &BufferPool{ + bufferSize, + make(chan []byte, poolSize), + } +} + +// Take is used to obtain a new zeroed buffer. This will allocate a new buffer +// if the pool was empty. +func (pool *BufferPool) Take() *bytes.Buffer { + return bytes.NewBuffer(pool.TakeSlice()) +} + +// TakeSlice is used to obtain a new slice. This will allocate a new slice +// if the pool was empty. +func (pool *BufferPool) TakeSlice() (slice []byte) { + select { + case slice = <-pool.pool: + default: + slice = make([]byte, 0, pool.bufferSize) + } + return +} + +// Give is used to attempt to return a buffer to the pool. It may not +// be added to the pool if it was already full. +func (pool *BufferPool) Give(buf *bytes.Buffer) error { + if buf.Len() != pool.bufferSize { + return errors.New("Gave an incorrectly sized buffer to the pool.") + } + + buf.Reset() + slice := buf.Bytes() + return pool.GiveSlice(slice[:buf.Len()]) +} + +// GiveSlice is used to attempt to return a slice to the pool. It may not +// be added to the pool if it was already full. +func (pool *BufferPool) GiveSlice(slice []byte) error { + select { + case pool.pool <- slice: + // Everything went smoothly! + default: + return errors.New("Gave a buffer to a full pool.") + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go new file mode 100644 index 0000000..809c806 --- /dev/null +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go @@ -0,0 +1,53 @@ +// Copyright 2013 The Bufferpool Authors. All rights reserved. +// Use of this source code is governed by the BSD 2-Clause license, +// which can be found in the LICENSE file. + +package bufferpool_test + +import ( + "bytes" + "fmt" + "testing" + + "github.com/pushrax/bufferpool" +) + +func TestTakeFromEmpty(t *testing.T) { + bp := bufferpool.New(1, 1) + poolBuf := bp.Take() + if !bytes.Equal(poolBuf.Bytes(), []byte("")) { + t.Fatalf("Buffer from empty bufferpool was allocated incorrectly.") + } +} + +func TestTakeFromFilled(t *testing.T) { + bp := bufferpool.New(1, 1) + + origBuf := bytes.NewBuffer([]byte("X")) + bp.Give(origBuf) + + reusedBuf := bp.Take() + if !bytes.Equal(reusedBuf.Bytes(), []byte("")) { + t.Fatalf("Buffer from filled bufferpool was recycled incorrectly.") + } + + // Compare addresses of the first element in the underlying slice. + if &origBuf.Bytes()[:1][0] != &reusedBuf.Bytes()[:1][0] { + t.Fatalf("Recycled buffer points at different address.") + } +} + +func ExampleNew() { + bp := bufferpool.New(10, 255) + + dogBuffer := bp.Take() + dogBuffer.WriteString("Dog!") + bp.Give(dogBuffer) + + catBuffer := bp.Take() // dogBuffer is reused and reset. + catBuffer.WriteString("Cat!") + + fmt.Println(catBuffer) + // Output: + // Cat! +}