Repository navigation
Allow trivially destructible types use constant erase - #52
ldeng-ustc wants to merge 1 commit into
Conversation
|
One behaviour change is worth noting in the commit message. The fast path skips the allocator's |
| return iterator(this, last.m_it); | ||
| } | ||
|
|
||
| //! Remove first <code>n</code> elements (with constant complexity for scalar types). |
There was a problem hiding this comment.
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); | ||
|
|
||
|
|
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
|
I dug some more into this with Claude. Allocator
|
| 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, andpop_front(),erase()and the other single-element paths still calldestroy(). 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 thatp->~T()does nothing. It says nothing abouta.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 forstd::allocator. - Only C++11 and later. In C++03,
boost::allocator_destroynever calls a memberdestroy(); it callsp->~T()directly. So C++03 builds behave the same before and after.
Options
-
Accept it and document it (simplest).
- Add a sentence to the
\noteblocks onerase_beginanderase_end, and toclear()and the destructor: for trivially destructible types, the allocator'sdestroy()is not called. - Add a Release Notes entry for the change from
is_scalartohas_trivial_destructor.
This matches the original design for scalars.
- Add a sentence to the
-
Take the fast path only when skipping can't be observed:
has_trivial_destructor<T>, and the allocator isstd::allocatoror has nodestroymember. Otherwise use the existing slow path, which callsdestroy()at the right time.- This is correct for all allocators, at no cost to
std::allocatorusers. - It needs a "has
destroy" trait. Boost.Core only has the internalboost::detail::alloc_has_destroy. std::allocatormust be special-cased, because it has adestroymember up to C++17.- Erasing becomes linear for allocators that customise
destroy, including for scalars, which are documented as constant time today.
- This is correct for all allocators, at no cost to
Alternatives considered
-
Defer
allocator_destroyuntil later, for example the buffer destructor. This doesn't work as it stands, because erased slots get reused by laterpush_back/push_frontcalls. It would need:- tracking of which free slots still hold objects not yet passed to
destroy, which soon breaks up afterinsert,linearizeorrotate; - 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. - tracking of which free slots still hold objects not yet passed to
-
Skip
allocator_constructtoo, and keep every slot alive from the constructor to the destructor. Construct and destroy would pair up, but:Tmust be default-constructible;- the constructor, the destructor and
set_capacitybecome linear in capacity; - insertion uses assignment instead of construction, which is observable, because trivially destructible doesn't mean trivially copyable;
- skipping
constructbreaks allocators whoseconstructchanges the result:scoped_allocator_adaptorand 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 whatcircular_bufferdoes 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.
|
@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. |
Use
boost::has_trivial_destructorinstead ofboost::is_scalarto determine whether constant-time erasing is enabled.Add tests to check constant-time erasing run correctly with trivially destructible types.
Fixes #51 .