Documentation
¶
Overview ¶
Package arena provides a 100% golang slab allocator for byte slices.
Index ¶
- func String(b []byte) (s string)
- func StringPointer(s string) unsafe.Pointer
- type ChannelArena
- type Loc
- type SlabArena
- func (s *SlabArena) AddRef(buf []byte)
- func (s *SlabArena) Alloc(bufLen int) (buf []byte)
- func (s *SlabArena) BufToLoc(buf []byte) Loc
- func (s *SlabArena) DecRef(buf []byte) bool
- func (s *SlabArena) GetNext(buf []byte) (bufNext []byte)
- func (s *SlabArena) LocAddRef(loc Loc)
- func (s *SlabArena) LocDecRef(loc Loc)
- func (s *SlabArena) LocToBuf(loc Loc) []byte
- func (s *SlabArena) Owns(buf []byte) bool
- func (s *SlabArena) SetNext(buf, bufNext []byte)
- func (s *SlabArena) Stats(m map[string]int64) map[string]int64
- type StringArena
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StringPointer ¶
StringPointer returns &s[0], which is not allowed in go
Types ¶
type ChannelArena ¶
type ChannelArena chan []byte
ChannelArena is a free list that provides quick access to pre-allocated byte slices, greatly reducing memory churn and effectively disabling GC for these allocations. After the ChannelArena is created, a slice of bytes can be requested by calling Pop(). The caller is responsible for calling Push(), which puts the blocks back in the queue for later usage. The bytes given by Pop() are *not* zeroed, so the caller should only read positions that it knows to have been overwitten. That can be done by shortening the slice at the right place, based on the count of bytes returned by Write() and similar functions.
func NewChannelArena ¶
func NewChannelArena(numBlocks int, blockSize int) ChannelArena
func (ChannelArena) Pop ¶
func (a ChannelArena) Pop() (x []byte)
func (ChannelArena) Push ¶
func (a ChannelArena) Push(x []byte)
func (ChannelArena) PushByte ¶
func (a ChannelArena) PushByte(x byte)
type Loc ¶
type Loc struct {
// contains filtered or unexported fields
}
An opaque reference to bytes managed by an SlabArena. See SlabArena.BufToLoc/LocToBuf(). A Loc struct is GC friendly in that a Loc does not have direct pointer fields into the SlabArena's memory that the GC's scanner must traverse.
func (Loc) Slice ¶
Slice returns a Loc that a represents a different slice of the backing buffer, where the bufStart and bufLen are relative to the backing buffer. Does not change the ref-count of the underlying buffer.
NOTE: Many API's (such as BufToLoc) do not correctly handle Loc's with non-zero bufStart, so please be careful with using sliced Loc's.
type SlabArena ¶
type SlabArena struct {
// contains filtered or unexported fields
}
An SlabArena manages a set of slab classes and memory.
func NewSlabArena ¶
func NewSlabArena(startChunkSize int, slabSize int, growthFactor float64, malloc func(size int) []byte) *SlabArena
NewSlabArena returns an SlabArena to manage byte slice memory based on a slab allocator approach.
The startChunkSize and slabSize should be > 0. The growthFactor should be > 1.0. The malloc() func is invoked when SlabArena needs memory for a new slab. When malloc() is nil, then SlabArena defaults to make([]byte, size).
func (*SlabArena) AddRef ¶
AddRef increase the ref count on a buf. The input buf must be from an Alloc() from the same SlabArena.
func (*SlabArena) Alloc ¶
Alloc may return nil on errors, such as if no more free chunks are available and new slab memory was not allocatable (such as if malloc() returns nil). The returned buf may not be append()'ed to for growth. The returned buf must be DecRef()'ed for memory reuse.
func (*SlabArena) BufToLoc ¶
BufToLoc returns a Loc that represents an SlabArena-managed buf. Does not affect the reference count of the buf. The buf slice must have start position 0 (must not be a sliced Loc with non-zero bufStart).
func (*SlabArena) DecRef ¶
DecRef decreases the ref count on a buf. The input buf must be from an Alloc() from the same SlabArena. Once the buf's ref-count drops to 0, the SlabArena may reuse the buf. Returns true if this was the last DecRef() invocation (ref count reached 0).
func (*SlabArena) GetNext ¶
GetNext returns the next chained buf for the given input buf. The buf's managed by an SlabArena can be chained. The returned bufNext may be nil. When the returned bufNext is non-nil, the caller owns a ref-count on bufNext and must invoke DecRef(bufNext) when the caller is finished using bufNext.
func (*SlabArena) LocToBuf ¶
LocToBuf returns a buf for an SlabArena-managed Loc. Does not affect the reference count of the buf. The Loc may have come from Loc.Slice().
func (*SlabArena) SetNext ¶
SetNext associates the next chain buf following the input buf to be bufNext. The buf's from an SlabArena can be chained, where buf will own an AddRef() on bufNext. When buf's ref-count goes to zero, it will call DecRef() on bufNext. The bufNext may be nil. The bufNext must have start position 0 (or bufStart of 0) with respect to its backing buffer.
type StringArena ¶
type StringArena struct {
// contains filtered or unexported fields
}
StringArena lets you consolidate allocations for a group of strings that have similar life length
func NewStringArena ¶
func NewStringArena(size int) *StringArena
NewStringArena creates an arena of the specified size.
func (*StringArena) NewString ¶
func (sa *StringArena) NewString(b []byte) string
NewString copies a byte slice into the arena and returns it as a string. If the arena is full, it returns a traditional go string.
func (*StringArena) SpaceLeft ¶
func (sa *StringArena) SpaceLeft() int
SpaceLeft returns the amount of space left in the arena.