- Notifications
You must be signed in to change notification settings - Fork0
Fast bounded SPSC queue written in Zig
License
freref/spsc-queue
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A single producer single consumer wait-free and lock-free fixed size queue written in Zig. Inspired byrigtorp's implementation in C++. This implementation is faster thanrigtorp/SPSCQueue,boost::lockfree::spsc,cdolan/zig-spsc-ring, andfolly::ProducerConsumerQueue.
This library provides amanaged and anunmanaged version of the queue, following the Zig standard library conventions. There are2 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 theenforce_po2 flag totrue 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 fromjoadnacer/atomic_queue.
You can find a basic examplehere. You can run this example with the following command:
zig build run-example
Unmanaged version:
pubfninitBuffer(buffer: []T)SelfpubfninitCapacity(allocator:std.mem.Allocator,num:usize)!Selfpubfndeinit(self:*Self,allocator:std.mem.Allocator)void
Managed version:
pubfninitCapacity(allocator:std.mem.Allocator,num:usize)!SelfpubfnfromOwnedSlice(allocator:std.mem.Allocator,buffer: []T)Selfpubfndeinit(self:*Self)void
General API:
pubfnisEmpty(self:*Self)boolpubfnsize(self:*Self)usizepubfnpush(self:*Self,value:T)voidpubfntryPush(self:*Self,value:T)boolpubfnfront(self:*Self)?*Tpubfnpop(self:*Self)void
I made a seperate repo for benchmarking various SPSC queue implementations, more info on the benchmarks can be foundthere. 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.
About
Fast bounded SPSC queue written in Zig
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.