Documentation
¶
Overview ¶
Package bytebufferpool implements a pool of byte buffers with anti-fragmentation protection.
The pool may waste limited amount of memory due to fragmentation. This amount equals to the maximum total size of the byte buffers in concurrent use.
Index ¶
- func Put(b *ByteBuffer)
- type ByteBuffer
- func (b *ByteBuffer) AppendBool(v bool)
- func (b *ByteBuffer) AppendFloat(f float64, bitSize int)
- func (b *ByteBuffer) AppendInt(i int64)
- func (b *ByteBuffer) AppendTime(t time.Time, layout string)
- func (b *ByteBuffer) AppendUint(i uint64)
- func (b *ByteBuffer) Bytes() []byte
- func (b *ByteBuffer) Cap() int
- func (b *ByteBuffer) Free()
- func (b *ByteBuffer) Len() int
- func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error)
- func (b *ByteBuffer) Reset()
- func (b *ByteBuffer) Set(p []byte)
- func (b *ByteBuffer) SetString(s string)
- func (b *ByteBuffer) String() string
- func (b *ByteBuffer) Write(p []byte) (int, error)
- func (b *ByteBuffer) WriteByte(c byte) error
- func (b *ByteBuffer) WriteString(s string) (int, error)
- func (b *ByteBuffer) WriteTo(w io.Writer) (int64, error)
- type Pool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Put ¶
func Put(b *ByteBuffer)
Put returns byte buffer to the pool.
ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.
Types ¶
type ByteBuffer ¶
type ByteBuffer struct { // B is a byte buffer to use in append-like workloads. // See example code for details. B []byte // contains filtered or unexported fields }
ByteBuffer provides byte buffer, which can be used for minimizing memory allocations.
ByteBuffer may be used with functions appending data to the given []byte slice. See example code for details.
Use Get for obtaining an empty byte buffer.
Example ¶
package main import ( "fmt" "github.com/soyacen/bytebufferpool" ) func main() { bb := bytebufferpool.Get() bb.WriteString("first line\n") bb.Write([]byte("second line\n")) bb.B = append(bb.B, "third line\n"...) fmt.Printf("bytebuffer contents=%q", bb.B) // It is safe to release byte buffer now, since it is // no longer used. bb.Free() }
Output:
func Get ¶
func Get() *ByteBuffer
Get returns an empty byte buffer from the pool.
Got byte buffer may be returned to the pool via Put call. This reduces the number of memory allocations required for byte buffer management.
func (*ByteBuffer) AppendBool ¶ added in v1.0.1
func (b *ByteBuffer) AppendBool(v bool)
AppendBool appends a bool to the underlying buffer.
func (*ByteBuffer) AppendFloat ¶ added in v1.0.1
func (b *ByteBuffer) AppendFloat(f float64, bitSize int)
AppendFloat appends a float to the underlying buffer. It doesn't quote NaN or +/- Inf.
func (*ByteBuffer) AppendInt ¶ added in v1.0.1
func (b *ByteBuffer) AppendInt(i int64)
AppendInt appends an integer to the underlying buffer (assuming base 10).
func (*ByteBuffer) AppendTime ¶ added in v1.0.1
func (b *ByteBuffer) AppendTime(t time.Time, layout string)
AppendTime appends the time formatted using the specified layout.
func (*ByteBuffer) AppendUint ¶ added in v1.0.1
func (b *ByteBuffer) AppendUint(i uint64)
AppendUint appends an unsigned integer to the underlying buffer (assuming base 10).
func (*ByteBuffer) Bytes ¶
func (b *ByteBuffer) Bytes() []byte
Bytes returns b.B, i.e. all the bytes accumulated in the buffer.
The purpose of this function is bytes.Buffer compatibility.
func (*ByteBuffer) Cap ¶ added in v1.0.1
func (b *ByteBuffer) Cap() int
Cap returns the capacity of the underlying byte slice.
func (*ByteBuffer) Free ¶ added in v1.0.1
func (b *ByteBuffer) Free()
Free returns the Buffer to its Pool.
Callers must not retain references to the Buffer after calling Free.
func (*ByteBuffer) ReadFrom ¶
func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error)
ReadFrom implements io.ReaderFrom.
The function appends all the data read from r to b.
func (*ByteBuffer) SetString ¶
func (b *ByteBuffer) SetString(s string)
SetString sets ByteBuffer.B to s.
func (*ByteBuffer) String ¶
func (b *ByteBuffer) String() string
String returns string representation of ByteBuffer.B.
func (*ByteBuffer) Write ¶
func (b *ByteBuffer) Write(p []byte) (int, error)
Write implements io.Writer - it appends p to ByteBuffer.B
func (*ByteBuffer) WriteByte ¶
func (b *ByteBuffer) WriteByte(c byte) error
WriteByte appends the byte c to the buffer.
The purpose of this function is bytes.Buffer compatibility.
The function always returns nil.
func (*ByteBuffer) WriteString ¶
func (b *ByteBuffer) WriteString(s string) (int, error)
WriteString appends s to ByteBuffer.B.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool represents byte buffer pool.
Distinct pools may be used for distinct types of byte buffers. Properly determined byte buffer types with their own pools may help reducing memory waste.
func (*Pool) Get ¶
func (p *Pool) Get() *ByteBuffer
Get returns new byte buffer with zero length.
The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.
func (*Pool) Put ¶
func (p *Pool) Put(b *ByteBuffer)
Put releases byte buffer obtained via Get to the pool.
The buffer mustn't be accessed after returning to the pool.