forked from ShuriZma/suyu
1
0
Fork 0

Merge pull request #5885 from MerryMage/ring_buffer-granularity

ring_buffer: Remove granularity template argument
This commit is contained in:
bunnei 2021-02-06 13:18:41 -08:00 committed by GitHub
commit 4a01812ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 16 deletions

View File

@ -19,15 +19,14 @@ namespace Common {
/// SPSC ring buffer /// SPSC ring buffer
/// @tparam T Element type /// @tparam T Element type
/// @tparam capacity Number of slots in ring buffer /// @tparam capacity Number of slots in ring buffer
/// @tparam granularity Slot size in terms of number of elements template <typename T, std::size_t capacity>
template <typename T, std::size_t capacity, std::size_t granularity = 1>
class RingBuffer { class RingBuffer {
/// A "slot" is made of `granularity` elements of `T`. /// A "slot" is made of a single `T`.
static constexpr std::size_t slot_size = granularity * sizeof(T); static constexpr std::size_t slot_size = sizeof(T);
// T must be safely memcpy-able and have a trivial default constructor. // T must be safely memcpy-able and have a trivial default constructor.
static_assert(std::is_trivial_v<T>); static_assert(std::is_trivial_v<T>);
// Ensure capacity is sensible. // Ensure capacity is sensible.
static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2 / granularity); static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2);
static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two"); static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two");
// Ensure lock-free. // Ensure lock-free.
static_assert(std::atomic_size_t::is_always_lock_free); static_assert(std::atomic_size_t::is_always_lock_free);
@ -47,7 +46,7 @@ public:
const std::size_t second_copy = push_count - first_copy; const std::size_t second_copy = push_count - first_copy;
const char* in = static_cast<const char*>(new_slots); const char* in = static_cast<const char*>(new_slots);
std::memcpy(m_data.data() + pos * granularity, in, first_copy * slot_size); std::memcpy(m_data.data() + pos, in, first_copy * slot_size);
in += first_copy * slot_size; in += first_copy * slot_size;
std::memcpy(m_data.data(), in, second_copy * slot_size); std::memcpy(m_data.data(), in, second_copy * slot_size);
@ -74,7 +73,7 @@ public:
const std::size_t second_copy = pop_count - first_copy; const std::size_t second_copy = pop_count - first_copy;
char* out = static_cast<char*>(output); char* out = static_cast<char*>(output);
std::memcpy(out, m_data.data() + pos * granularity, first_copy * slot_size); std::memcpy(out, m_data.data() + pos, first_copy * slot_size);
out += first_copy * slot_size; out += first_copy * slot_size;
std::memcpy(out, m_data.data(), second_copy * slot_size); std::memcpy(out, m_data.data(), second_copy * slot_size);
@ -84,9 +83,9 @@ public:
} }
std::vector<T> Pop(std::size_t max_slots = ~std::size_t(0)) { std::vector<T> Pop(std::size_t max_slots = ~std::size_t(0)) {
std::vector<T> out(std::min(max_slots, capacity) * granularity); std::vector<T> out(std::min(max_slots, capacity));
const std::size_t count = Pop(out.data(), out.size() / granularity); const std::size_t count = Pop(out.data(), out.size());
out.resize(count * granularity); out.resize(count);
return out; return out;
} }
@ -113,7 +112,7 @@ private:
alignas(128) std::atomic_size_t m_write_index{0}; alignas(128) std::atomic_size_t m_write_index{0};
#endif #endif
std::array<T, granularity * capacity> m_data; std::array<T, capacity> m_data;
}; };
} // namespace Common } // namespace Common

View File

@ -14,7 +14,7 @@
namespace Common { namespace Common {
TEST_CASE("RingBuffer: Basic Tests", "[common]") { TEST_CASE("RingBuffer: Basic Tests", "[common]") {
RingBuffer<char, 4, 1> buf; RingBuffer<char, 4> buf;
// Pushing values into a ring buffer with space should succeed. // Pushing values into a ring buffer with space should succeed.
for (std::size_t i = 0; i < 4; i++) { for (std::size_t i = 0; i < 4; i++) {
@ -77,7 +77,7 @@ TEST_CASE("RingBuffer: Basic Tests", "[common]") {
} }
TEST_CASE("RingBuffer: Threaded Test", "[common]") { TEST_CASE("RingBuffer: Threaded Test", "[common]") {
RingBuffer<char, 4, 2> buf; RingBuffer<char, 8> buf;
const char seed = 42; const char seed = 42;
const std::size_t count = 1000000; const std::size_t count = 1000000;
std::size_t full = 0; std::size_t full = 0;
@ -92,8 +92,8 @@ TEST_CASE("RingBuffer: Threaded Test", "[common]") {
std::array<char, 2> value = {seed, seed}; std::array<char, 2> value = {seed, seed};
std::size_t i = 0; std::size_t i = 0;
while (i < count) { while (i < count) {
if (const std::size_t c = buf.Push(&value[0], 1); c > 0) { if (const std::size_t c = buf.Push(&value[0], 2); c > 0) {
REQUIRE(c == 1U); REQUIRE(c == 2U);
i++; i++;
next_value(value); next_value(value);
} else { } else {
@ -107,7 +107,7 @@ TEST_CASE("RingBuffer: Threaded Test", "[common]") {
std::array<char, 2> value = {seed, seed}; std::array<char, 2> value = {seed, seed};
std::size_t i = 0; std::size_t i = 0;
while (i < count) { while (i < count) {
if (const std::vector<char> v = buf.Pop(1); v.size() > 0) { if (const std::vector<char> v = buf.Pop(2); v.size() > 0) {
REQUIRE(v.size() == 2U); REQUIRE(v.size() == 2U);
REQUIRE(v[0] == value[0]); REQUIRE(v[0] == value[0]);
REQUIRE(v[1] == value[1]); REQUIRE(v[1] == value[1]);