Skip to content

Constant-time erase for trivially destructible types, with correct allocator destroy() calls - #67

Open
nigels-com wants to merge 4 commits into
boostorg:developfrom
nigels-com:trivially-destructible-constant-erase
Open

nigels-com wants to merge 4 commits into
boostorg:developfrom
nigels-com:trivially-destructible-constant-erase

Conversation

@nigels-com

@nigels-com nigels-com commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

(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 own destroy() still sees every call, unless the allocator opts out.

Commits

  1. ad251cf: Allow trivially destructible types use constant erase. Dispatch on has_trivial_destructor<value_type> instead of is_scalar<value_type>, and add tests for a trivially destructible class type.
  2. ea389c3: Update erase and clear docs for trivially destructible types. The Doxygen blocks for erase_begin, erase_end, clear() and the destructor said "scalar types". They also had two typos, "destructruction" and "non-sacalar".
  3. 9601e92: Run constant_erase_test with debug support enabled. The test reads erased elements through saved pointers, but debug mode overwrites erased memory, so 26 checks failed with BOOST_CB_ENABLE_DEBUG=1. Adds CB_TEST_DEBUG_ONLY and CB_TEST_DEBUG_EXCLUDE to test.hpp, and a constant_erase_test_dbg run to the Jamfile.
  4. bc7fcb3: Call allocator destroy() unless the allocator opts out. See below.

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 customise destroy() (counting, logging, leak checking, scoped_allocator_adaptor) no longer saw those calls. Their construct() and destroy() calls stopped pairing up, and the standard expects elements to be destroyed through allocator_traits<A>::destroy.

destroy() calls counted by an allocator with its own destroy(), on a buffer of 8 elements (C++11):

Element type Code erase_begin(2) erase_end(1) clear() (4 left) destructor (3 left)
struct Pod { int x; } develop 2 1 4 3
struct Pod { int x; } commit 1 only 0 0 0 0
struct Pod { int x; } this PR 2 1 4 3

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 a destroy() call for every element, and can opt in to the fast path:

namespace boost {
template <class T>
struct circular_buffer_trivial_destroy<MyAllocator<T> > : true_type {};
}

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, and BOOST_CB_DEFAULT_ALLOCATOR is 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 private trivial_erase type combines both conditions. Debug mode still always takes the slow path.

Behaviour changes

Element type Allocator Before After
Scalar default constant constant
Trivially destructible class default linear constant
Scalar custom constant, destroy() skipped linear, destroy() called (opt in to keep constant)
Trivially destructible class custom linear, destroy() called linear, destroy() called (opt in for constant)
Non-trivial destructor any linear linear

In C++03, boost::allocator_destroy never calls a member destroy(), so there a custom allocator only sees the change in complexity.

Alternatives considered and rejected:

  • Deferring destroy() to the destructor: erased slots get reused, so this would need per-slot tracking and a check on every insert.
  • Skipping construct() as well: this changes element requirements and complexity, and breaks uses-allocator construction.
  • A third template parameter: this would break hand-written forward declarations and template template uses.

Tests

test/constant_erase_test.cpp:

  • compile-time checks of the trait for the default allocator, a custom allocator and an opted-in allocator;
  • destroy() counts from erase_begin, erase_end, pop_front, clear() and the destructor, using a counting allocator:
Element type Custom allocator Opted-in allocator
int every element skipped (every element in debug mode)
trivially destructible struct every element skipped (every element in debug mode)
MyInteger (real destructor) not tested every element
  • circular_buffer_space_optimized must call destroy() once for every construct().

Also:

  • The erased-memory checks are excluded in debug mode, and the test now also runs with BOOST_CB_ENABLE_DEBUG=1.
  • Results: gcc and clang, C++03, C++11, C++17 and C++20, debug on and off: base_test, space_optimized_test, soft_iterator_invalidation (debug off only) and constant_erase_test all pass with -Wall -Wextra -Werror. ASan and UBSan are clean.
  • Do the tests catch regressions? Breaking the dispatch condition three ways (ignoring the allocator, ignoring the opt-in, ignoring the destructor) fails 10, 8 and 9 checks respectively.

Documentation

  • The Doxygen Complexity paragraphs and \notes on erase_begin, erase_end, clear() and the destructor now describe the allocator condition and link to circular_buffer_trivial_destroy.
  • circular_buffer_fwd.hpp is added to the Doxygen input, and PREDEFINED maps BOOST_CB_DEFAULT_ALLOCATOR(T) to std::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.
  • Release notes are added under Boost 1.94.
  • CLAUDE.md describes the new rule, the trait and the test macros.

Notes for reviewers

  • The main decision is the default for custom allocators: correct 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_allocator and circular_buffer_trivial_destroy are new public API.
  • circular_buffer_space_optimized doesn't make erase_begin and erase_end public. 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.

ldeng-ustc and others added 4 commits December 2, 2026 18:00
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

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@cf22540). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants