{"id":47577464,"url":"https://github.com/ANDRVV/SPSCQueue","last_synced_at":"2026-04-15T03:01:03.086Z","repository":{"id":332408290,"uuid":"1126442186","full_name":"ANDRVV/SPSCQueue","owner":"ANDRVV","description":"A bounded single-producer, single-consumer and lock-free concurrent queue written in Zig, C++17","archived":false,"fork":false,"pushed_at":"2026-04-02T22:12:18.000Z","size":106,"stargazers_count":25,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-03T08:53:36.758Z","etag":null,"topics":["concurrency","concurrent-data-structure","concurrent-queue","cpp","lock-free","queue","spsc-queue","zig"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ANDRVV.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-01T23:19:36.000Z","updated_at":"2026-04-02T22:10:31.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/ANDRVV/SPSCQueue","commit_stats":null,"previous_names":["andrvv/spscqueue"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/ANDRVV/SPSCQueue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANDRVV%2FSPSCQueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANDRVV%2FSPSCQueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANDRVV%2FSPSCQueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANDRVV%2FSPSCQueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ANDRVV","download_url":"https://codeload.github.com/ANDRVV/SPSCQueue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANDRVV%2FSPSCQueue/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31824118,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T18:05:02.291Z","status":"online","status_checked_at":"2026-04-15T02:00:06.175Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrency","concurrent-data-structure","concurrent-queue","cpp","lock-free","queue","spsc-queue","zig"],"created_at":"2026-03-31T06:00:36.040Z","updated_at":"2026-04-15T03:01:03.077Z","avatar_url":"https://github.com/ANDRVV.png","language":"C++","readme":"# SPSCQueue\nA single-producer single-consumer wait-free and lock-free fixed size queue, inspired by [rigtorp's](https://github.com/rigtorp/SPSCQueue/tree/master) implementation and faster than it.\nDesigned to minimize latency and maximize processing speed without overengineering.\n\n## Optimizations\n\n* **Branchless index wrap**:\nUsing `(idx + 1) \u0026 (size - 1)` instead of `if (idx == capacity) idx = 0` eliminates\na branch in the hot path, removing a potential pipeline stall on every push and pop.\nThis comes with a trade-off: capacity must be a power of two. In practice this is a\nreasonable constraint: use `recommendedSlots\u003cT\u003e()` to get a sensible default.\n\n* **`pause` in the spin loop**:\nWhen the queue is empty, the consumer spins with the `pause` instruction. This signals the CPU that the thread is in a spin-wait loop, reducing memory bus contention and improving throughput. The substantial difference is the correct positioning of the pause, see the `pop()` implementation\n\n* **No slot padding**:\nExtra dummy elements at the start and end of the internal array waste memory and reduce cache density.\nSlots are kept tightly packed in a plain `std::vector`, making them more likely to remain in cache under load.\n\n* **No slack slot**:\nKeeping a sentinel slot always empty wastes capacity. The distinction between full and\nempty is implicit in the difference between the two cursors, with no wasted slot.\n\n**See the resulting GCC x86-64 assembly on https://godbolt.org/z/xzxTjf6nW**\n\n# Benchmarks\n\nTo run local benchmark:\n* For **Zig** implementation: `zig run src/zig/benchmark.zig -O ReleaseFast -fomit-frame-pointer`\n* For **C++** implementation: `g++ src/cpp/benchmark.cpp -o benchmark -O3; ./benchmark`\n\nBenchmarked on `Intel i7-12700H` with WSL2:\n\n| Queue (C++ version)        | Throughput (ops/ms) | Latency RTT (ns) |\n| -------------------------- | ------------------: | ---------------: |\n| SPSCQueue (Andrea Vaccaro) |       (p50)  943597 |        (p50) 142 |\n| SPSCQueue (Andrea Vaccaro) |       (p90) 1049140 |        (p90) 147 |\n| SPSCQueue (Andrea Vaccaro) |       (p99) 1068306 |        (p99) 152 |\n\n| Queue (Zig version)        | Throughput (ops/ms) | Latency RTT (ns) |\n| -------------------------- | ------------------: | ---------------: |\n| SPSCQueue (Andrea Vaccaro) |       (p50) 1008548 |        (p50) 179 |\n| SPSCQueue (Andrea Vaccaro) |       (p90) 1350722 |        (p90) 189 |\n| SPSCQueue (Andrea Vaccaro) |       (p99) 1357763 |        (p99) 190 |\n\nOther queues (recommended to benchmark with [rigtorp' benchmark](https://github.com/rigtorp/SPSCQueue/blob/master/src/SPSCQueueBenchmark.cpp)):\n\n| Queue                 | Throughput (ops/ms) | Latency RTT (ns) |\n| --------------------- | ------------------: | ---------------: |\n| SPSCQueue (rigtorp)   |        (avg) 166279 |        (avg) 206 |\n| boost::lockfree::spsc |        (avg) 258024 |        (avg) 224 |\n\n# Zig\n**Example**\n```zig\nconst slots: u64 = queue.recommendedSlots(u64);\n\nvar q: queue.SPSCQueue(u64) = try .initCapacity(allocator, slots);\ndefer q.deinit(allocator);\n\nvar i: u64 = 0;\nwhile (i \u003c 1000) : (i += 1) {\n    q.push(i);\n    _ = q.pop();\n}\n```\n\n**API**\n```zig\npub fn initBuffer(buf: []T) Self\npub fn initCapacity(allocator: std.mem.Allocator, slots: usize) error{OutOfMemory}!Self\npub fn deinit(self: *Self, allocator: std.mem.Allocator) void\n\npub fn push(self: *Self, value: T) void // blocking push\npub fn pop(self: *Self) T // blocking pop\npub fn tryPush(self: *Self, value: T) bool // non-blocking push\npub fn tryPop(self: *Self) ?T // non-blocking pop\n\npub inline fn count(self: *Self) usize\npub inline fn isEmpty(self: *Self) bool\n```\n\n# C++\n**Example**\n```cpp\nsize_t slots = recommendedSlots\u003cuint64_t\u003e();\n\nSPSCQueue\u003cuint64_t\u003e q(slots);\n\nfor (uint64_t i = 0; i \u003c 1000; ++i) {\n    q.push(i);\n    auto value = q.pop();\n    (void)value;\n}\n```\n\n**API**\n```cpp\ntemplate\u003ctypename T\u003e\nstatic constexpr size_t recommendedSlots();\ntemplate\u003ctypename T\u003e\nexplicit SPSCQueue(size_t slots);\n\nvoid skip(); /* pop() without returning value */\nvoid push(const T\u0026 value); /* blocking push */\nT pop(); /* blocking pop */\nbool tryPush(const T\u0026 value); /* non-blocking push */\nbool tryPop(T\u0026 out); /* non-blocking pop */\n\nsize_t count() const;\nbool isEmpty() const;\n```\n\n## About\n\nThis project was created by Andrea Vaccaro \u003c[vaccaro.andrea45@gmail.com](mailto:vaccaro.andrea45@gmail.com)\u003e.\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FANDRVV%2FSPSCQueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FANDRVV%2FSPSCQueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FANDRVV%2FSPSCQueue/lists"}