Documentation
¶
Index ¶
- Variables
- func Append(buf []byte, elems ...byte) []byte
- func AppendString(buf []byte, elems string) []byte
- func Bytes(len, cap int) (b []byte)
- func Clone(buf []byte) []byte
- func Get(size int) []byte
- func GetWithStats() bool
- func InitDefaultPools(minSize, maxSize int)
- func Make(capacity int) []byte
- func Make64(capacity uint64) []byte
- func MakeMax() []byte
- func MakeMin() []byte
- func MaxSize() int
- func MinSize() int
- func New(size int) []byte
- func New64(size uint64) []byte
- func NewBytes(bs []byte) []byte
- func NewMax() []byte
- func NewMin() []byte
- func NewString(s string) []byte
- func Put(buf []byte)
- func Release(buf []byte) bool
- func RuntimeStats(ps ...*CapacityPools) map[string]uint64
- func SetWithStats(t bool)
- type BufPool
- type CapacityPools
- func (p *CapacityPools) Append(buf []byte, elems ...byte) []byte
- func (p *CapacityPools) AppendString(buf []byte, elems string) []byte
- func (p *CapacityPools) Clone(buf []byte) []byte
- func (p *CapacityPools) Get(size int) []byte
- func (p *CapacityPools) GetWithStats() bool
- func (p *CapacityPools) Make(capacity int) []byte
- func (p *CapacityPools) Make64(capacity uint64) []byte
- func (p *CapacityPools) MakeMax() []byte
- func (p *CapacityPools) MakeMin() []byte
- func (p *CapacityPools) MaxSize() int
- func (p *CapacityPools) MinSize() int
- func (p *CapacityPools) New(size int) (buf []byte)
- func (p *CapacityPools) New64(size uint64) []byte
- func (p *CapacityPools) NewBytes(bs []byte) []byte
- func (p *CapacityPools) NewMax() []byte
- func (p *CapacityPools) NewMin() []byte
- func (p *CapacityPools) NewString(s string) []byte
- func (p *CapacityPools) Put(buf []byte)
- func (p *CapacityPools) Release(buf []byte) bool
- func (p *CapacityPools) SetWithStats(t bool)
- type PoolStat
- type RuntimeSummary
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultCapacityPools = NewCapacityPools(defaultMinSize, defaultMaxSize)
Functions ¶
func AppendString ¶ added in v1.2.0
func Bytes ¶ added in v1.4.0
Bytes allocates a byte slice but does not clean up the memory it references. Throw a fatal error instead of panic if cap is greater than runtime.maxAlloc. NOTE: MUST set any byte element before it's read. Ref: xiaost/bytedance-gopkg
func GetWithStats ¶ added in v1.5.0
func GetWithStats() bool
GetWithStats returns the current status of statistics collection. When true, statistics are being collected. When false (default), statistics are not being collected.
func InitDefaultPools ¶
func InitDefaultPools(minSize, maxSize int)
InitDefaultPools initialize to the default pool.
func New ¶
Example ¶
package main
import (
"fmt"
"github.com/fufuok/bytespool"
)
func main() {
// Get() is the same as New()
bs := bytespool.Get(1024)
// len: 1024, cap: 1024
fmt.Printf("len: %d, cap: %d\n", len(bs), cap(bs))
// Put() is the same as Release(), Put it back into the pool after use
bytespool.Put(bs)
// len: 0, cap: 4 (Specified capacity, automatically adapt to the capacity scale)
bs3 := bytespool.Make(3)
bs3 = append(bs3, "123"...)
fmt.Printf("len: %d, cap: %d, %s\n", len(bs3), cap(bs3), bs3)
bytespool.Release(bs3)
// len: 4, cap: 4 (Fixed length)
bs4 := bytespool.New(4)
// Reuse of bs3
fmt.Printf("same array: %v\n", &bs3[0] == &bs4[0])
// Contain old data
fmt.Printf("bs3: %s, bs4: %s\n", bs3, bs4[:3])
copy(bs4, "xy")
fmt.Printf("len: %d, cap: %d, %s\n", len(bs4), cap(bs4), bs4[:3])
bytespool.Release(bs4)
}
Output: len: 1024, cap: 1024 len: 3, cap: 4, 123 same array: true bs3: 123, bs4: 123 len: 4, cap: 4, xy3
func RuntimeStats ¶ added in v1.3.0
func RuntimeStats(ps ...*CapacityPools) map[string]uint64
RuntimeStats returns runtime statistics for byte pools. The statistics include: - NewBytes: total bytes newly allocated for pools - OutBytes: total bytes allocated outside pools - OutCount: total number of bytes allocated outside pools - ReusedBytes: total bytes reused from pools
The statistics collection can be enabled/disabled with SetWithStats(). When disabled (default), all counters will be zero.
func SetWithStats ¶ added in v1.5.0
func SetWithStats(t bool)
SetWithStats enables or disables statistics collection. When enabled, statistics will be collected, but this may affect performance. When disabled (default), all atomic operations for statistics are skipped for better performance. This function is not thread-safe and should be called before any pool operations.
Types ¶
type BufPool ¶ added in v1.0.1
type BufPool struct {
// contains filtered or unexported fields
}
BufPool implements the httputil.BufferPool interface.
func NewBufPool ¶ added in v1.0.1
type CapacityPools ¶
type CapacityPools struct {
// contains filtered or unexported fields
}
func NewCapacityPools ¶
func NewCapacityPools(minSize, maxSize int) *CapacityPools
NewCapacityPools divide into multiple pools according to the capacity scale. Maximum range of byte slice pool: [minCapacity,math.MaxInt32]
func (*CapacityPools) Append ¶ added in v1.2.0
func (p *CapacityPools) Append(buf []byte, elems ...byte) []byte
Append similar to the built-in function to append elements to the end of a slice. If there is insufficient capacity, a new underlying array is allocated and the old array is reclaimed.
func (*CapacityPools) AppendString ¶ added in v1.2.0
func (p *CapacityPools) AppendString(buf []byte, elems string) []byte
func (*CapacityPools) Clone ¶ added in v1.2.0
func (p *CapacityPools) Clone(buf []byte) []byte
Clone return a copy of the byte slice
func (*CapacityPools) Get ¶ added in v1.0.1
func (p *CapacityPools) Get(size int) []byte
func (*CapacityPools) GetWithStats ¶ added in v1.5.0
func (p *CapacityPools) GetWithStats() bool
GetWithStats returns the current status of statistics collection for this pool. When true, statistics are being collected. When false (default), statistics are not being collected.
func (*CapacityPools) Make ¶
func (p *CapacityPools) Make(capacity int) []byte
Make return a byte slice of length 0.
func (*CapacityPools) Make64 ¶ added in v0.0.2
func (p *CapacityPools) Make64(capacity uint64) []byte
func (*CapacityPools) MakeMax ¶ added in v0.0.2
func (p *CapacityPools) MakeMax() []byte
func (*CapacityPools) MakeMin ¶ added in v0.0.2
func (p *CapacityPools) MakeMin() []byte
func (*CapacityPools) MaxSize ¶ added in v1.2.0
func (p *CapacityPools) MaxSize() int
func (*CapacityPools) MinSize ¶ added in v1.2.0
func (p *CapacityPools) MinSize() int
func (*CapacityPools) New ¶
func (p *CapacityPools) New(size int) (buf []byte)
New return byte slice of the specified size. Warning: may contain old data. Warning: returned buf is never equal to nil
func (*CapacityPools) New64 ¶ added in v0.0.2
func (p *CapacityPools) New64(size uint64) []byte
func (*CapacityPools) NewBytes ¶ added in v1.1.1
func (p *CapacityPools) NewBytes(bs []byte) []byte
NewBytes returns a byte slice of the specified content.
func (*CapacityPools) NewMax ¶ added in v0.0.2
func (p *CapacityPools) NewMax() []byte
func (*CapacityPools) NewMin ¶ added in v0.0.2
func (p *CapacityPools) NewMin() []byte
func (*CapacityPools) NewString ¶ added in v1.1.1
func (p *CapacityPools) NewString(s string) []byte
NewString returns a byte slice of the specified content.
func (*CapacityPools) Put ¶ added in v1.0.1
func (p *CapacityPools) Put(buf []byte)
func (*CapacityPools) Release ¶
func (p *CapacityPools) Release(buf []byte) bool
Release put it back into the byte pool of the corresponding scale. Buffers smaller than the minimum capacity or larger than the maximum capacity are discarded.
func (*CapacityPools) SetWithStats ¶ added in v1.5.0
func (p *CapacityPools) SetWithStats(t bool)
SetWithStats enables or disables statistics collection for this pool. When enabled, statistics will be collected, but this may affect performance. When disabled (default), all atomic operations for statistics are skipped for better performance. This function is not thread-safe and should be called before any pool operations.
type PoolStat ¶ added in v1.5.0
PoolStat represents a pool statistic entry
func PoolReuseStats ¶ added in v1.5.0
func PoolReuseStats(topN int, ps ...*CapacityPools) []PoolStat
PoolReuseStats returns the top N pool reuse statistics (by reuse hits). If n <= 0 it returns an empty slice.
type RuntimeSummary ¶ added in v1.5.0
type RuntimeSummary struct {
NewBytes uint64 // total bytes newly allocated for pools
OutBytes uint64 // total bytes allocated outside pools
OutCount uint64 // total number of bytes allocated outside pools
ReusedBytes uint64 // total bytes reused from pools
TopPools []PoolStat // top pools by reuse hits (ranked)
}
RuntimeSummary is a structured summary of runtime pool statistics. It contains global byte counters and the top pools by reuse hits.
func RuntimeStatsSummary ¶ added in v1.5.0
func RuntimeStatsSummary(topN int, ps ...*CapacityPools) RuntimeSummary
RuntimeStatsSummary returns a structured RuntimeSummary for the provided CapacityPools (or the default pools when none provided).
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
auto_reuse
command
|
|
|
buffer/Reader
command
|
|
|
buffer/ReaderFrom
command
|
|
|
buffer/WriteCloser
command
|
|
|
buffer/WriterTo
command
|
|
|
buffer/quickstart
command
|
|
|
buffer/special
command
|
|
|
buffer/stats
command
|
|
|
bufpool
command
|
|
|
custom_pools
command
|
|
|
quickstart
command
|
|
|
reset_default
command
|
|
|
reverse_proxy
command
|
|
|
stats
command
|
|
|
warning
command
|
|