Documentation ¶
Overview ¶
Package bytearray provides a fixed size chunk []byte array with slab allocation. ByteArrays utilize next-chunk-linking which provides full reader/writer compatibility with any size you need. It uses an automatic chunk allocation with manual deallocation, which means that all ByteArrays must be manually released when not used anymore. The slab and chunk sizes are globally configurable and must be done before any chunks are allocated.
Index ¶
- Constants
- Variables
- func ChunkQuantize(size int64) int64
- func DisableAutoGC()
- func EnableAutoGC(releaseInterval int, maxAge int)
- func GC(maxAge int)
- func Setup(chunkSize int, slabSize int, maxSlabs int)
- func Stats() (AllocatedSlabs int64, GrabbedChunks int64, ReleasedChunks int64, ...)
- type ByteArray
- func (b ByteArray) Len() int
- func (b *ByteArray) Read(p []byte) (n int, err error)
- func (b *ByteArray) ReadFrom(r io.Reader) (n int64, err error)
- func (b ByteArray) ReadPosition() int
- func (b *ByteArray) ReadSeek(offset int, whence int) (absolute int, err error)
- func (b *ByteArray) ReadSlice() ([]byte, error)
- func (b *ByteArray) Release()
- func (b *ByteArray) Split(offset int) (newArray ByteArray)
- func (b *ByteArray) Truncate(offset int) int
- func (b *ByteArray) Write(p []byte) (n int, err error)
- func (b ByteArray) WritePosition() int
- func (b *ByteArray) WriteSeek(offset int, whence int) int
- func (b *ByteArray) WriteSlice() []byte
- func (b *ByteArray) WriteTo(w io.Writer) (n int64, err error)
Constants ¶
Variables ¶
var ChunkSize int = 2048
ChunkSize defaults to 2048 bytes (2KiB) chunks. Use Setup function to change.
var MaxSlabs int = 4097 // +1 because slab 0 is never used (or allocated), it is reserved for emptyLocation pointers
MaxSlabs defaults to 4096 slabs (16GiB memory with default SlabSize and ChunkSize). Use Setup function to change.
var SlabSize int = ChunkSize * 2048
SlabSize defaults to 2048 slabs (4MiB memory with default ChunkSize). Use Setup function to change.
Functions ¶
func ChunkQuantize ¶
ChunkQuantize takes a size and round it up to an even chunk size (this can be used to calculate used memory)
func EnableAutoGC ¶
EnableAutoGC enables the automatic GC goroutine releaseInterval is how often the GC is run. maxAge is the number of seconds since the slab was touched before it can be freed
func GC ¶
func GC(maxAge int)
Not really a GC, more of a slab releaser in case it has not been used for a while. maxAge is the number of seconds since the slab was touched before it can be freed
Types ¶
type ByteArray ¶
type ByteArray struct {
// contains filtered or unexported fields
}
Byte array read and write is not concurrency safe however the underlying slab structures are so you can use multiple ByteArrays at the same time
func (*ByteArray) Read ¶
Read from the byte array into a buffer and advance the current read position
func (*ByteArray) ReadFrom ¶
ReadFrom reads from the Reader until EOF and fills up the ByteArray (at the current write position)
func (ByteArray) ReadPosition ¶
ReadPosition returns the current write position
func (*ByteArray) ReadSlice ¶
ReadSlice returns a byte slice chunk for the current read position (it does not advance read position)
func (*ByteArray) Release ¶
func (b *ByteArray) Release()
Release will release all chunks associated with the ByteArray
func (*ByteArray) Split ¶
Split a byte array into a new ByteArray at the specified offset, the old byte array will be truncated at the split offset
func (*ByteArray) Truncate ¶
Truncate sets the length, it also expands the length in case offset > usedBytes
func (*ByteArray) Write ¶
Write to the byte array from a buffer and advance the current write position
func (ByteArray) WritePosition ¶
WritePosition returns the current write position
func (*ByteArray) WriteSlice ¶
WriteSlice returns a byte slice chunk for the current write position (it does not advance write position)