https://github.com/freref/spsc-queue
Fast bounded SPSC queue written in Zig
https://github.com/freref/spsc-queue
spsc-queue zig zig-package
Last synced: 18 days ago
JSON representation
Fast bounded SPSC queue written in Zig
- Host: GitHub
- URL: https://github.com/freref/spsc-queue
- Owner: freref
- License: mit
- Created: 2025-09-06T21:56:23.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2025-09-08T21:32:54.000Z (9 months ago)
- Last Synced: 2025-12-30T18:54:07.130Z (5 months ago)
- Topics: spsc-queue, zig, zig-package
- Language: Zig
- Homepage:
- Size: 37.1 KB
- Stars: 26
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# spsc-queue
A single producer single consumer wait-free and lock-free fixed size queue written in Zig. Inspired by [rigtorp's](https://github.com/rigtorp/SPSCQueue/tree/master) implementation in C++. This implementation is faster than [rigtorp/SPSCQueue](https://github.com/rigtorp/SPSCQueue/tree/master),
[*boost::lockfree::spsc*](https://www.boost.org/doc/libs/1_76_0/doc/html/boost/lockfree/spsc_queue.html), [cdolan/zig-spsc-ring](https://github.com/cdolan/zig-spsc-ring.git), and [*folly::ProducerConsumerQueue*](https://github.com/facebook/folly/blob/master/folly/docs/ProducerConsumerQueue.md).
## Implementation
This library provides a **managed** and an **unmanaged** version of the queue, following the Zig standard library conventions. There are **2 implementations** of the queue:
- One that uses a slack space in the buffer and allows the user to set any capacity.
- One that enforces power-of-2 (po2) capacity and is faster due to less expensive arithmetic operations.
The user can choose which implementation they want to use by setting the ``enforce_po2`` flag to ``true`` when defining the queue type. I opted for this interface over detecting if the capacity is po2, because the flag makes the choice explicit and known at comptime. It's clear to the user that there are two distinct implementations with different trade-offs. I borrowed this idea from [joadnacer/atomic_queue](https://github.com/joadnacer/atomic_queues.git).
## Usage
You can find a basic example [here](./src/example.zig). You can run this example with the following command:
```sh
zig build run-example
```
**Unmanaged version:**
```zig
pub fn initBuffer(buffer: []T) Self
pub fn initCapacity(allocator: std.mem.Allocator, num: usize) !Self
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void
```
**Managed version:**
```zig
pub fn initCapacity(allocator: std.mem.Allocator, num: usize) !Self
pub fn fromOwnedSlice(allocator: std.mem.Allocator, buffer: []T) Self
pub fn deinit(self: *Self) void
```
**General API:**
```zig
pub fn isEmpty(self: *Self) bool
pub fn size(self: *Self) usize
pub fn push(self: *Self, value: T) void
pub fn tryPush(self: *Self, value: T) bool
pub fn front(self: *Self) ?*T
pub fn pop(self: *Self) void
```
## Benchmarks
I made a seperate repo for benchmarking various SPSC queue implementations, more info on the benchmarks can be found [there](https://github.com/freref/spsc-queue-benchmark/tree/master). These benchmarks are currently not very rigorous, but they give a rudimentary idea of the performance of this implementation compared to others. The benchmarks were run on a MacBook Pro (Apple M4 Pro, 14 cores: 10 performance + 4 efficiency) with 48 GB unified memory. 