From ad251cf8e6229899a82833b7e9deba94e5b1bc03 Mon Sep 17 00:00:00 2001 From: Long Deng Date: Wed, 16 Oct 2024 15:05:47 +0000 Subject: [PATCH 1/4] Allow trivially destructible types use constant erase --- include/boost/circular_buffer/base.hpp | 8 +-- test/constant_erase_test.cpp | 69 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index 47441544..34c661e4 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -25,10 +25,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -2279,7 +2279,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG erase_begin(n, false_type()); #else - erase_begin(n, is_scalar()); + erase_begin(n, has_trivial_destructor()); #endif } @@ -2311,7 +2311,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG erase_end(n, false_type()); #else - erase_end(n, is_scalar()); + erase_end(n, has_trivial_destructor()); #endif } @@ -2462,7 +2462,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG destroy_content(false_type()); #else - destroy_content(is_scalar()); + destroy_content(has_trivial_destructor()); #endif } diff --git a/test/constant_erase_test.cpp b/test/constant_erase_test.cpp index e2431be2..7a9069f2 100644 --- a/test/constant_erase_test.cpp +++ b/test/constant_erase_test.cpp @@ -68,6 +68,31 @@ void erase_begin_test() { BOOST_TEST(cb3[0] == 4); BOOST_TEST(cb3[1] == 5); BOOST_TEST(cb3[2] == 6); + + + circular_buffer cb4(5); + cb4.push_back(1); + cb4.push_back(2); + cb4.push_back(3); + cb4.push_back(4); + cb4.push_back(5); + cb4.push_back(6); + + + circular_buffer::pointer p4 = &cb4[0]; + cb4.erase_begin(5); + BOOST_TEST(cb4.empty()); + BOOST_TEST(p4->m_n == 2); + BOOST_TEST((p4 + 1)->m_n == 3); + BOOST_TEST((p4 + 2)->m_n == 4); + BOOST_TEST((p4 + 3)->m_n == 5); + BOOST_TEST((p4 - 1)->m_n == 6); + + cb4.push_back(10); + cb4.push_back(11); + BOOST_TEST(cb4.size() == 2); + BOOST_TEST(cb4[0].m_n == 10); + BOOST_TEST(cb4[1].m_n == 11); } void erase_end_test() { @@ -127,6 +152,31 @@ void erase_end_test() { BOOST_TEST(cb3[0] == 2); BOOST_TEST(cb3[1] == 3); BOOST_TEST(cb3[2] == 4); + + + circular_buffer cb4(5); + cb4.push_back(1); + cb4.push_back(2); + cb4.push_back(3); + cb4.push_back(4); + cb4.push_back(5); + cb4.push_back(6); + + circular_buffer::pointer p4 = &cb4[0]; + + cb4.erase_end(5); + BOOST_TEST(cb4.empty()); + BOOST_TEST(p4->m_n == 2); + BOOST_TEST((p4 + 1)->m_n == 3); + BOOST_TEST((p4 + 2)->m_n == 4); + BOOST_TEST((p4 + 3)->m_n == 5); + BOOST_TEST((p4 - 1)->m_n == 6); + + cb4.push_back(10); + cb4.push_back(11); + BOOST_TEST(cb4.size() == 2); + BOOST_TEST(cb4[0].m_n == 10); + BOOST_TEST(cb4[1].m_n == 11); } void clear_test() { @@ -170,6 +220,25 @@ void clear_test() { cb3.clear(); BOOST_TEST(cb3.empty()); + + circular_buffer cb4(5); + cb4.push_back(1); + cb4.push_back(2); + cb4.push_back(3); + cb4.push_back(4); + cb4.push_back(5); + cb4.push_back(6); + + circular_buffer::pointer p4 = &cb4[0]; + + cb4.clear(); + + BOOST_TEST(cb4.empty()); + BOOST_TEST(p4->m_n == 2); + BOOST_TEST((p4 + 1)->m_n == 3); + BOOST_TEST((p4 + 2)->m_n == 4); + BOOST_TEST((p4 + 3)->m_n == 5); + BOOST_TEST((p4 - 1)->m_n == 6); } // test main From ea389c3692100cb6536097d80223f9a9a31e8343 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Wed, 2 Dec 2026 18:03:31 +1000 Subject: [PATCH 2/4] Update erase and clear docs for trivially destructible types ad251cf changed erase_begin(), erase_end() and clear() to dispatch on has_trivial_destructor instead of is_scalar, but their documentation still described a fast path for scalar types. - erase_begin(), erase_end(), clear() and ~circular_buffer(): say "trivially destructible types" instead of "scalar types" or "scalars". The destructor goes through the same destroy_content() fast path. - erase_begin() and erase_end() notes: give a class with a trivial destructor as an example alongside integer, float and pointer, and fix the typos "destructruction" and "non-sacalar". - CLAUDE.md: the "Scalar fast paths" note becomes "Trivially destructible fast paths", names has_trivial_destructor, and includes the destructor. Documentation only; no code changes. --- CLAUDE.md | 7 +++-- include/boost/circular_buffer/base.hpp | 38 ++++++++++++++------------ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 5e35a44f..4eb1083f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -81,9 +81,10 @@ build.jam b2 project and dependency list - **Exceptions** use `BOOST_TRY` / `BOOST_CATCH(...)` / `BOOST_CATCH_END` / `BOOST_RETHROW` and `boost::throw_exception`. The code must build with `BOOST_NO_EXCEPTIONS`. -- **Scalar fast paths**: `erase_begin(n)`, `erase_end(n)` and `clear()` take - constant time for scalar `T`, because they dispatch on `is_scalar` - and no destructors run. Preserve these complexity +- **Trivially destructible fast paths**: `erase_begin(n)`, `erase_end(n)`, + `clear()` and the destructor take constant time for trivially destructible + `T`, because they dispatch on `has_trivial_destructor` and no + destructors run. Preserve these complexity guarantees, since they are documented. ## Conventions diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index 34c661e4..eae7c655 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -1189,7 +1189,8 @@ private empty_value Invalidates all iterators pointing to the circular_buffer (including iterators equal to end()). \par Complexity - Constant (in the size of the circular_buffer) for scalar types; linear for other types. + Constant (in the size of the circular_buffer) for trivially destructible types; linear for + other types. \sa clear() */ ~circular_buffer() BOOST_NOEXCEPT { @@ -2251,7 +2252,7 @@ private empty_value return iterator(this, last.m_it); } - //! Remove first n elements (with constant complexity for scalar types). + //! Remove first n elements (with constant complexity for trivially destructible types). /*! \pre n \<= size() \post The n elements at the beginning of the circular_buffer will be removed. @@ -2259,16 +2260,16 @@ private empty_value \throws Exceptions of move_if_noexcept(T&). \par Exception Safety Basic; no-throw if the operation in the Throws section does not throw anything. (I.e. no throw in - case of scalars.) + case of trivially destructible types.) \par Iterator Invalidation Invalidates iterators pointing to the first n erased elements. \par Complexity - Constant (in n) for scalar types; linear for other types. - \note This method has been specially designed for types which do not require an explicit destructruction (e.g. - integer, float or a pointer). For these scalar types a call to a destructor is not required which makes - it possible to implement the "erase from beginning" operation with a constant complexity. For non-sacalar - types the complexity is linear (hence the explicit destruction is needed) and the implementation is - actually equivalent to + Constant (in n) for trivially destructible types; linear for other types. + \note This method has been specially designed for types which do not require an explicit destruction (e.g. + integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types + a call to a destructor is not required which makes it possible to implement the "erase from beginning" + operation with a constant complexity. For other types the complexity is linear (hence the explicit + destruction is needed) and the implementation is actually equivalent to \link circular_buffer::rerase(iterator, iterator) rerase(begin(), begin() + n)\endlink. \sa erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), @@ -2283,7 +2284,7 @@ private empty_value #endif } - //! Remove last n elements (with constant complexity for scalar types). + //! Remove last n elements (with constant complexity for trivially destructible types). /*! \pre n \<= size() \post The n elements at the end of the circular_buffer will be removed. @@ -2291,16 +2292,16 @@ private empty_value \throws Exceptions of move_if_noexcept(T&). \par Exception Safety Basic; no-throw if the operation in the Throws section does not throw anything. (I.e. no throw in - case of scalars.) + case of trivially destructible types.) \par Iterator Invalidation Invalidates iterators pointing to the last n erased elements. \par Complexity - Constant (in n) for scalar types; linear for other types. - \note This method has been specially designed for types which do not require an explicit destructruction (e.g. - integer, float or a pointer). For these scalar types a call to a destructor is not required which makes - it possible to implement the "erase from end" operation with a constant complexity. For non-sacalar - types the complexity is linear (hence the explicit destruction is needed) and the implementation is - actually equivalent to + Constant (in n) for trivially destructible types; linear for other types. + \note This method has been specially designed for types which do not require an explicit destruction (e.g. + integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types + a call to a destructor is not required which makes it possible to implement the "erase from end" + operation with a constant complexity. For other types the complexity is linear (hence the explicit + destruction is needed) and the implementation is actually equivalent to \link circular_buffer::erase(iterator, iterator) erase(end() - n, end())\endlink. \sa erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), @@ -2325,7 +2326,8 @@ private empty_value Invalidates all iterators pointing to the circular_buffer (except iterators equal to end()). \par Complexity - Constant (in the size of the circular_buffer) for scalar types; linear for other types. + Constant (in the size of the circular_buffer) for trivially destructible types; linear for + other types. \sa ~circular_buffer(), erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), erase_begin(size_type), erase_end(size_type) From 9601e92540e365c2e6d9a4ac85bd99eb483cd5a5 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Wed, 2 Dec 2026 18:10:57 +1000 Subject: [PATCH 3/4] Run constant_erase_test with debug support enabled constant_erase_test reads erased elements through saved pointers, to check that erasing trivially destructible elements leaves their memory untouched. With BOOST_CB_ENABLE_DEBUG=1, destroy_item() overwrites erased memory, so 26 of these checks failed: the 15 MyDefaultConstructible checks added in ad251cf and 11 older checks on circular_buffer. The test only ran with debug off, so the failures went unnoticed. - test.hpp: add CB_TEST_DEBUG_ONLY(expr) and CB_TEST_DEBUG_EXCLUDE(expr), which run a check only with or only without debug support. A disabled check still names its expression in an unevaluated sizeof, so variables used only by the check don't trigger unused-variable warnings. - constant_erase_test.cpp: use CB_TEST_DEBUG_EXCLUDE for the erased-memory reads. All other checks still run in both modes. - Jamfile.v2: also run the test with BOOST_CB_ENABLE_DEBUG=1, as constant_erase_test_dbg. - CLAUDE.md: describe the new macros, and move constant_erase_test into the debug on/off loop of the quick-run script. Tested with gcc and clang at C++03, C++11 and C++20, with debug on and off. --- CLAUDE.md | 13 +++++---- test/Jamfile.v2 | 1 + test/constant_erase_test.cpp | 56 +++++++++++++++++++----------------- test/test.hpp | 11 +++++++ 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 4eb1083f..a7b26d50 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -62,8 +62,11 @@ build.jam b2 project and dependency list - **Debug mode**: `BOOST_CB_ENABLE_DEBUG=1` makes iterators derive from `debug_iterator_base` and register with the container, so invalidated iterators are caught by `BOOST_CB_ASSERT`. It is off by default. Some - behaviour differs between modes: for example, `constant_erase_test` and - `soft_iterator_invalidation` only make sense with debug off. + behaviour differs between modes. For example, `soft_iterator_invalidation` + only makes sense with debug off, and debug mode overwrites erased memory, so + `constant_erase_test` skips its checks of erased memory when debug is on. + For checks like these, `test.hpp` provides `CB_TEST_DEBUG_ONLY(expr)` and + `CB_TEST_DEBUG_EXCLUDE(expr)`. - **Internal macros**: `BOOST_CB_ASSERT`, `BOOST_CB_IS_CONVERTIBLE` and `BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS` are defined in `circular_buffer.hpp` and `#undef`'d at its end. As a result, the @@ -173,15 +176,13 @@ copy): INC="-Iinclude $(for d in $BOOST/libs/*/include; do case $d in *circular_buffer*) ;; *) printf -- '-isystem %s ' $d;; esac; done)" for std in c++03 c++11; do for dbg in 0 1; do - for t in base_test space_optimized_test; do + for t in base_test space_optimized_test constant_erase_test; do g++ -std=$std -Wall -Wextra -DBOOST_CB_ENABLE_DEBUG=$dbg $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t $std dbg=$dbg" done done done # debug-off only: -for t in soft_iterator_invalidation constant_erase_test; do - g++ -std=c++11 $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t" -done +g++ -std=c++11 $INC test/soft_iterator_invalidation.cpp -o /tmp/sii && /tmp/sii || echo "FAIL soft_iterator_invalidation" ``` `bounded_buffer_comparison.cpp` and `circular_buffer_bound_example.cpp` need diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a1742c52..930e5606 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,4 +27,5 @@ run base_test.cpp : : : "BOOST_CB_ENABLE_DEBUG=1" : base_test_dbg ; run space_optimized_test.cpp : : : "BOOST_CB_ENABLE_DEBUG=1" : space_optimized_test_dbg ; run soft_iterator_invalidation.cpp ; run constant_erase_test.cpp ; +run constant_erase_test.cpp : : : "BOOST_CB_ENABLE_DEBUG=1" : constant_erase_test_dbg ; compile bounded_buffer_comparison.cpp : multi /boost/thread//boost_thread /boost/timer//boost_timer ; diff --git a/test/constant_erase_test.cpp b/test/constant_erase_test.cpp index 7a9069f2..cdfaafd6 100644 --- a/test/constant_erase_test.cpp +++ b/test/constant_erase_test.cpp @@ -11,6 +11,10 @@ int MyInteger::ms_exception_trigger = 0; int InstanceCounter::ms_count = 0; +// Erasing a trivially destructible element leaves its memory untouched, so the old value can still be read +// through a pointer. Debug mode (BOOST_CB_ENABLE_DEBUG) overwrites erased memory, so those checks use +// CB_TEST_DEBUG_EXCLUDE. + void erase_begin_test() { circular_buffer cb1(5); @@ -32,9 +36,9 @@ void erase_begin_test() { cb1.erase_begin(3); BOOST_TEST(cb1.empty()); - BOOST_TEST(*p == 2); - BOOST_TEST(*(p + 1) == 3); - BOOST_TEST(*(p + 2) == 4); + CB_TEST_DEBUG_EXCLUDE(*p == 2); + CB_TEST_DEBUG_EXCLUDE(*(p + 1) == 3); + CB_TEST_DEBUG_EXCLUDE(*(p + 2) == 4); cb1.push_back(10); cb1.push_back(11); @@ -82,11 +86,11 @@ void erase_begin_test() { circular_buffer::pointer p4 = &cb4[0]; cb4.erase_begin(5); BOOST_TEST(cb4.empty()); - BOOST_TEST(p4->m_n == 2); - BOOST_TEST((p4 + 1)->m_n == 3); - BOOST_TEST((p4 + 2)->m_n == 4); - BOOST_TEST((p4 + 3)->m_n == 5); - BOOST_TEST((p4 - 1)->m_n == 6); + CB_TEST_DEBUG_EXCLUDE(p4->m_n == 2); + CB_TEST_DEBUG_EXCLUDE((p4 + 1)->m_n == 3); + CB_TEST_DEBUG_EXCLUDE((p4 + 2)->m_n == 4); + CB_TEST_DEBUG_EXCLUDE((p4 + 3)->m_n == 5); + CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); cb4.push_back(10); cb4.push_back(11); @@ -116,9 +120,9 @@ void erase_end_test() { cb1.erase_end(3); BOOST_TEST(cb1.empty()); - BOOST_TEST(*p == 5); - BOOST_TEST(*(p - 1) == 4); - BOOST_TEST(*(p - 2) == 3); + CB_TEST_DEBUG_EXCLUDE(*p == 5); + CB_TEST_DEBUG_EXCLUDE(*(p - 1) == 4); + CB_TEST_DEBUG_EXCLUDE(*(p - 2) == 3); cb1.push_back(10); cb1.push_back(11); @@ -166,11 +170,11 @@ void erase_end_test() { cb4.erase_end(5); BOOST_TEST(cb4.empty()); - BOOST_TEST(p4->m_n == 2); - BOOST_TEST((p4 + 1)->m_n == 3); - BOOST_TEST((p4 + 2)->m_n == 4); - BOOST_TEST((p4 + 3)->m_n == 5); - BOOST_TEST((p4 - 1)->m_n == 6); + CB_TEST_DEBUG_EXCLUDE(p4->m_n == 2); + CB_TEST_DEBUG_EXCLUDE((p4 + 1)->m_n == 3); + CB_TEST_DEBUG_EXCLUDE((p4 + 2)->m_n == 4); + CB_TEST_DEBUG_EXCLUDE((p4 + 3)->m_n == 5); + CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); cb4.push_back(10); cb4.push_back(11); @@ -194,11 +198,11 @@ void clear_test() { cb1.clear(); BOOST_TEST(cb1.empty()); - BOOST_TEST(*p == 2); - BOOST_TEST(*(p + 1) == 3); - BOOST_TEST(*(p + 2) == 4); - BOOST_TEST(*(p + 3) == 5); - BOOST_TEST(*(p - 1) == 6); + CB_TEST_DEBUG_EXCLUDE(*p == 2); + CB_TEST_DEBUG_EXCLUDE(*(p + 1) == 3); + CB_TEST_DEBUG_EXCLUDE(*(p + 2) == 4); + CB_TEST_DEBUG_EXCLUDE(*(p + 3) == 5); + CB_TEST_DEBUG_EXCLUDE(*(p - 1) == 6); circular_buffer cb2(5, InstanceCounter()); @@ -234,11 +238,11 @@ void clear_test() { cb4.clear(); BOOST_TEST(cb4.empty()); - BOOST_TEST(p4->m_n == 2); - BOOST_TEST((p4 + 1)->m_n == 3); - BOOST_TEST((p4 + 2)->m_n == 4); - BOOST_TEST((p4 + 3)->m_n == 5); - BOOST_TEST((p4 - 1)->m_n == 6); + CB_TEST_DEBUG_EXCLUDE(p4->m_n == 2); + CB_TEST_DEBUG_EXCLUDE((p4 + 1)->m_n == 3); + CB_TEST_DEBUG_EXCLUDE((p4 + 2)->m_n == 4); + CB_TEST_DEBUG_EXCLUDE((p4 + 3)->m_n == 5); + CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); } // test main diff --git a/test/test.hpp b/test/test.hpp index dfdd5de0..6b8a6460 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -24,6 +24,17 @@ #include #endif +// Checks that only hold with or without debug support (BOOST_CB_ENABLE_DEBUG). When a check is disabled, its +// expression is still named in an unevaluated sizeof, so variables used only by the check don't trigger +// unused-variable warnings. +#if BOOST_CB_ENABLE_DEBUG + #define CB_TEST_DEBUG_ONLY(expr) BOOST_TEST(expr) + #define CB_TEST_DEBUG_EXCLUDE(expr) ((void)sizeof(expr)) +#else + #define CB_TEST_DEBUG_ONLY(expr) ((void)sizeof(expr)) + #define CB_TEST_DEBUG_EXCLUDE(expr) BOOST_TEST(expr) +#endif + // Integer (substitute for int) - more appropriate for testing class MyInteger { private: From bc7fcb3df9f2208c81e1d7466dad1c554fc67179 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Wed, 2 Dec 2026 19:17:03 +1000 Subject: [PATCH 4/4] Call allocator destroy() unless the allocator opts out The constant-time erase_begin(), erase_end(), clear() and destructor skip the allocator's destroy() as well as the element destructor. Since ad251cf (is_scalar -> has_trivial_destructor) this applies to trivially destructible class types too. An allocator with its own destroy() (counting, logging, leak checking, scoped) no longer saw those calls, so its construct() and destroy() calls stopped pairing up. The standard expects elements to be destroyed through allocator_traits::destroy. Take the fast path only when skipping destroy() can't be observed: the element type is trivially destructible and the allocator is the default one, whose destroy() only runs the destructor. With any other allocator, each element is destroyed through the allocator. An allocator can opt in to the fast path by specializing a trait. - circular_buffer_fwd.hpp: add circular_buffer_default_allocator, whose type member is the default allocator. The default template arguments now use it, and BOOST_CB_DEFAULT_ALLOCATOR is #undef'd right after it. Add circular_buffer_trivial_destroy, true only for the default allocator, which users specialize to opt in. - base.hpp: dispatch erase_begin(), erase_end() and destroy_content() on a private trivial_erase type, which combines has_trivial_destructor with circular_buffer_trivial_destroy. Debug mode still always takes the slow path. Update the Complexity paragraphs and notes. - constant_erase_test.cpp: check the trait for the default, custom and opted-in allocators at compile time. Count destroy() calls with a counting allocator for int, a trivially destructible struct and MyInteger, with and without opting in, in both debug modes. Check that the space-optimized buffer calls destroy() once for every construct(). The counting checks need the C++11 allocator model; in C++03 boost::allocator_destroy never calls a member destroy(). - doc/jamfile.v2: add circular_buffer_fwd.hpp to the Doxygen input, and predefine BOOST_CB_DEFAULT_ALLOCATOR(T) as std::allocator. - circular_buffer.qbk: add release notes, including that scalars with a non-default allocator now take linear time unless the allocator opts in. - CLAUDE.md: describe the new rule and the trait. Tested with gcc and clang at C++03, C++11, C++17 and C++20, with debug on and off. ASan and UBSan are clean. Breaking the dispatch condition in three ways each makes the new tests fail. Doxygen (standalone, with the jamfile settings) gives no warnings. --- CLAUDE.md | 14 ++- doc/circular_buffer.qbk | 11 +++ doc/jamfile.v2 | 4 + include/boost/circular_buffer/base.hpp | 40 ++++++--- include/boost/circular_buffer_fwd.hpp | 39 +++++++- test/constant_erase_test.cpp | 120 +++++++++++++++++++++++++ 6 files changed, 207 insertions(+), 21 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index a7b26d50..7e97e198 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -85,9 +85,15 @@ build.jam b2 project and dependency list `BOOST_RETHROW` and `boost::throw_exception`. The code must build with `BOOST_NO_EXCEPTIONS`. - **Trivially destructible fast paths**: `erase_begin(n)`, `erase_end(n)`, - `clear()` and the destructor take constant time for trivially destructible - `T`, because they dispatch on `has_trivial_destructor` and no - destructors run. Preserve these complexity + `clear()` and the destructor take constant time when `T` is trivially + destructible and the allocator doesn't need `destroy()` calls. They dispatch + on the private `trivial_erase` type: `has_trivial_destructor` + and `circular_buffer_trivial_destroy` (in `circular_buffer_fwd.hpp`). + The latter is true only for the default allocator + (`circular_buffer_default_allocator::type`), and users specialize it to + opt in for other allocators. The fast path skips the allocator's `destroy()` + as well as the destructor, so it is opt-in for any non-default allocator. + Debug mode always takes the slow path. Preserve these complexity guarantees, since they are documented. ## Conventions @@ -202,7 +208,7 @@ to `boost-root/doc/html/circular_buffer.html`. | Stage | Tool | Source | |---|---|---| | Narrative | Quickbook 1.6 | `doc/circular_buffer.qbk` | -| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) | +| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `circular_buffer_fwd.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) | | Index | AutoIndex | `doc/circular_buffer.idx` | | Rendering | BoostBook XSL (xsltproc; FOP for PDF) | the combined DocBook | diff --git a/doc/circular_buffer.qbk b/doc/circular_buffer.qbk index 65f0561a..fd4be695 100644 --- a/doc/circular_buffer.qbk +++ b/doc/circular_buffer.qbk @@ -473,6 +473,17 @@ Pull requests are welcome at [@https://github.com/boostorg/circular_buffer boost [section:release Release Notes] +[h4 Boost 1.94] + +* `erase_begin(size_type)`, `erase_end(size_type)`, `clear()` and the destructor now have constant complexity for + all trivially destructible types, not only scalars (`has_trivial_destructor` instead of `is_scalar`). +* The constant-time path skips the allocator's `destroy()` as well as the destructor, so it is now only used with + the default allocator. With any other allocator each element is destroyed through the allocator, so an allocator + with its own `destroy()` sees every call; for scalars this makes these operations linear where they were constant. + Specialize `boost::circular_buffer_trivial_destroy` to derive from `boost::true_type` to use the + constant-time path with an allocator that does not need `destroy()` calls for trivially destructible elements. +* Added `boost::circular_buffer_default_allocator::type`, the allocator used when none is specified. + [h4 Boost 1.91] * Avoided a GCC 4.x warning about calling `memset` with a constant size of zero. diff --git a/doc/jamfile.v2 b/doc/jamfile.v2 index b4dc2798..40c48f75 100644 --- a/doc/jamfile.v2 +++ b/doc/jamfile.v2 @@ -27,6 +27,7 @@ doxygen autodoc : # List all the files individually (RECURSIVE=NO ). [ glob ../include/boost/circular_buffer.hpp ] + [ glob ../include/boost/circular_buffer_fwd.hpp ] [ glob ../include/boost/circular_buffer/base.hpp ] [ glob ../include/boost/circular_buffer/space_optimized.hpp ] @@ -65,12 +66,15 @@ doxygen autodoc \"BOOST_NOEXCEPT_IF(T)=noexcept(T)\" \\ \"BOOST_ASSERT(expr)=\" \\ \"BOOST_RV_REF(T)=T &&\" \\ + \"BOOST_CB_DEFAULT_ALLOCATOR(T):=std::allocator\" \\ \"ASSERT(x)=assert(x)\" \\ \"__cplusplus \"" # BOOST_PREVENT_MACRO_SUBSTITUTION, will not be replaced by , # BOOST_STATIC_CONSTANT will be replaced by "static x const y", # BOOST_DEDUCED_TYPENAME will be replaced by "typename", # BOOST_CONSTEXPR will be replaced by "constexpr". + # BOOST_CB_DEFAULT_ALLOCATOR(T) will be replaced by "std::allocator" (":=" so the #define and #undef in + # circular_buffer_fwd.hpp do not override it). EXCLUDE_SYMBOLS=*_throws # IMAGE_PATH="../images" # for circular_buffer.svg # See autodoxywarnings.log to check this is correct. diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index eae7c655..bd249593 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -19,6 +19,7 @@ #endif #include +#include #include #include #include @@ -29,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -1189,8 +1191,8 @@ private empty_value Invalidates all iterators pointing to the circular_buffer (including iterators equal to end()). \par Complexity - Constant (in the size of the circular_buffer) for trivially destructible types; linear for - other types. + Constant (in the size of the circular_buffer) for trivially destructible types with the + default allocator (see circular_buffer_trivial_destroy); linear otherwise. \sa clear() */ ~circular_buffer() BOOST_NOEXCEPT { @@ -2264,12 +2266,15 @@ private empty_value \par Iterator Invalidation Invalidates iterators pointing to the first n erased elements. \par Complexity - Constant (in n) for trivially destructible types; linear for other types. + Constant (in n) for trivially destructible types with the default allocator (see + circular_buffer_trivial_destroy); linear otherwise. \note This method has been specially designed for types which do not require an explicit destruction (e.g. integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types a call to a destructor is not required which makes it possible to implement the "erase from beginning" - operation with a constant complexity. For other types the complexity is linear (hence the explicit - destruction is needed) and the implementation is actually equivalent to + operation with a constant complexity, without calling destroy() on the allocator. This + applies to the default allocator, and to any other allocator for which + circular_buffer_trivial_destroy is specialized. In all other cases the complexity is linear + (hence the explicit destruction is needed) and the implementation is actually equivalent to \link circular_buffer::rerase(iterator, iterator) rerase(begin(), begin() + n)\endlink. \sa erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), @@ -2280,7 +2285,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG erase_begin(n, false_type()); #else - erase_begin(n, has_trivial_destructor()); + erase_begin(n, trivial_erase()); #endif } @@ -2296,12 +2301,15 @@ private empty_value \par Iterator Invalidation Invalidates iterators pointing to the last n erased elements. \par Complexity - Constant (in n) for trivially destructible types; linear for other types. + Constant (in n) for trivially destructible types with the default allocator (see + circular_buffer_trivial_destroy); linear otherwise. \note This method has been specially designed for types which do not require an explicit destruction (e.g. integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types a call to a destructor is not required which makes it possible to implement the "erase from end" - operation with a constant complexity. For other types the complexity is linear (hence the explicit - destruction is needed) and the implementation is actually equivalent to + operation with a constant complexity, without calling destroy() on the allocator. This + applies to the default allocator, and to any other allocator for which + circular_buffer_trivial_destroy is specialized. In all other cases the complexity is linear + (hence the explicit destruction is needed) and the implementation is actually equivalent to \link circular_buffer::erase(iterator, iterator) erase(end() - n, end())\endlink. \sa erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), @@ -2312,7 +2320,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG erase_end(n, false_type()); #else - erase_end(n, has_trivial_destructor()); + erase_end(n, trivial_erase()); #endif } @@ -2326,8 +2334,8 @@ private empty_value Invalidates all iterators pointing to the circular_buffer (except iterators equal to end()). \par Complexity - Constant (in the size of the circular_buffer) for trivially destructible types; linear for - other types. + Constant (in the size of the circular_buffer) for trivially destructible types with the + default allocator (see circular_buffer_trivial_destroy); linear otherwise. \sa ~circular_buffer(), erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), erase_begin(size_type), erase_end(size_type) @@ -2340,6 +2348,12 @@ private empty_value private: // Helper methods + /*! INTERNAL ONLY */ + // Whether erasing can skip both the destructor and the allocator's destroy() (see + // circular_buffer_trivial_destroy). Selects the constant-time erase_begin(), erase_end() and destroy_content(). + typedef integral_constant::value && circular_buffer_trivial_destroy::value> trivial_erase; + /*! INTERNAL ONLY */ void check_position(size_type index) const { if (index >= size()) @@ -2464,7 +2478,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG destroy_content(false_type()); #else - destroy_content(has_trivial_destructor()); + destroy_content(trivial_erase()); #endif } diff --git a/include/boost/circular_buffer_fwd.hpp b/include/boost/circular_buffer_fwd.hpp index 621fb953..a0724efa 100644 --- a/include/boost/circular_buffer_fwd.hpp +++ b/include/boost/circular_buffer_fwd.hpp @@ -16,6 +16,8 @@ #endif #include +#include +#include #if !defined(BOOST_NO_STD_ALLOCATOR) #include #else @@ -30,14 +32,43 @@ namespace boost { #define BOOST_CB_DEFAULT_ALLOCATOR(T) BOOST_DEDUCED_TYPENAME std::vector::allocator_type #endif -template +//! The default allocator of circular_buffer and circular_buffer_space_optimized. +template +struct circular_buffer_default_allocator { + //! The allocator used for elements of type T when none is specified. + typedef BOOST_CB_DEFAULT_ALLOCATOR(T) type; +}; + +#undef BOOST_CB_DEFAULT_ALLOCATOR + +//! Whether elements with a trivial destructor can be erased without calling the allocator's destroy(). +/*! + When this is true and the stored type has a trivial destructor, erase_begin(), + erase_end(), clear() and the destructor of circular_buffer run in constant + time and do not call destroy() on the allocator for the erased elements. Otherwise each element is + destroyed through the allocator and these operations are linear. + + It is true only for the default allocator, whose destroy() does nothing more than call + the destructor. Specialize it to derive from boost::true_type for an allocator that does not need + destroy() calls for elements with a trivial destructor: + \code + namespace boost { + template + struct circular_buffer_trivial_destroy > : true_type {}; + } + \endcode +*/ +template +struct circular_buffer_trivial_destroy + : is_same::type> {}; + +template ::type> class circular_buffer; -template +template ::type> class circular_buffer_space_optimized; -#undef BOOST_CB_DEFAULT_ALLOCATOR - } // namespace boost #endif // #if !defined(BOOST_CIRCULAR_BUFFER_FWD_HPP) diff --git a/test/constant_erase_test.cpp b/test/constant_erase_test.cpp index cdfaafd6..b1a78acf 100644 --- a/test/constant_erase_test.cpp +++ b/test/constant_erase_test.cpp @@ -7,6 +7,10 @@ // http://www.boost.org/LICENSE_1_0.txt) #include "test.hpp" +#include +#include +#include +#include int MyInteger::ms_exception_trigger = 0; int InstanceCounter::ms_count = 0; @@ -245,11 +249,127 @@ void clear_test() { CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); } +// An allocator with its own construct() and destroy(), which counts the calls. +int g_constructs = 0; +int g_destroys = 0; + +template +class counting_allocator : public std::allocator { +public: + typedef T value_type; + template struct rebind { typedef counting_allocator other; }; + counting_allocator() {} + template counting_allocator(const counting_allocator&) {} +#if !defined(BOOST_NO_CXX11_ALLOCATOR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ + !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + template + void construct(U* p, Args&&... args) { + ++g_constructs; + ::new (static_cast(p)) U(std::forward(args)...); + } + template + void destroy(U* p) { + ++g_destroys; + p->~U(); + } +#endif +}; + +// The same allocator, opted in to skipping destroy() for trivially destructible elements. +template +class opt_in_allocator : public counting_allocator { +public: + template struct rebind { typedef opt_in_allocator other; }; + opt_in_allocator() {} + template opt_in_allocator(const opt_in_allocator&) {} +}; + +namespace boost { +template +struct circular_buffer_trivial_destroy > : true_type {}; +} // namespace boost + +// Only the default allocator, or an allocator that opts in, may skip destroy(). +BOOST_STATIC_ASSERT(circular_buffer_trivial_destroy >::value); +BOOST_STATIC_ASSERT(circular_buffer_trivial_destroy::type>::value); +BOOST_STATIC_ASSERT(!circular_buffer_trivial_destroy >::value); +BOOST_STATIC_ASSERT(circular_buffer_trivial_destroy >::value); + +// Count the allocator's destroy() calls for erase_begin(), erase_end(), clear() and the destructor. When +// "skipped" is true they take the constant-time path, which does not call destroy(), except in debug mode, which +// always destroys each element. +template +void check_allocator_destroy(bool skipped) { + const bool fast = skipped && !BOOST_CB_ENABLE_DEBUG; + g_constructs = g_destroys = 0; + { + circular_buffer cb(8); + for (int i = 0; i < 8; ++i) + cb.push_back(T(i)); + BOOST_TEST_EQ(g_constructs, 8); + + int before = g_destroys; + cb.erase_begin(2); + BOOST_TEST_EQ(g_destroys - before, fast ? 0 : 2); + + before = g_destroys; + cb.erase_end(1); + BOOST_TEST_EQ(g_destroys - before, fast ? 0 : 1); + + before = g_destroys; + cb.pop_front(); // Always destroys the element. + BOOST_TEST_EQ(g_destroys - before, 1); + + before = g_destroys; + cb.clear(); + BOOST_TEST_EQ(g_destroys - before, fast ? 0 : 4); + + for (int i = 0; i < 3; ++i) + cb.push_back(T(i)); + } + BOOST_TEST_EQ(g_destroys, fast ? 1 : g_constructs); // Destructor: 3 more, unless skipped. +} + +// Every element constructed through the allocator is destroyed through it. +template +void check_space_optimized_allocator_destroy() { + g_constructs = g_destroys = 0; + { + circular_buffer_space_optimized cb(8); + for (int i = 0; i < 12; ++i) + cb.push_back(T(i)); + cb.erase(cb.begin(), cb.begin() + 2); + cb.erase(cb.end() - 1, cb.end()); + cb.clear(); + for (int i = 0; i < 3; ++i) + cb.push_back(T(i)); + } + BOOST_TEST(g_constructs > 0); + BOOST_TEST_EQ(g_constructs, g_destroys); +} + +void allocator_destroy_test() { +#if !defined(BOOST_NO_CXX11_ALLOCATOR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ + !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + // A custom allocator sees every destroy, even for trivially destructible types. + check_allocator_destroy >(false); + check_allocator_destroy >(false); + check_space_optimized_allocator_destroy >(); + check_space_optimized_allocator_destroy >(); + + // An opted-in allocator skips destroy() for trivially destructible types only. + check_allocator_destroy >(true); + check_allocator_destroy >(true); + check_allocator_destroy >(false); +#endif +} + // test main int main() { erase_begin_test(); erase_end_test(); clear_test(); + allocator_destroy_test(); return boost::report_errors(); }