veloxquant_mlx.dsa.ring_buffer module¶
Fixed-capacity circular buffer for streaming, FIFO-evicted token windows.
Implements RingBuffer, a generic O(1)-append circular buffer with
negative indexing and oldest-first iteration, used wherever a component
needs a bounded sliding window over recent elements (e.g. token or key/value
history) that silently evicts the oldest entry once full rather than
growing unbounded.
- class veloxquant_mlx.dsa.ring_buffer.RingBuffer(capacity)[source]¶
Bases:
Generic[T]Fixed-capacity circular buffer with O(1) append and index access.
When full, the oldest element is silently overwritten (FIFO eviction). Supports negative indexing (
buf[-1]is the most recently appended item).- Internal layout:
_data: list of length_capacity, pre-filled withNone._head: index in_dataof the oldest element._size: number of currently live elements.
- Parameters:
capacity (
int) – Maximum number of elements the buffer can hold. Must be >= 1.- Raises:
ValueError – If capacity < 1.
- append(item)[source]¶
Append an item to the buffer.
If the buffer is full the oldest item is evicted and returned.
- to_stacked()[source]¶
Stack all stored MLX arrays along axis 0.
Only valid when
T = mx.array. Usesmx.stackfor efficiency.- Return type:
array- Returns:
Single MLX array of shape (size, *element_shape).
- Raises:
RuntimeError – If the buffer is empty.