From 94e93c3822aa7aae22eca3345f8fa40f8bfe098d Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Mon, 16 Nov 2026 23:13:24 -0500 Subject: [PATCH] Omit buffer padding for elements aligned to the false-sharing range --- include/rigtorp/SPSCQueue.h | 8 +++-- src/SPSCQueueTest.cpp | 70 +++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/include/rigtorp/SPSCQueue.h b/include/rigtorp/SPSCQueue.h index 118beb1..0d1e519 100644 --- a/include/rigtorp/SPSCQueue.h +++ b/include/rigtorp/SPSCQueue.h @@ -302,8 +302,12 @@ class SPSCQueue { static constexpr size_t kFalseSharingRange = 64; #endif - // Padding to avoid false sharing between slots_ and adjacent allocations - static constexpr size_t kPadding = (kFalseSharingRange - 1) / sizeof(T) + 1; + // Aligned elements already put both allocation boundaries on separate + // false-sharing ranges. Otherwise pad both ends against adjacent allocations. + static constexpr size_t kPadding = + alignof(T) >= kFalseSharingRange + ? 0 + : (kFalseSharingRange - 1) / sizeof(T) + 1; private: size_t capacity_; diff --git a/src/SPSCQueueTest.cpp b/src/SPSCQueueTest.cpp index b9e09c0..9d861cc 100644 --- a/src/SPSCQueueTest.cpp +++ b/src/SPSCQueueTest.cpp @@ -85,7 +85,7 @@ template struct TrackingAllocator { T *allocate(size_t n) { ++state->allocations; state->lastCount = n; - state->lastPtr = static_cast(::operator new(n * sizeof(T))); + state->lastPtr = std::allocator{}.allocate(n); return state->lastPtr; } @@ -93,7 +93,7 @@ template struct TrackingAllocator { CHECK(p == state->lastPtr); CHECK(n == state->lastCount); ++state->deallocations; - ::operator delete(p); + std::allocator{}.deallocate(p, n); } }; @@ -492,9 +492,11 @@ template struct RecordingAtLeastAllocator : RecordingAllocator { } }; -template static void testCapacityAllocationBounds() { - // For char, each end needs one false-sharing range of padding. - const size_t padding = alignof(rigtorp::SPSCQueue); +// By default test char, which needs one false-sharing range at each end. +template +static void testCapacityAllocationBounds( + size_t padding = alignof(rigtorp::SPSCQueue)) { + using T = typename Allocator::value_type; const size_t maxCapacity = SIZE_MAX - 2 * padding - 1; size_t requested = 0; Allocator allocator; @@ -505,7 +507,7 @@ template static void testCapacityAllocationBounds() { requested = 0; bool threw = false; try { - rigtorp::SPSCQueue q(capacity, allocator); + rigtorp::SPSCQueue q(capacity, allocator); } catch (const std::bad_alloc &) { threw = true; } @@ -518,6 +520,61 @@ template static void testCapacityAllocationBounds() { } } +static void testAlignedElementPadding() { + constexpr size_t range = alignof(rigtorp::SPSCQueue); + struct alignas(range) Aligned { + explicit Aligned(int v = 0) : value(v) {} + int value; + }; + struct alignas(2 * range) OverAligned { + explicit OverAligned(int v = 0) : value(v) {} + int value; + }; + struct Unaligned { + explicit Unaligned(int v = 0) : value(v) {} + int value; + char bytes[range - sizeof(int)]{}; + }; + static_assert(sizeof(Unaligned) == range); + static_assert(alignof(Unaligned) < range); + + auto check = [](size_t padding) { + testCapacityAllocationBounds>(padding); + testCapacityAllocationBounds>(padding); + for (size_t capacity : {size_t{0}, size_t{1}, size_t{3}}) { + typename TrackingAllocator::State state; + { + rigtorp::SPSCQueue> q( + capacity, TrackingAllocator(&state)); + const size_t actual = capacity == 0 ? 1 : capacity; + CHECK(q.capacity() == actual); + CHECK(state.lastCount == actual + 1 + 2 * padding); + for (int cycle = 0; cycle < 3; ++cycle) { + for (size_t i = 0; i < actual; ++i) { + CHECK(q.try_emplace(static_cast(i))); + } + CHECK(!q.try_emplace()); + CHECK(q.front() != nullptr); + CHECK(reinterpret_cast(q.front()) % alignof(T) == 0); + for (size_t i = 0; i < actual; ++i) { + CHECK(q.front() != nullptr); + CHECK(q.front()->value == static_cast(i)); + q.pop(); + } + CHECK(q.front() == nullptr); + } + q.emplace(); // Exercise destruction of a non-empty queue. + } + CHECK(state.allocations == 1); + CHECK(state.deallocations == 1); + } + }; + check.template operator()(0); + check.template operator()(0); + // Size alone is insufficient: this type still needs padding at both ends. + check.template operator()(1); +} + static void testCapacityBounds() { using namespace rigtorp; @@ -782,6 +839,7 @@ int main(int argc, char *argv[]) { testExceptionSafety(); testCustomAllocator(); testCapacityBounds(); + testAlignedElementPadding(); testCapacityAllocationBounds>(); testCapacityAllocationBounds>(); testDeathPopEmpty();