ringbuffer

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

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

Go to latest
Published: Jun 28, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

README

ringbuffer

ringBuffer is a Go package that provides an implementation of a ring buffer, also known as a circular buffer. A ring buffer is a data structure that allows efficient storage and retrieval of a fixed-size collection of elements in a circular fashion.

Features

  • Thread-safe implementation: A lock-free ring buffer implementation using atomic operations.
  • Generics ready
  • Flexible capacity: Set the capacity of the ring buffer upon initialization.
  • Simple interface: Push and Pop methods
  • Efficient operations: Constant time complexity O(1).
  • High performance: Designed to be efficient for concurrent operations with minimal synchronization overhead.
  • Memory efficient: uses less memory than container/ring
  • Zero memory allocation (besides the initial buffer)
  • Ring buffer is full on Capacity - 1

Installation

Use go get to install the package:

go get github.com/mauri870/go-ringbuffer

Usage

Godoc

There are a couple of different implementations available:

LockFreeCached // Fastest implementation, Lock free with a cache approach to minimize atomic memory operations
LockFree // Lock free implementation
ContainerRing // Lock free backed by container/ring, intended for use on benchmarks

Benchmarks

go test -bench=. -benchmem=true ./...
goos: linux
goarch: amd64
pkg: github.com/mauri870/go-ringbuffer
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
BenchmarkLockFreePush-16                        652895372                1.793 ns/op
BenchmarkLockFreePop-16                         625940958                1.872 ns/op
BenchmarkLockFreeCachedPush-16                  656273632                1.798 ns/op
BenchmarkLockFreeCachedPop-16                   643739690                1.806 ns/op
BenchmarkLockFreeContainerRingPush-16           411274116                3.015 ns/op
BenchmarkLockFreeContainerRingPop-16            407771115                3.030 ns/op
PASS
ok      github.com/mauri870/go-ringbuffer       12.361s

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockFree

type LockFree[T any] struct {
	// contains filtered or unexported fields
}

func NewLockFree

func NewLockFree[T any](cap int) *LockFree[T]

func (*LockFree[T]) Pop

func (r *LockFree[T]) Pop() (T, bool)

func (*LockFree[T]) Push

func (r *LockFree[T]) Push(val T) bool

type LockFreeCached

type LockFreeCached[T any] struct {
	// contains filtered or unexported fields
}

func NewLockFreeCached

func NewLockFreeCached[T any](cap int) *LockFreeCached[T]

func (*LockFreeCached[T]) Pop

func (r *LockFreeCached[T]) Pop() (T, bool)

func (*LockFreeCached[T]) Push

func (r *LockFreeCached[T]) Push(val T) bool

type LockFreeContainerRing

type LockFreeContainerRing[T any] struct {
	// contains filtered or unexported fields
}

func NewContainerRing

func NewContainerRing[T any](cap int) *LockFreeContainerRing[T]

func (*LockFreeContainerRing[T]) Pop

func (r *LockFreeContainerRing[T]) Pop() (T, bool)

func (*LockFreeContainerRing[T]) Push

func (r *LockFreeContainerRing[T]) Push(val T) bool

type RingBufferer

type RingBufferer[T any] interface {
	Pop() (T, bool)
	Push(T) bool
}

RingBufferer defines an interface for a RingBuffer

func New

func New[T any](cap int) RingBufferer[T]

New creates a new ring buffer. This method returns the LockFreeCached implementation.

Example
buf := New[int](3)

buf.Push(1)
buf.Push(2)
buf.Push(3) // buffer is full!

val, ok := buf.Pop()
fmt.Println(val, ok)
Output:

1 true

Jump to

Keyboard shortcuts

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