Skip to content

Allow trivially destructible types use constant erase - #52

Open
ldeng-ustc wants to merge 1 commit into
boostorg:developfrom
ldeng-ustc:develop
Open

ldeng-ustc wants to merge 1 commit into
boostorg:developfrom
ldeng-ustc:develop

Conversation

@ldeng-ustc

Copy link
Copy Markdown

Use boost::has_trivial_destructor instead of boost::is_scalar to determine whether constant-time erasing is enabled.
Add tests to check constant-time erasing run correctly with trivially destructible types.
Fixes #51 .

@nigels-com

Copy link
Copy Markdown
Collaborator

One behaviour change is worth noting in the commit message. The fast path skips the allocator's destroy call as well as the destructor. That was already true for scalars, but now it covers class types too. An allocator with its own destroy (for example one that counts or logs) will no longer see calls for trivially destructible class types. That's probably acceptable, but it's a visible change.

return iterator(this, last.m_it);
}

//! Remove first <code>n</code> elements (with constant complexity for scalar types).

@nigels-com nigels-com Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments need updating.

The comments on erase_begin and erase_end (around base.hpp:2254–2275 and 2287–2307) still say "constant complexity for scalar types", "for these scalar types…" and "no throw in case of scalars". They should say "trivially destructible types". It would be a good moment to fix the existing typos there too ("destructruction", "non-sacalar"). The clear() comment probably needs the same update.

cb4.push_back(5);
cb4.push_back(6);


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank lines in the tests: the cb4 blocks in erase_begin_test and erase_end_test are preceded by two blank lines, and the erase_begin_test block also has two blank lines after its last push_back. The rest of the file uses single blank lines. The blank line before p4 = &cb4[0] also differs between the erase_begin and erase_end blocks.

circular_buffer<MyDefaultConstructible>::pointer p4 = &cb4[0];
cb4.erase_begin(5);
BOOST_TEST(cb4.empty());
BOOST_TEST(p4->m_n == 2);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests only work in non-debug builds. Reading through p4 after erasing only works without BOOST_CB_ENABLE_DEBUG, because the debug build overwrites erased memory.

@nigels-com

Copy link
Copy Markdown
Collaborator

I dug some more into this with Claude.


Allocator destroy() is skipped for trivially destructible class types

The fast path in this PR skips boost::allocator_destroy as well as the element destructor. Scalars have always worked this way. With has_trivial_destructor, the shortcut now also applies to trivially destructible class types, so an allocator with its own destroy() (for example one that counts or logs) no longer sees those calls.

I think this is probably acceptable, but it's a user-visible change. It should be documented, or the fast path should be limited to allocators where it can't be observed.

What changes

erase_begin, erase_end, clear() and the destructor dispatch on has_trivial_destructor<value_type>. On the true_type path they only move m_first, m_last and m_size, so the allocator is never told about those elements (base.hpp: erase_begin(n, true_type), erase_end(n, true_type), destroy_content(true_type)).

Calls to destroy() made by an allocator that counts them, on a buffer of 8 elements (C++11):

Element type Code erase_begin(2) erase_end(1) pop_front() clear() (4 left) destructor (3 left)
int before 0 0 1 0 0
int after 0 0 1 0 0
struct Pod { int x; } before 2 1 1 4 3
struct Pod { int x; } after 0 0 1 0 0
Reproducer
#include <boost/circular_buffer.hpp>
#include <cstdio>
#include <memory>

static int destroys = 0;

template <class T> struct counting_alloc : std::allocator<T> {
    typedef T value_type;
    template <class U> struct rebind { typedef counting_alloc<U> other; };
    counting_alloc() {}
    template <class U> counting_alloc(const counting_alloc<U>&) {}
    template <class U> void destroy(U* p) { ++destroys; p->~U(); }
};

struct Pod { int x; Pod(int v = 0) : x(v) {} };

int main() {
    boost::circular_buffer<Pod, counting_alloc<Pod> > cb(8);
    for (int i = 0; i < 8; ++i) cb.push_back(Pod(i));
    cb.erase_begin(2);
    std::printf("destroy() calls after erase_begin(2): %d\n", destroys); // 2 before, 0 after
}

Why it matters

  • Construct and destroy calls no longer pair up. Every element is still built through boost::allocator_construct, and pop_front(), erase() and the other single-element paths still call destroy(). The bulk paths don't. An allocator that matches constructs against destroys, such as a leak checker or something that tracks live objects, will see elements that are never destroyed.
  • The standard expects every element to be destroyed through the allocator. Allocator-aware containers destroy elements through allocator_traits<A>::destroy. has_trivial_destructor<T> only says that p->~T() does nothing. It says nothing about a.destroy(p). Standard libraries take this shortcut only when it can't be seen; libstdc++'s bulk _Destroy, for example, only skips per-element destroys for std::allocator.
  • Only C++11 and later. In C++03, boost::allocator_destroy never calls a member destroy(); it calls p->~T() directly. So C++03 builds behave the same before and after.

Options

  1. Accept it and document it (simplest).

    • Add a sentence to the \note blocks on erase_begin and erase_end, and to clear() and the destructor: for trivially destructible types, the allocator's destroy() is not called.
    • Add a Release Notes entry for the change from is_scalar to has_trivial_destructor.

    This matches the original design for scalars.

  2. Take the fast path only when skipping can't be observed: has_trivial_destructor<T>, and the allocator is std::allocator or has no destroy member. Otherwise use the existing slow path, which calls destroy() at the right time.

    • This is correct for all allocators, at no cost to std::allocator users.
    • It needs a "has destroy" trait. Boost.Core only has the internal boost::detail::alloc_has_destroy.
    • std::allocator must be special-cased, because it has a destroy member up to C++17.
    • Erasing becomes linear for allocators that customise destroy, including for scalars, which are documented as constant time today.

Alternatives considered

  • Defer allocator_destroy until later, for example the buffer destructor. This doesn't work as it stands, because erased slots get reused by later push_back/push_front calls. It would need:

    • tracking of which free slots still hold objects not yet passed to destroy, which soon breaks up after insert, linearize or rotate;
    • a check before every construct into a free slot;
    • linear-time clear() and destructor.

    The total work would be the same as the slow path, and the allocator would still see destroy() calls at misleading times.

  • Skip allocator_construct too, and keep every slot alive from the constructor to the destructor. Construct and destroy would pair up, but:

    • T must be default-constructible;
    • the constructor, the destructor and set_capacity become linear in capacity;
    • insertion uses assignment instead of construction, which is observable, because trivially destructible doesn't mean trivially copyable;
    • skipping construct breaks allocators whose construct changes the result: scoped_allocator_adaptor and other uses-allocator construction, and default-init allocators.
  • A variant with no allocator (a ring over user-supplied storage, or static_circular_buffer<T, N>). With no allocator, the fast path is always correct. That may be worth doing as a feature in its own right, but it doesn't change what circular_buffer does with a custom allocator.

Suggestion

Merge with option 1 (documentation and a release note). Track option 2 as a follow-up if anyone needs exact allocator destroy() calls. The docs were updated to say "trivially destructible types" in ea389c3, so the allocator caveat could go in the same \note blocks.

@nigels-com

Copy link
Copy Markdown
Collaborator

@ldeng-ustc For reasons mentioned here I've filed an alternative PR #67 which aims for more correctness in terms of allocator construct/destruct. Welcoming your opinion or feedback about that.

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.

Relaxing the Requirements for the Constant Time Versions of erase_begin and erase_end

2 participants