Repository navigation
Constant-time erase for trivially destructible types, with correct allocator destroy() calls - #67
Open
nigels-com wants to merge 4 commits into
Open
nigels-com wants to merge 4 commits into
nigels-com wants to merge 4 commits into
Conversation
ad251cf changed erase_begin(), erase_end() and clear() to dispatch on has_trivial_destructor<value_type> instead of is_scalar<value_type>, 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.
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<int>. 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.
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<T>, 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<Alloc>, 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<value_type> with circular_buffer_trivial_destroy<Alloc>. 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<T>. - 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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #67 +/- ##
==========================================
Coverage ? 99.44%
==========================================
Files ? 10
Lines ? 4688
Branches ? 3439
==========================================
Hits ? 4662
Misses ? 23
Partials ? 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ldeng-ustc
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(An extended version of PR #52 providing opt-in performance improvement for custom allocators)
erase_begin(n),erase_end(n),clear()and the destructor already run in constant time for scalar types, because no destructors need to run. This PR extends that to every trivially destructible type, such as plain structs. It also makes sure an allocator with its owndestroy()still sees every call, unless the allocator opts out.Commits
has_trivial_destructor<value_type>instead ofis_scalar<value_type>, and add tests for a trivially destructible class type.erase_begin,erase_end,clear()and the destructor said "scalar types". They also had two typos, "destructruction" and "non-sacalar".BOOST_CB_ENABLE_DEBUG=1. AddsCB_TEST_DEBUG_ONLYandCB_TEST_DEBUG_EXCLUDEtotest.hpp, and aconstant_erase_test_dbgrun to the Jamfile.Allocator
destroy()The constant-time path skips the allocator's
destroy()as well as the destructor. For scalars it always did. With commit 1 it also applied to trivially destructible class types, so allocators that customisedestroy()(counting, logging, leak checking,scoped_allocator_adaptor) no longer saw those calls. Theirconstruct()anddestroy()calls stopped pairing up, and the standard expects elements to be destroyed throughallocator_traits<A>::destroy.destroy()calls counted by an allocator with its owndestroy(), on a buffer of 8 elements (C++11):erase_begin(2)erase_end(1)clear()(4 left)struct Pod { int x; }developstruct Pod { int x; }struct Pod { int x; }With this PR, the fast path is taken only when skipping can't be observed. That means the element type is trivially destructible and the allocator is the default one, whose
destroy()only runs the destructor. Any other allocator gets adestroy()call for every element, and can opt in to the fast path:New public names in
circular_buffer_fwd.hpp:circular_buffer_default_allocator<T>::type: the allocator used when none is given. The default template arguments use it, andBOOST_CB_DEFAULT_ALLOCATORis now#undef'd straight after it.circular_buffer_trivial_destroy<Alloc>: true only for the default allocator. Users specialise it to opt in.Inside
base.hpp, a privatetrivial_erasetype combines both conditions. Debug mode still always takes the slow path.Behaviour changes
destroy()skippeddestroy()called (opt in to keep constant)destroy()calleddestroy()called (opt in for constant)In C++03,
boost::allocator_destroynever calls a memberdestroy(), so there a custom allocator only sees the change in complexity.Alternatives considered and rejected:
destroy()to the destructor: erased slots get reused, so this would need per-slot tracking and a check on every insert.construct()as well: this changes element requirements and complexity, and breaks uses-allocator construction.Tests
test/constant_erase_test.cpp:destroy()counts fromerase_begin,erase_end,pop_front,clear()and the destructor, using a counting allocator:intMyInteger(real destructor)circular_buffer_space_optimizedmust calldestroy()once for everyconstruct().Also:
BOOST_CB_ENABLE_DEBUG=1.base_test,space_optimized_test,soft_iterator_invalidation(debug off only) andconstant_erase_testall pass with-Wall -Wextra -Werror. ASan and UBSan are clean.Documentation
\notes onerase_begin,erase_end,clear()and the destructor now describe the allocator condition and link tocircular_buffer_trivial_destroy.circular_buffer_fwd.hppis added to the Doxygen input, andPREDEFINEDmapsBOOST_CB_DEFAULT_ALLOCATOR(T)tostd::allocator<T>. Doxygen 1.15, run on its own with the jamfile settings, gives no warnings, and the existing class docs are unchanged apart from the new links. The full BoostBook build hasn't been run.Notes for reviewers
destroy()calls (this PR) or constant time (the old behaviour for scalars). The opt-in trait keeps constant time available to anyone who needs it.circular_buffer_default_allocatorandcircular_buffer_trivial_destroyare new public API.circular_buffer_space_optimizeddoesn't makeerase_beginanderase_endpublic. It inherits privately, and they're only reachable in debug mode, where it inherits publicly. That was already the case and isn't changed here.