Boost logo

Boost :

Subject: [boost] [Circular Buffer]Could erase_begin run in constant time for trivially destructible objects?
From: Pradhan Kumar (pradhan.ptr_at_[hidden])
Date: 2015-06-29 21:18:13


Hello everyone,

The implementation of circular_buffer<T>::erase_begin(n) is tag-dispatched
on is_scalar<T>, to use a constant time implementation for scalar types.
For non-scalar types, it falls back to using rerase(begin(), begin()+n).
Could we change this to dispatch on std::is_trivially_destructible?

Examining the generated code, I see that the below loop has not been
completed optimized out for a POD-type.
(I am using Boost 1.58 and gcc4.9.2. The compile option was g++ -O3
-std=c++11 -g -DBOOST_CB_DISABLE_DEBUG -fno-inline to make it easier for me
to understand the assembly.)

       do {
            destroy_item(m_first);
            increment(m_first);
            --m_size;
        } while(m_first != p);

destroy_item is optimized out, as expected. However, increment has a
"modulo" check and this prevents the above while loop from getting
optimized to one instruction.

    template <class Pointer>
    void increment(Pointer& p) const {
        if (++p == m_end)
            p = m_buff;
    }

Thoughts?

Thanks,
Pradhan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk