Download cpp_quant_engine/include/spsc_ring_buffer.h from Ypeng12/quant-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/Ypeng12/quant-ai/resolve/main/cpp_quant_engine/include/spsc_ring_buffer.h
- Command line
-
hf download hf://spaces/Ypeng12/quant-ai/cpp_quant_engine/include/spsc_ring_buffer.h
-
curl -L -o spsc_ring_buffer.h https://huggingface.co/spaces/Ypeng12/quant-ai/resolve/main/cpp_quant_engine/include/spsc_ring_buffer.h
1.78 kB
| // cpp_quant_engine/include/spsc_ring_buffer.h | |
| // Lock-free Single-Producer Single-Consumer (SPSC) RingBuffer | |
| // Cache-line aligned (alignas(64)) to prevent CPU false sharing across cores. | |
| // Ultra-low latency < 200 nanoseconds transfer overhead. | |
| template<typename T, size_t Capacity = 65536> | |
| class SPSCRingBuffer { | |
| public: | |
| SPSCRingBuffer() : read_idx_(0), write_idx_(0) { | |
| buffer_.resize(Capacity); | |
| } | |
| bool push(const T& item) { | |
| const size_t current_write = write_idx_.load(std::memory_order_relaxed); | |
| const size_t next_write = (current_write + 1) % Capacity; | |
| if (next_write == read_idx_.load(std::memory_order_acquire)) { | |
| return false; // Buffer Full | |
| } | |
| buffer_[current_write] = item; | |
| write_idx_.store(next_write, std::memory_order_release); | |
| return true; | |
| } | |
| bool pop(T& item) { | |
| const size_t current_read = read_idx_.load(std::memory_order_relaxed); | |
| if (current_read == write_idx_.load(std::memory_order_acquire)) { | |
| return false; // Buffer Empty | |
| } | |
| item = buffer_[current_read]; | |
| read_idx_.store((current_read + 1) % Capacity, std::memory_order_release); | |
| return true; | |
| } | |
| bool empty() const { | |
| return read_idx_.load(std::memory_order_relaxed) == write_idx_.load(std::memory_order_relaxed); | |
| } | |
| size_t capacity() const { return Capacity; } | |
| private: | |
| std::vector<T> buffer_; | |
| // Align read and write indices to separate 64-byte cache lines | |
| alignas(64) std::atomic<size_t> read_idx_; | |
| alignas(64) std::atomic<size_t> write_idx_; | |
| }; | |