项目作者: octu0

项目描述 :
Buffer/Byte/image.RGBA/bufio.Reader/bufio.Writer pool
高级语言: Go
项目地址: git://github.com/octu0/bp.git
创建时间: 2020-08-04T17:36:05Z
项目社区:https://github.com/octu0/bp

开源协议:Apache License 2.0

下载


bp

Apache License
GoDoc
Go Report Card
Releases

bp implements buffer pool of various objects such as byte array ([]byte) or *bytes.Buffer / *image.RGBA and *bufio.Reader.
It is inspired by bpool and its many features are similar.

bp provides the following pool types

  • bp.BufferPool which provides fixed-size pool of *bytes.Buffers
  • bp.BytePool which provides fixed-size pool of []byte slice
  • bp.MmapBytePool Same as BytePool, but uses mmap to allocate the slices
  • bp.BufioReaderPool which provides fixed-size pool of *bufio.Reader
  • bp.BufioWriterPool which provides fixed-size pool of *bufio.Writer
  • bp.ImageRGBAPool which provides fixed-size pool of *image.RGBA
  • bp.ImageYCbCrPool which provides fixed-size pool of *image.YCbCr
  • bp.CopyIOPool which provides fixed-size pool of io.CopyBuffer and io.ReadAll
  • bp.TickerPool which provides fixed-size pool of *time.Ticker
  • bp.TimerPool which provides fixed-size pool of *time.Timer

It also provides a MultiPool to bundle multiple pools:

  • MultiBytePool
  • MultiMmapBytePool
  • MultiBufferPool
  • MultiImageRGBAPool
  • MultiImageYCbCrPool

In addition, bp provides an easy to manipulate object interface to prevent forgetting to put it back into the pool

  • bp.ByteRef
  • bp.BufferRef
  • bp.BufioReaderRef
  • bp.BufioWriterRef
  • bp.ImageRGBARef
  • bp.ImageYCbCrRef

Installation

  1. go get github.com/octu0/bp

Example

Here’s a quick example for using bp.BufferPool. We create a pool of the desired size, call the Get() method to obtain a buffer for use, and call Put(buf) to return the buffer to the pool.

  1. var (
  2. bufpool := bp.NewBufferPool(1000, 128) // capacity 1000 items, each buffer initial 128 Byte pre-sized
  3. )
  4. func main() {
  5. // get buffer from pool
  6. buf := bufpool.Get()
  7. ...
  8. ...
  9. // return buffer to pool
  10. bufpool.Put(buf)
  11. }

Benchmark

bytes.Buffer: sync.Pool vs BufferPool

  1. $ go test -bench=BenchmarkBufferPool -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkBufferPool/default/8-8 1608904 768.9 ns/op 32 B/op 1 allocs/op
  7. BenchmarkBufferPool/default/4096-8 1421576 791.1 ns/op 32 B/op 1 allocs/op
  8. BenchmarkBufferPool/syncpool/8-8 1584180 743.3 ns/op 48 B/op 1 allocs/op
  9. BenchmarkBufferPool/syncpool/4096-8 1505594 798.4 ns/op 48 B/op 1 allocs/op
  10. BenchmarkBufferPool/bufferpool/8-8 1439310 917.2 ns/op 48 B/op 1 allocs/op
  11. BenchmarkBufferPool/bufferpool/4096-8 1225413 967.4 ns/op 48 B/op 1 allocs/op
  12. PASS
  13. ok github.com/octu0/bp 12.309s

[]byte: sync.Pool vs BytePool

  1. $ go test -bench=BenchmarkBytePool -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkBytePool/default/8-8 1827867 653.8 ns/op 16 B/op 1 allocs/op
  7. BenchmarkBytePool/default/4096-8 1562638 788.3 ns/op 16 B/op 1 allocs/op
  8. BenchmarkBytePool/syncpool/8-8 1643428 763.2 ns/op 48 B/op 2 allocs/op
  9. BenchmarkBytePool/syncpool/4096-8 1586283 803.8 ns/op 48 B/op 2 allocs/op
  10. BenchmarkBytePool/bytepool/8-8 1357020 904.1 ns/op 24 B/op 1 allocs/op
  11. BenchmarkBytePool/bytepool/4096-8 1359921 846.6 ns/op 24 B/op 1 allocs/op
  12. PASS
  13. ok github.com/octu0/bp 12.105s

bufio.Reader: sync.Pool vs BufioReaderPool

  1. $ go test -bench=BenchmarkBufioReaderPool -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkBufioReaderPool/default/8-8 1000000 1120 ns/op 1056 B/op 3 allocs/op
  7. BenchmarkBufioReaderPool/default/4096-8 803418 1542 ns/op 5136 B/op 3 allocs/op
  8. BenchmarkBufioReaderPool/syncpool/8-8 1000000 1102 ns/op 1048 B/op 2 allocs/op
  9. BenchmarkBufioReaderPool/syncpool/4096-8 1000000 1110 ns/op 1051 B/op 2 allocs/op
  10. BenchmarkBufioReaderPool/bufiopool/8-8 1000000 1290 ns/op 1160 B/op 4 allocs/op
  11. BenchmarkBufioReaderPool/bufiopool/4096-8 918162 1279 ns/op 1048 B/op 2 allocs/op
  12. PASS
  13. ok github.com/octu0/bp 7.147s

image.RGBA: sync.Pool vs ImageRGBAPool

  1. $ go test -bench=BenchmarkImageRGBAPool -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkImageRGBAPool/default/360-8 31308 36206 ns/op 925814 B/op 3 allocs/op
  7. BenchmarkImageRGBAPool/default/1080-8 4639 821211 ns/op 8282518 B/op 3 allocs/op
  8. BenchmarkImageRGBAPool/syncpool/360-8 1657608 684.7 ns/op 20 B/op 1 allocs/op
  9. BenchmarkImageRGBAPool/syncpool/1080-8 1635321 696.2 ns/op 56 B/op 1 allocs/op
  10. BenchmarkImageRGBAPool/imagepool/360-8 1000000 1190 ns/op 151 B/op 3 allocs/op
  11. BenchmarkImageRGBAPool/imagepool/1080-8 1000000 1105 ns/op 151 B/op 3 allocs/op
  12. PASS
  13. ok github.com/octu0/bp 11.502s

io.Copy vs CopyIOPool.Copy Benchmark

CopyIOPool.Copy to reduce allocation of io.Copy

  1. $ go test -bench=BenchmarkIoCopy -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkIoCopy-8 461892 2637 ns/op 32816 B/op 3 allocs/op
  7. BenchmarkIoCopyPoolDefault-8 619404 1982 ns/op 16608 B/op 7 allocs/op
  8. BenchmarkIoCopyPoolFixedSize-8 2858724 410.9 ns/op 48 B/op 2 allocs/op
  9. PASS
  10. ok github.com/octu0/bp 4.126s

ioutil.ReadAll vs CopyIOPool.ReadAll Benchmark

similarly, CopyIOPool.ReadAll reduces allocation of io.ReadAll

  1. $ go test -bench=BenchmarkIoReadAll -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkIoReadAllIoUtil-8 2628 455815 ns/op 5862972 B/op 30 allocs/op
  7. BenchmarkIoReadAllPoolDefault-8 3057 378599 ns/op 4063444 B/op 13 allocs/op
  8. BenchmarkIoReadAllPoolFixedSize-8 3180 378923 ns/op 4046892 B/op 8 allocs/op
  9. PASS
  10. ok github.com/octu0/bp 3.718s

*time.Ticker: sync.Pool vs TickerPool

  1. $ go test -bench=BenchmarkTickerPool -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkTickerPool/default-8 114298 10731 ns/op 224 B/op 4 allocs/op
  7. BenchmarkTickerPool/syncpool-8 110748 10863 ns/op 32 B/op 1 allocs/op
  8. BenchmarkTickerPool/pool-8 110414 10867 ns/op 32 B/op 1 allocs/op
  9. PASS
  10. ok github.com/octu0/bp 3.994s

*time.Timer: sync.Pool vs TimerPool

  1. $ go test -bench=BenchmarkTimerPool -benchmem ./
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/bp
  5. cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  6. BenchmarkTimerPool/default-8 115437 10889 ns/op 224 B/op 4 allocs/op
  7. BenchmarkTimerPool/syncpool-8 107302 11099 ns/op 32 B/op 1 allocs/op
  8. BenchmarkTimerPool/pool-8 106790 11061 ns/op 32 B/op 1 allocs/op
  9. PASS
  10. ok github.com/octu0/bp 4.002s

License

Apache 2.0, see LICENSE file for details.