bytebuf

package module
v0.0.0-...-cdd4f68 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Introduction

go-bytebuf is a random access buffer implementation. It can also mark/reset read/write indexes, which can be very useful in packet encoding/decoding scenario and many others.

see go doc for detaled usage

go get github.com/joesonw/go-bytebuf

Benchmark

$ go test -bench=.
goos: darwin
goarch: amd64
pkg: github.com/joesonw/bytebuf
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkPooledBuffer/BenchmarkPooledBuffer_512_128-12             82303             13370 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_512_512-12             95144             12129 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_512_1024-12            97935             12389 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_1024_128-12            47499             25463 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_1024_512-12            51002             23594 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_1024_1024-12           51302             23434 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_2048_128-12            24166             50067 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_2048_512-12            25639             46754 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_2048_1024-12           26004             46475 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_4096_128-12            12092            100358 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_4096_512-12            10000            110779 ns/op
BenchmarkPooledBuffer/BenchmarkPooledBuffer_4096_1024-12           12300             93992 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_512_2-12               91994             13662 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_512_4-12               82226             13287 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_1024_2-12              42008             26302 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_1024_4-12              48309             26050 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_2048_2-12              22138             59846 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_2048_4-12              20758             54824 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_4096_2-12               8649            117953 ns/op
BenchmarkDirectBuffer/BenchmarkDirectBuffer_4096_4-12              10000            106439 ns/op
BenchmarkBytesBuffer/BenchmarkBytesBuffer_512-12                   99037             12921 ns/op
BenchmarkBytesBuffer/BenchmarkBytesBuffer_1024-12                  47634             24565 ns/op
BenchmarkBytesBuffer/BenchmarkBytesBuffer_2048-12                  24796             48782 ns/op
BenchmarkBytesBuffer/BenchmarkBytesBuffer_4096-12                  12783             92306 ns/op
PASS
ok      github.com/joesonw/bytebuf      37.231s

Documentation

Index

Constants

View Source
const MinRead = 512

Variables

View Source
var (
	ErrBufferTypeNotMatch = errors.New("target buffer type is not desired")
	ErrInvalidLengthWrote = errors.New("wrote invalid length")
	ErrBufferReleased     = errors.New("buffer is already released")
)

Functions

This section is empty.

Types

type Allocator

type Allocator interface {
	Allocate(capacity int) Buffer
	Release(instrument Instrument) error
	Stat() AllocatorStat
}

func DirectAllocator

func DirectAllocator(options ...DirectAllocatorOptions) Allocator

func PooledAllocator

func PooledAllocator(pageSize int) Allocator

type AllocatorStat

type AllocatorStat interface {
	Memory() int64
	Count() int64
}

type Buffer

type Buffer interface {
	// Instrument get the underlying instrument
	Instrument() Instrument

	// Clear clear marked indexes
	Clear()
	// Release release underlying instrument, this buffer can no longer be used
	Release()
	// Reset reset the buffer to its full capacity, means length = 0
	Reset()
	// Clone clone an entire new buffer, with the same instrument implementation (different instance)
	Clone() Buffer
	// Cap returns current instrument allocated capacity
	Cap() int
	// Len current readable number of bytes
	Len() int
	// Grow grow capacity with at least this much more to write
	Grow(size int)
	// Ensure at least this much capacity
	Ensure(capacity int)
	// DiscardReadBytes discard already read bytes, this will reset marks and indexes
	DiscardReadBytes()
	// Bytes return all readable bytes
	Bytes() []byte
	// UnsafeBytes return all underlying bytes, this may contain discarded bytes
	UnsafeBytes() []byte
	// String return all readable as string
	String() string

	// ReaderIndex current reader index in memory
	ReaderIndex() int
	MarkReaderIndex()
	ResetReaderIndex()
	// WriterIndex current writer index in memory
	WriterIndex() int
	MarkWriterIndex()
	ResetWriterIndex()

	Read(p []byte) (int, error)
	Write(p []byte) (int, error)
	Get(index int, p []byte) (int, error)
	Set(index int, p []byte) (int, error)
	ReadFrom(r io.Reader) (int64, error)
	WriteTo(w io.Writer) (n int64, err error)

	ReadString(n int) (string, error)
	WriteString(s string) (int, error)
	ReadByte() (byte, error)
	WriteByte(b byte) error
	GetByte(index int) (byte, error)
	SetByte(index int, b byte) error

	Int16Buffer
	Uint16Buffer
	Int32Buffer
	Uint32Buffer
	Int64Buffer
	Uint64Buffer
	Float32Buffer
	Float64Buffer
}

type DirectAllocateSizer

type DirectAllocateSizer func(capacity, newCapacity int) int

type DirectAllocatorOptions

type DirectAllocatorOptions func(*directAllocator)

func WithDirectMultiplier

func WithDirectMultiplier(mul float64) DirectAllocatorOptions

func WithDirectStepper

func WithDirectStepper(step int) DirectAllocatorOptions

type DirectAllocatorStat

type DirectAllocatorStat struct {
	// contains filtered or unexported fields
}

func (DirectAllocatorStat) Count

func (s DirectAllocatorStat) Count() int64

func (DirectAllocatorStat) Memory

func (s DirectAllocatorStat) Memory() int64

type Float32Buffer

type Float32Buffer interface {
	ReadFloat32() (float32, error)
	ReadFloat32LE() (float32, error)
	WriteFloat32(v float32) error
	WriteFloat32LE(v float32) error
	GetFloat32(index int) (float32, error)
	GetFloat32LE(index int) (float32, error)
	SetFloat32(index int, v float32) error
	SetFloat32LE(index int, v float32) error
}

type Float64Buffer

type Float64Buffer interface {
	ReadFloat64() (float64, error)
	ReadFloat64LE() (float64, error)
	WriteFloat64(v float64) error
	WriteFloat64LE(v float64) error
	GetFloat64(index int) (float64, error)
	GetFloat64LE(index int) (float64, error)
	SetFloat64(index int, v float64) error
	SetFloat64LE(index int, v float64) error
}

type Instrument

type Instrument interface {
	// contains filtered or unexported methods
}

type Int16Buffer

type Int16Buffer interface {
	ReadInt16() (int16, error)
	ReadInt16LE() (int16, error)
	WriteInt16(v int16) error
	WriteInt16LE(v int16) error
	GetInt16(index int) (int16, error)
	GetInt16LE(index int) (int16, error)
	SetInt16(index int, v int16) error
	SetInt16LE(index int, v int16) error
}

type Int32Buffer

type Int32Buffer interface {
	ReadInt32() (int32, error)
	ReadInt32LE() (int32, error)
	WriteInt32(v int32) error
	WriteInt32LE(v int32) error
	GetInt32(index int) (int32, error)
	GetInt32LE(index int) (int32, error)
	SetInt32(index int, v int32) error
	SetInt32LE(index int, v int32) error
}

type Int64Buffer

type Int64Buffer interface {
	ReadInt64() (int64, error)
	ReadInt64LE() (int64, error)
	WriteInt64(v int64) error
	WriteInt64LE(v int64) error
	GetInt64(index int) (int64, error)
	GetInt64LE(index int) (int64, error)
	SetInt64(index int, v int64) error
	SetInt64LE(index int, v int64) error
}

type PooledAllocatorStat

type PooledAllocatorStat struct {
	// contains filtered or unexported fields
}

func (PooledAllocatorStat) Count

func (s PooledAllocatorStat) Count() int64

func (PooledAllocatorStat) Memory

func (s PooledAllocatorStat) Memory() int64

func (PooledAllocatorStat) Pages

func (s PooledAllocatorStat) Pages() int64

type Uint16Buffer

type Uint16Buffer interface {
	ReadUint16() (uint16, error)
	ReadUint16LE() (uint16, error)
	WriteUint16(v uint16) error
	WriteUint16LE(v uint16) error
	GetUint16(index int) (uint16, error)
	GetUint16LE(index int) (uint16, error)
	SetUint16(index int, v uint16) error
	SetUint16LE(index int, v uint16) error
}

type Uint32Buffer

type Uint32Buffer interface {
	ReadUint32() (uint32, error)
	ReadUint32LE() (uint32, error)
	WriteUint32(v uint32) error
	WriteUint32LE(v uint32) error
	GetUint32(index int) (uint32, error)
	GetUint32LE(index int) (uint32, error)
	SetUint32(index int, v uint32) error
	SetUint32LE(index int, v uint32) error
}

type Uint64Buffer

type Uint64Buffer interface {
	ReadUint64() (uint64, error)
	ReadUint64LE() (uint64, error)
	WriteUint64(v uint64) error
	WriteUint64LE(v uint64) error
	GetUint64(index int) (uint64, error)
	GetUint64LE(index int) (uint64, error)
	SetUint64(index int, v uint64) error
	SetUint64LE(index int, v uint64) error
}

Jump to

Keyboard shortcuts

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