bytespool

package module
v0.0.0-...-670ac55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2022 License: MIT Imports: 4 Imported by: 0

README

bytespool

GoDoc

feature

  • reuse byte slice based on the range of its length.

  • reclaim the original byte slice when the buffer grows.

benchmark

plan one
go test github.com/wencan/bytespool -bench=. -benchmem
BenchmarkBufferWriteStrings-16        	100000000	        17.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkBufferWriteRandomTop1K-16    	50000000	        25.1 ns/op	       0 B/op	       0 allocs/op
plan two
 go test github.com/wencan/go-benchmark/... -bench=. -benchmem
BenchmarkGenericBuf-16                         	 2000000	       735 ns/op	    2576 B/op	       4 allocs/op
BenchmarkGenericStackBuf-16                    	 2000000	       768 ns/op	    2576 B/op	       4 allocs/op
BenchmarkAllocBuf-16                           	 2000000	       773 ns/op	    2576 B/op	       4 allocs/op
BenchmarkSyncPoolBuf-16                        	100000000	        15.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkBpoolPoolBuf-16                       	 2000000	       814 ns/op	       0 B/op	       0 allocs/op
BenchmarkByteBufferPoolBuf-16                  	100000000	        28.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkEasyJsonBuffer-16                     	10000000	       180 ns/op	     609 B/op	       4 allocs/op
BenchmarkEasyJsonBuffer_OptimizedConfig-16     	50000000	        29.5 ns/op	      32 B/op	       1 allocs/op
BenchmarkBytesPoolBuffer-16                    	100000000	        15.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkBytesPoolBuffer_OptimizedConfig-16    	100000000	        15.5 ns/op	       0 B/op	       0 allocs/op

usage

bytes
bytes := bytespool.GetBytes(100)
fmt.Printf("len: %d, cap: %d", len(bytes), cap(bytes))
bytespool.PutBytes(bytes)

output:

len: 100, cap: 128
buffer
buffer := bytespool.GetBuffer()
defer bytespool.PutBuffer(buffer)

_, err := buffer.Write([]byte{0, 1, 2, 3})
fmt.Printf("len: %d, cap: %d\n", buffer.Len(), buffer.Cap())

// auto growth
_, err = buffer.Write([]byte{4, 5, 6, 7, 8, 9})
fmt.Printf("len: %d, cap: %d\n", buffer.Len(), buffer.Cap())

// read
buff := bytespool.GetBytes(10)
defer bytespool.PutBytes(buff)
nRead, err := buffer.Read(buff)
buff = buff[:nRead]

fmt.Printf("data: %X", buff)

Output:

len: 4, cap: 4
len: 10, cap: 16
data: 00010203040506070809

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultBufferMinGrowLength is the default value of Buffer.MinGrowLength.
	DefaultBufferMinGrowLength = 64

	// DefaultBufferReadBuffLength is the default value of Buffer.ReadBuffLength.
	DefaultBufferReadBuffLength = 512

	// DefaultBufferReserveLength is the default value of Buffer..
	DefaultBufferReserveLength = 1024

	// BufferPool is the pool of Buffer instance.
	BufferPool Pool = &sync.Pool{
		New: func() interface{} {
			return new(Buffer)
		},
	}
)
View Source
var (
	// DefaultBytesPool is the default instance of BytesPool.
	DefaultBytesPool = &BytesPool{}

	// EmptyBytes represents empty bytes
	EmptyBytes = make([]byte, 0)

	// DefaultSizedBytesPoolFactory is a default factory for producing SizedBytesPool instance.
	DefaultSizedBytesPoolFactory = func(size int) Pool {
		return &sync.Pool{
			New: func() interface{} {
				return make([]byte, 0, size)
			},
		}
	}
)

Functions

func GetBytes

func GetBytes(length int) []byte

GetBytes is a quick method for DefaultBytesPool.Get.

Example
package main

import (
	"fmt"

	"github.com/wencan/bytespool"
)

func main() {
	bytes := bytespool.GetBytes(100)
	fmt.Printf("len: %d, cap: %d", len(bytes), cap(bytes))
	bytespool.PutBytes(bytes)

}
Output:
len: 100, cap: 128

func PutBuffer

func PutBuffer(buffer *Buffer)

PutBuffer reset and release buffer.

func PutBytes

func PutBytes(bytes []byte)

PutBytes is a quick method for DefaultBytesPool.Put.

Types

type Buffer

type Buffer struct {
	// BytesPool is a pool of bytes of buffer.
	// default is DefaultBytesPool.
	BytesPool SizedBytesPool

	// MinGrowLength is a minimum length of the buffer growth.
	// default is BufferDefaultMinGrowLength.
	MinGrowLength int

	// ReadBuffLength is a buff length of the Buffer.ReadFrom.
	// default value is BufferDefaultReadBuffLength.
	ReadBuffLength int

	// ReserveLength represents the buffer will reserve the bytes
	// if the capacity of the bytes is equal to less than this value.
	// default is DefaultBufferReserveLength.
	ReserveLength int
	// contains filtered or unexported fields
}

Buffer get bytes from pool and put idle bytes to pool.

Example
package main

import (
	"fmt"

	"github.com/wencan/bytespool"
)

func main() {
	buffer := bytespool.GetBuffer()
	defer bytespool.PutBuffer(buffer)

	_, err := buffer.Write([]byte{0, 1, 2, 3})
	_ = err
	fmt.Printf("len: %d, cap: %d\n", buffer.Len(), buffer.Cap())

	// auto growth
	_, err = buffer.Write([]byte{4, 5, 6, 7, 8, 9})
	_ = err
	fmt.Printf("len: %d, cap: %d\n", buffer.Len(), buffer.Cap())

	// read
	buff := bytespool.GetBytes(10)
	defer bytespool.PutBytes(buff)
	nRead, err := buffer.Read(buff)
	_ = err
	buff = buff[:nRead]

	fmt.Printf("data: %X", buff)

}
Output:
len: 4, cap: 64
len: 10, cap: 64
data: 00010203040506070809

func GetBuffer

func GetBuffer() *Buffer

GetBuffer acquire a buffer at default bytes pool.

func (*Buffer) Bytes

func (buffer *Buffer) Bytes() []byte

Bytes returns bytes of buffer.

func (*Buffer) Cap

func (buffer *Buffer) Cap() int

Cap returns the capacity of the bytes of buffer.

func (*Buffer) Grow

func (buffer *Buffer) Grow(n int)

Grow grows the buffer's capacity. After Grow(n), at least n bytes can be written to the buffer without another allocation.

func (*Buffer) Len

func (buffer *Buffer) Len() int

Len returns the size of the bytes of buffer.

func (*Buffer) Read

func (buffer *Buffer) Read(p []byte) (int, error)

Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return first value is the number of bytes read. If the buffer has no data to return, return 0, io.EOF.

func (*Buffer) ReadFrom

func (buffer *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed.

func (*Buffer) Reset

func (buffer *Buffer) Reset()

Reset release bytes and reset the buffer status.

func (*Buffer) Write

func (buffer *Buffer) Write(p []byte) (int, error)

Write appends the contents of p to the buffer, growing the buffer as needed.

func (*Buffer) WriteString

func (buffer *Buffer) WriteString(str string) (int, error)

WriteString appends the contents of str to the buffer, growing the buffer as needed. The return first value is the length of str.

func (*Buffer) WriteTo

func (buffer *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo writes data to w until the buffer is drained or an error occurs.

type BytesPool

type BytesPool struct {
	// SizedPoolFactory is a factory for producing SizedBytesPool instance.
	// default is DefaultSizedBytesPoolFactory.
	SizedPoolFactory SizedBytesPoolFactory
	// contains filtered or unexported fields
}

BytesPool represents bytes pool

func (*BytesPool) Get

func (pool *BytesPool) Get(length int) []byte

Get acquire a slice with a len of length and a capacity of at least length.

func (*BytesPool) Put

func (pool *BytesPool) Put(bytes []byte)

Put reset and release a bytes slice.

func (*BytesPool) Reset

func (pool *BytesPool) Reset()

type Pool

type Pool interface {
	Get() interface{}
	Put(x interface{})
}

Pool is the interface of the universal pool.

type SizedBytesPool

type SizedBytesPool interface {
	Get(length int) []byte
	Put([]byte)
}

SizedBytesPool is a interface that represents a pool of sized bytes.

type SizedBytesPoolFactory

type SizedBytesPoolFactory func(length int) Pool

SizedBytesPoolFactory creates the pool for the bytes that length is fixed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL