arena

package
v0.0.0-...-bb1a4e7 Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: GPL-3.0 Imports: 7 Imported by: 0

README

Arena

This package contains multiple implementations of Arena's in pure Go. The goal of them is to reduce GC pressoure and critical stop the world situations.

Who is this for

This library may be interesting to you if you wish to reduce garbage collection (e.g. stop-the-world GC) performance issues in your golang programs, allowing you to switch to explicit byte array memory management techniques.

This can be useful, for example, for long-running server programs that manage lots of in-memory data items, such as caches and databases.

Documentation

Overview

Package arena provides a 100% golang slab allocator for byte slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func String

func String(b []byte) (s string)

String force casts a []byte to a string. USE AT YOUR OWN RISK

func StringPointer

func StringPointer(s string) unsafe.Pointer

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 NilLoc

func NilLoc() Loc

NilLoc returns a Loc where Loc.IsNil() is true.

func (Loc) IsNil

func (cl Loc) IsNil() bool

IsNil returns true if the Loc came from NilLoc().

func (Loc) Slice

func (cl Loc) Slice(bufStart, bufLen int) Loc

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

func (s *SlabArena) AddRef(buf []byte)

AddRef increase the ref count on a buf. The input buf must be from an Alloc() from the same SlabArena.

func (*SlabArena) Alloc

func (s *SlabArena) Alloc(bufLen int) (buf []byte)

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

func (s *SlabArena) BufToLoc(buf []byte) Loc

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

func (s *SlabArena) DecRef(buf []byte) bool

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

func (s *SlabArena) GetNext(buf []byte) (bufNext []byte)

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) LocAddRef

func (s *SlabArena) LocAddRef(loc Loc)

func (*SlabArena) LocDecRef

func (s *SlabArena) LocDecRef(loc Loc)

func (*SlabArena) LocToBuf

func (s *SlabArena) LocToBuf(loc Loc) []byte

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) Owns

func (s *SlabArena) Owns(buf []byte) bool

Owns returns true if this SlabArena owns the buf.

func (*SlabArena) SetNext

func (s *SlabArena) SetNext(buf, bufNext []byte)

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.

func (*SlabArena) Stats

func (s *SlabArena) Stats(m map[string]int64) map[string]int64

Stats fills an input map with runtime metrics about the SlabArena.

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.

Jump to

Keyboard shortcuts

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