[libcxx-commits] [libcxx] [libc++] Optimize std::{, ranges}::{fill, fill_n} for segmented iterators (PR #132665)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 8 00:18:46 PDT 2025


================
@@ -21,23 +24,42 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 // fill isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset.
 
-template <class _ForwardIterator, class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
-__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, forward_iterator_tag) {
+template <class _ForwardIterator, class _Sentinel, class _Tp>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
+__fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __value) {
   for (; __first != __last; ++__first)
     *__first = __value;
+  return __first;
 }
 
-template <class _RandomAccessIterator, class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
-__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value, random_access_iterator_tag) {
-  std::fill_n(__first, __last - __first, __value);
+template <class _RandomAccessIterator,
+          class _Tp,
+          __enable_if_t<__has_random_access_iterator_category<_RandomAccessIterator>::value &&
+                            !__is_segmented_iterator<_RandomAccessIterator>::value,
----------------
frederick-vs-ja wrote:

`__is_segmented_iterator` is recently replaced by `__is_segmented_iterator_v`.
```suggestion
                            !__is_segmented_iterator_v<_RandomAccessIterator>,
```
Ditto below.

https://github.com/llvm/llvm-project/pull/132665


More information about the libcxx-commits mailing list