You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Replace the 128-bit reserved_ atomic (std::atomic<reserved_info>, a lock cmpxchg16b on every op) with two cache-line-aligned 64-bit counters (reserved_, consumed_), each touched once per operation.
Eliminate producer/consumer false sharing: the consumer hot path never reads the producer counter.
Single-slot ring publication: sequence-to-slot mapping is sequence & (size-1); removed the unused multi-slot size bookkeeping and the CAS-based reservation round-trip on free().
Read-then-claim consumer: the payload is loaded before the single CAS that claims the slot. While consumed_ names a slot no producer can reach the sequence that reuses it, so the slot is stable and no re-validation is needed.
Constructor and reset() now initialize the ring with direct writes (no atomic reserve/publish round-trips) and are exception-safe via unique_ptr.
Make the destructor non-virtual so pool usage can be fully inlined.
Added
try_allocate() - pooled allocation without heap fallback (returns nullptr when empty).
try_free() - return a pooled object without deleting non-pool objects.
is_pool_member() - address-range ownership check.
object_pool_cache<T> - per-thread freelist that amortizes shared-atomic costs for hot producers/consumers.
object_pool_cache<T>::default_capacity(pool) - the capacity a default-constructed cache would use, queryable without constructing one.
available() - approximate number of objects currently in the pool. Unsynchronized, so an estimate rather than a snapshot; for monitoring and tests, not for deciding whether an allocate() will succeed.
heap_fallback_count() - cumulative count of allocate() calls that fell through to new T(). The production signal for an undersized pool or for over-large per-thread caches. Only written on the (already slow) fallback path, so the fast path is unaffected.
value_type member alias on ObjectPool<T>.
Fixed
Consumers no longer lose pooled objects under multi-threaded load. The payload was read after the claiming CAS, which let a producer reusing the same ring slot overwrite it in between; the object published there was then dropped and never circulated again. Under contention a 16-object pool collapsed to a single usable object, silently degrading allocate() to a heap allocator. The same window could hand one object to two callers when the producer's payload store landed but its sequence store had not.
Pooled objects are value-initialized (new T[n]()), matching the new T() used by the heap fallback. Previously a pooled POD started out indeterminate while a heap-fallback one came back zeroed.
object_pool_cache<T>::allocate() is no longer noexcept: it can fall through to the pool's heap fallback, so a std::bad_alloc would have called std::terminate.
Removed a reset-detection branch in try_allocate() whose condition was unreachable and which, had it fired, would have rewound consumed_ underneath concurrent consumers. reset() remains documented as not thread-safe.
Changed
The header is now <slick/object_pool.hpp>, matching the .hpp convention for C++ headers. <slick/object_pool.h> still works: it is a shim that emits a deprecation notice and includes the .hpp, so existing code compiles unchanged. Define SLICK_OBJECT_POOL_NO_DEPRECATION_WARNING to silence the notice. The shim will be removed in a future release.
object_pool_cache<T>'s default capacity is now derived from the pool (pool.size() / 16, clamped to [1, 64]) instead of a fixed 64. Capacity only makes sense relative to the pool: with a 64-object pool and 4 default caches, the old fixed default let the caches park 256 objects, draining the pool completely - 75% of allocations fell through to the heap, with no visible signal. The derived default brings that to 0.2%.
An explicit cache capacity is now clamped to [1, pool.size()]. A cache larger than the whole pool can only starve other threads. This is a sanity bound, not a safe setting - see the sizing rule on the class.
object_pool_cache<T>::free() now returns only the coldest half of the local list to the pool when full, instead of the whole list, so a thread whose burst size sits at capacity no longer round-trips everything to the shared pool each cycle (measured ~20% better below the capacity floor, parity above it). flush() still drains completely.
Non-power-of-2 pool sizes now throw std::runtime_error instead of asserting in debug builds.
Removed
Unused private helpers reserve(), publish(), and operator[]. Nothing called them; the ring operations are inline in try_free()/try_allocate().
Tests
Expanded the suite from 14 to 30 tests, covering the new API surface, constructor validation, reset semantics, and per-thread cache behavior.
Added RawPoolMultiThreadedLosesNoObjects, which hammers the pool directly (no per-thread cache, which would mask the race) and then asserts every object is still in circulation exactly once.
Added coverage for the derived default capacity, the capacity clamp, half-flush semantics, available(), heap_fallback_count(), and DefaultCachesDoNotStarveASmallPool - the regression test for the fixed-default starvation above.