Direct implementation of segmented_fill[_n], calling segmented_generate[_n] can be suboptimal in some compilers.

This commit is contained in:
Ion Gaztañaga
2026-04-24 13:19:57 +02:00
parent 1586338ce0
commit f2b048a685
2 changed files with 230 additions and 4 deletions
@@ -21,19 +21,113 @@
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/experimental/segmented_iterator_traits.hpp>
#include <boost/container/experimental/segmented_generate.hpp>
#include <boost/container/detail/iterator.hpp>
#include <boost/container/experimental/segmented_fill_n.hpp>
namespace boost {
namespace container {
template <class FwdIt, class Sent, class T>
void segmented_fill(FwdIt first, Sent last, const T& value);
namespace detail_algo {
#if defined(BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING)
template <class RAIter, class T>
void segmented_fill_range
(RAIter first, RAIter last, const T& value, const non_segmented_iterator_tag &, const std::random_access_iterator_tag &)
{
typedef typename iterator_traits<RAIter>::difference_type difference_type;
difference_type n = last - first;
while(n >= difference_type(4)) {
*first = value; ++first;
*first = value; ++first;
*first = value; ++first;
*first = value; ++first;
n -= 4;
}
switch(n) {
case 3:
*first = value; ++first;
BOOST_FALLTHROUGH;
case 2:
*first = value; ++first;
BOOST_FALLTHROUGH;
case 1:
*first = value;
BOOST_FALLTHROUGH;
default:
break;
}
}
#endif //BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING
template <class FwdIt, class Sent, class T, class Tag, class Cat>
BOOST_CONTAINER_FORCEINLINE typename algo_enable_if_c<
!Tag::value || is_sentinel<Sent, FwdIt>::value>::type
segmented_fill_range(FwdIt first, Sent last, const T& value, Tag, Cat)
{
for(; first != last; ++first)
*first = value;
}
template <class SegIter, class T, class Cat>
void segmented_fill_range
(SegIter first, SegIter last, const T& value, segmented_iterator_tag, Cat)
{
typedef segmented_iterator_traits<SegIter> traits;
typedef typename traits::local_iterator local_iterator;
typedef typename traits::segment_iterator segment_iterator;
typedef typename segmented_iterator_traits<local_iterator>::is_segmented_iterator is_local_seg_t;
typedef typename iterator_traits<local_iterator>::iterator_category local_cat_t;
segment_iterator sfirst = traits::segment(first);
segment_iterator slast = traits::segment(last);
if(sfirst == slast) {
(segmented_fill_range)(traits::local(first), traits::local(last), value, is_local_seg_t(), local_cat_t());
}
else {
(segmented_fill_range)(traits::local(first), traits::end(sfirst), value, is_local_seg_t(), local_cat_t());
for(++sfirst; sfirst != slast; ++sfirst)
(segmented_fill_range)(traits::begin(sfirst), traits::end(sfirst), value, is_local_seg_t(), local_cat_t());
(segmented_fill_range)(traits::begin(sfirst), traits::local(last), value, is_local_seg_t(), local_cat_t());
}
}
template <class FwdIt, class T>
BOOST_CONTAINER_FORCEINLINE
void segmented_fill_dispatch(FwdIt first, FwdIt last, const T& value, const std::random_access_iterator_tag &)
{
(segmented_fill_n)(first, last - first, value);
}
template <class FwdIt, class Sent, class T, class Cat>
BOOST_CONTAINER_FORCEINLINE
void segmented_fill_dispatch(FwdIt first, Sent last, const T& value, const Cat &)
{
typedef segmented_iterator_traits<FwdIt> traits;
(segmented_fill_range)(first, last, value, typename traits::is_segmented_iterator(), Cat());
}
} // namespace detail_algo
//! Assigns \c value to every element in [first, last).
//! When \c Iter is a segmented iterator, exploits segmentation
//! to reduce per-element overhead.
//! When \c Sent is the same type as \c FwdIt and the iterator
//! category is random access, derives to \c segmented_fill_n.
template <class FwdIt, class Sent, class T>
BOOST_CONTAINER_FORCEINLINE
void segmented_fill(FwdIt first, Sent last, const T& value)
{
(segmented_generate)(first, last, detail_algo::constref_generator<T>(value));
detail_algo::segmented_fill_dispatch(first, last, value, typename iterator_traits<FwdIt>::iterator_category());
}
} // namespace container
@@ -21,11 +21,141 @@
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/experimental/segmented_iterator_traits.hpp>
#include <boost/container/experimental/segmented_generate_n.hpp>
#include <boost/container/detail/iterators.hpp>
#include <boost/container/detail/std_fwd.hpp>
namespace boost {
namespace container {
template <class FwdIt, class Size, class T>
FwdIt segmented_fill_n(FwdIt first, Size count, const T& value);
namespace detail_algo {
template <class OutIter, class Size, class T>
OutIter fill_n_scan_non_segmented(OutIter first, OutIter last, Size& count, const T& value, const std::random_access_iterator_tag &)
{
std::size_t range_sz = static_cast<std::size_t>(last - first);
const Size local_count = (std::size_t)count < range_sz ? count : (Size)range_sz;
#if defined(BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING)
Size cnt = local_count;
while(cnt >= Size(4)) {
*first = value; ++first;
*first = value; ++first;
*first = value; ++first;
*first = value; ++first;
cnt -= Size(4);
}
switch(cnt) {
case 3:
*first = value; ++first;
BOOST_FALLTHROUGH;
case 2:
*first = value; ++first;
BOOST_FALLTHROUGH;
case 1:
*first = value; ++first;
BOOST_FALLTHROUGH;
default:
break;
}
#else
for(Size cnt = local_count; cnt; ++first, --cnt)
*first = value;
#endif
count -= local_count;
return first;
}
template <class OutIter, class Size, class T, class Tag>
OutIter fill_n_scan_non_segmented(OutIter first, OutIter last, Size& count, const T& value, Tag)
{
Size local_count = count;
for (; local_count > 0 && first != last; ++first, --local_count)
*first = value;
count = local_count;
return first;
}
template <class OutIter, class Size, class T>
BOOST_CONTAINER_FORCEINLINE
OutIter fill_n_scan(OutIter first, OutIter last, Size& count, const T& value, non_segmented_iterator_tag)
{
return (fill_n_scan_non_segmented)(first, last, count, value, typename iterator_traits<OutIter>::iterator_category());
}
template <class SegIt, class Size, class T>
SegIt fill_n_scan(SegIt first, SegIt last, Size& count, const T& value, segmented_iterator_tag)
{
typedef segmented_iterator_traits<SegIt> traits;
typedef typename traits::local_iterator local_iterator;
typedef typename traits::segment_iterator segment_iterator;
typedef typename segmented_iterator_traits<local_iterator>::is_segmented_iterator is_local_seg_t;
segment_iterator scur = traits::segment(first);
segment_iterator const slast = traits::segment(last);
if(scur == slast) {
const local_iterator ll = traits::local(last);
const local_iterator r = (fill_n_scan)(traits::local(first), ll, count, value, is_local_seg_t());
return (r != ll) ? traits::compose(scur, r) : last;
}
else {
local_iterator r = fill_n_scan(traits::local(first), traits::end(scur), count, value, is_local_seg_t());
if (!count)
return traits::compose(scur, r);
for (++scur; scur != slast; ++scur) {
r = fill_n_scan(traits::begin(scur), traits::end(scur), count, value, is_local_seg_t());
if (!count)
return traits::compose(scur, r);
}
const local_iterator ll = traits::local(last);
r = fill_n_scan(traits::begin(slast), ll, count, value, is_local_seg_t());
return (r != ll) ? traits::compose(scur, r) : last;
}
}
template <class SegIter, class Size, class T>
SegIter segmented_fill_n_ref
(SegIter first, Size count, const T& value, segmented_iterator_tag)
{
typedef segmented_iterator_traits<SegIter> traits;
typedef typename traits::local_iterator local_iterator;
typedef typename traits::segment_iterator segment_iterator;
typedef typename segmented_iterator_traits<local_iterator>::is_segmented_iterator is_local_seg_t;
segment_iterator scur = traits::segment(first);
local_iterator lcur = traits::local(first);
while(1) {
lcur = fill_n_scan(lcur, traits::end(scur), count, value, is_local_seg_t());
if(count == 0)
break;
++scur;
lcur = traits::begin(scur);
}
return traits::compose(scur, lcur);
}
template <class OutIt, class Size, class T>
OutIt segmented_fill_n_ref
(OutIt first, Size count, const T& value, non_segmented_iterator_tag)
{
for(; count > 0; ++first, --count)
*first = value;
return first;
}
} // namespace detail_algo
//! Assigns \c value to the first \c count elements starting at \c first.
//! Returns an iterator past the last filled element.
//! Exploits segmentation when available.
@@ -33,7 +163,9 @@ template <class FwdIt, class Size, class T>
BOOST_CONTAINER_FORCEINLINE
FwdIt segmented_fill_n(FwdIt first, Size count, const T& value)
{
return (segmented_generate_n)(first, count, detail_algo::constref_generator<T>(value));
typedef segmented_iterator_traits<FwdIt> traits;
return detail_algo::segmented_fill_n_ref(first, count, value,
typename traits::is_segmented_iterator());
}
} // namespace container