ringbuf

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

package ringbuf Cache-line-aligned, lock-free MPMC ring buffer. Every slot occupies exactly one 64-byte cache line so producers and consumers never false-share. Power-of-two capacity; Vyukov MPMC queue discipline adapted for fixed-size slots.

Zero allocation on Push/Pop/BatchDrain in steady state. No locks. The only allocation is the backing slices in New.

Slot fields are deliberately generic: Seq and Timestamp are owned by the ring, everything else is caller-defined. If a caller needs more than 8 bytes of payload, store a handle (index, hash, offset) in Payload and keep the bulk data in a side buffer the caller owns. A fixed-size slot is the entire point; variable-size payloads defeat it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer added in v0.9.2

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

Buffer is a simple mutex-guarded ring buffer for T.

func NewBuffer added in v0.9.2

func NewBuffer[T any](capacity int) *Buffer[T]

func (*Buffer[T]) Push added in v0.9.2

func (b *Buffer[T]) Push(item T)

func (*Buffer[T]) Snapshot added in v0.9.2

func (b *Buffer[T]) Snapshot() []T

type Ring

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

Ring is a fixed-capacity MPMC ring buffer. Use New.

head and tail are padded to cache lines so concurrent producers and consumers do not false-share the two counters.

func New

func New(capacity int) *Ring

New allocates a Ring. A capacity below 2 is replaced with defaultCapacity (1024); otherwise capacity is rounded up to the next power of two and clamped to maxCapacity. The smallest ring obtainable is New(2).

func (*Ring) BatchDrain

func (r *Ring) BatchDrain(dst []Slot) int

BatchDrain pops up to len(dst) slots. Returns the number drained.

func (*Ring) Cap

func (r *Ring) Cap() int

Cap returns the ring's fixed capacity (a power of two, >= 2).

func (*Ring) Len

func (r *Ring) Len() int

Len returns a best-effort snapshot of queued slots; under concurrent Push/Pop it may be stale by the time it returns. Do not use it for correctness-critical decisions.

func (*Ring) Pop

func (r *Ring) Pop(dst *Slot) bool

Pop dequeues one slot into dst. Returns false when the buffer is empty.

func (*Ring) Push

func (r *Ring) Push(s *Slot) bool

Push enqueues one slot. Returns false when the buffer is full. The caller's Slot is copied by value; the caller may reuse it immediately.

type Slot

type Slot struct {
	Seq       uint64   // offset  0 — written by the ring
	Timestamp int64    // offset  8 — caller-defined
	A         int64    // offset 16 — caller-defined
	B         int64    // offset 24 — caller-defined
	ID        [16]byte // offset 32 — caller-defined binary identifier
	Status    uint16   // offset 48 — caller-defined
	Kind      uint8    // offset 50 — caller-defined

	Payload [8]byte // offset 56 — caller-defined short payload
	// contains filtered or unexported fields
}

Slot is exactly one cache line (64 bytes on all supported architectures). Size is enforced by a compile-time assertion below.

Jump to

Keyboard shortcuts

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